From 0d6206a81e756a313bbae1ae66e37511b5fcbafa Mon Sep 17 00:00:00 2001 From: Dani Pinyol Date: Fri, 7 Aug 2020 07:37:05 +0200 Subject: [PATCH] feat(contentful): duplicate asset files for new locales --- .../bin/l10n/migrate-locales.sh | 2 ++ .../contentful/ignore-fallback-decorator.ts | 4 +-- .../src/tools/l10n/csv-import.ts | 28 +++++++++++++++++-- .../tools/l10n/import-csv-for-translators.ts | 3 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/botonic-plugin-contentful/bin/l10n/migrate-locales.sh b/packages/botonic-plugin-contentful/bin/l10n/migrate-locales.sh index f183f05522..a5ab826797 100755 --- a/packages/botonic-plugin-contentful/bin/l10n/migrate-locales.sh +++ b/packages/botonic-plugin-contentful/bin/l10n/migrate-locales.sh @@ -19,3 +19,5 @@ REMOVE_LOCALES=$5 ../../node_modules/.bin/ts-node --files src/tools/l10n/locale-migrate.ts \ "$FROM_FILE" "$TO_FILE" "$FROM_LOCALE" "$TO_LOCALE" "$REMOVE_LOCALES" + +echo "Change Element.image so that it does not have 1 version per locale" diff --git a/packages/botonic-plugin-contentful/src/contentful/ignore-fallback-decorator.ts b/packages/botonic-plugin-contentful/src/contentful/ignore-fallback-decorator.ts index 3d6bfaa5ea..1ba12a06b5 100644 --- a/packages/botonic-plugin-contentful/src/contentful/ignore-fallback-decorator.ts +++ b/packages/botonic-plugin-contentful/src/contentful/ignore-fallback-decorator.ts @@ -64,7 +64,7 @@ export class IgnoreFallbackDecorator implements DeliveryApi { getAsset(id: string, context: Context, query?: any): Promise { console.warn( - 'IgnoreFallbackDecorator does not any special traatment for getAsset' + 'IgnoreFallbackDecorator does not any special treatment for getAsset' ) return this.api.getAsset(id, context, query) } @@ -74,7 +74,7 @@ export class IgnoreFallbackDecorator implements DeliveryApi { query?: any ): Promise { console.warn( - 'IgnoreFallbackDecorator does not any special traatment for getAssets' + 'IgnoreFallbackDecorator does not any special treatment for getAssets' ) return this.api.getAssets(context, query) } diff --git a/packages/botonic-plugin-contentful/src/tools/l10n/csv-import.ts b/packages/botonic-plugin-contentful/src/tools/l10n/csv-import.ts index 3c9b07a732..51395b82c1 100644 --- a/packages/botonic-plugin-contentful/src/tools/l10n/csv-import.ts +++ b/packages/botonic-plugin-contentful/src/tools/l10n/csv-import.ts @@ -132,7 +132,7 @@ export class ReferenceFieldDuplicator { readonly manageContext: ManageContext ) {} - async duplicateReferenceFields() { + async duplicateReferenceFields(): Promise { const defaultLocale = await this.manageCms.getDefaultLocale() const fields = { [ContentType.TEXT]: [ContentFieldType.BUTTONS], @@ -140,9 +140,9 @@ export class ReferenceFieldDuplicator { [ContentType.ELEMENT]: [ContentFieldType.IMAGE], } for (const contentType of Object.keys(fields)) { - console.log(`***Duplicating contents of type ${contentType}`) + console.log(`***Duplicating contents of type '${contentType}'`) for (const fieldType of (fields as any)[contentType]) { - console.log(` **Duplicating field ${contentType}`) + console.log(` **Duplicating '${contentType}' fields`) await this.duplicate( defaultLocale, contentType as ContentType, @@ -150,6 +150,28 @@ export class ReferenceFieldDuplicator { ) } } + this.warning() + } + + async duplicateAssetFiles() { + const defaultLocale = await this.manageCms.getDefaultLocale() + console.log(`***Duplicating assets`) + const assets = await this.cms.assets({ locale: defaultLocale }) + console.log(` **Duplicating ${assets.length} assets`) + for (const a of assets) { + await this.manageCms.copyAssetFile( + this.manageContext, + a.id, + defaultLocale + ) + } + this.warning() + } + + private warning() { + if (this.manageContext.preview) { + console.warn('Remember to publish the entries from contentful.com') + } } private async duplicate( diff --git a/packages/botonic-plugin-contentful/src/tools/l10n/import-csv-for-translators.ts b/packages/botonic-plugin-contentful/src/tools/l10n/import-csv-for-translators.ts index 5047999391..8cf6ea9afc 100644 --- a/packages/botonic-plugin-contentful/src/tools/l10n/import-csv-for-translators.ts +++ b/packages/botonic-plugin-contentful/src/tools/l10n/import-csv-for-translators.ts @@ -75,6 +75,7 @@ async function main() { allowOverwrites: importType == ImportType.OVERWRITE, } + await readCsvForTranslators(manageOptions, manageContext, fileName) if (duplicateReferences) { console.log('Duplicating reference fields') await duplicateReferenceFields( @@ -87,7 +88,6 @@ async function main() { manageContext ) } - await readCsvForTranslators(manageOptions, manageContext, fileName) console.log('done') } catch (e) { console.error(e) @@ -112,6 +112,7 @@ async function duplicateReferenceFields( manageContext ) await referenceDuplicator.duplicateReferenceFields() + await referenceDuplicator.duplicateAssetFiles() } // void tells linters that we don't want to wait for promise