Skip to content

Commit

Permalink
feat(contentful): duplicate asset files for new locales
Browse files Browse the repository at this point in the history
  • Loading branch information
dpinol committed Aug 7, 2020
1 parent 11da025 commit 0d6206a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class IgnoreFallbackDecorator implements DeliveryApi {

getAsset(id: string, context: Context, query?: any): Promise<Asset> {
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)
}
Expand All @@ -74,7 +74,7 @@ export class IgnoreFallbackDecorator implements DeliveryApi {
query?: any
): Promise<contentful.AssetCollection> {
console.warn(
'IgnoreFallbackDecorator does not any special traatment for getAssets'
'IgnoreFallbackDecorator does not any special treatment for getAssets'
)
return this.api.getAssets(context, query)
}
Expand Down
28 changes: 25 additions & 3 deletions packages/botonic-plugin-contentful/src/tools/l10n/csv-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,46 @@ export class ReferenceFieldDuplicator {
readonly manageContext: ManageContext
) {}

async duplicateReferenceFields() {
async duplicateReferenceFields(): Promise<void> {
const defaultLocale = await this.manageCms.getDefaultLocale()
const fields = {
[ContentType.TEXT]: [ContentFieldType.BUTTONS],
[ContentType.STARTUP]: [ContentFieldType.BUTTONS],
[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,
fieldType as ContentFieldType
)
}
}
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -87,7 +88,6 @@ async function main() {
manageContext
)
}
await readCsvForTranslators(manageOptions, manageContext, fileName)
console.log('done')
} catch (e) {
console.error(e)
Expand All @@ -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
Expand Down

0 comments on commit 0d6206a

Please sign in to comment.