diff --git a/src/core/codename-translate-helper.ts b/src/core/codename-translate-helper.ts index 12e1a12..449fcba 100644 --- a/src/core/codename-translate-helper.ts +++ b/src/core/codename-translate-helper.ts @@ -1,9 +1,15 @@ +import { IIdCodenameTranslationResult } from './core.models'; + export class CodenameTranslateHelper { - public replaceIdReferencesWithCodenames(data: any, allData: any): void { + public replaceIdReferencesWithCodenames( + data: any, + allData: any, + storedCodenames: IIdCodenameTranslationResult + ): void { if (data) { if (Array.isArray(data)) { for (const arrayItem of data) { - this.replaceIdReferencesWithCodenames(arrayItem, allData); + this.replaceIdReferencesWithCodenames(arrayItem, allData, storedCodenames); } } else { for (const key of Object.keys(data)) { @@ -14,7 +20,7 @@ export class CodenameTranslateHelper { if (!codename) { // replace id with codename - const foundCodename = this.tryFindCodenameForId(id, allData); + const foundCodename = this.tryFindCodenameForId(id, allData, storedCodenames); if (foundCodename) { // remove id prop @@ -27,7 +33,7 @@ export class CodenameTranslateHelper { } if (typeof val === 'object' && val !== null) { - this.replaceIdReferencesWithCodenames(val, allData); + this.replaceIdReferencesWithCodenames(val, allData, storedCodenames); } } } @@ -61,11 +67,24 @@ export class CodenameTranslateHelper { } } - public tryFindCodenameForId(findId: string, data: any, foundCodename?: string): string | undefined { + public tryFindCodenameForId( + findId: string, + data: any, + storedCodenames: IIdCodenameTranslationResult, + foundCodename?: string + ): string | undefined { + + // try looking up codename in stored references + const storedCodename = storedCodenames[findId]; + + if (storedCodename) { + return storedCodename; + } + if (data) { if (Array.isArray(data)) { for (const arrayItem of data) { - foundCodename = this.tryFindCodenameForId(findId, arrayItem, foundCodename); + foundCodename = this.tryFindCodenameForId(findId, arrayItem, storedCodenames, foundCodename); } } else { for (const key of Object.keys(data)) { @@ -74,12 +93,18 @@ export class CodenameTranslateHelper { const id = (data as any).id; const codename = (data as any).codename; + if (id && codename) { + // store id -> codename mapping so that we don't have to always + // search for it as its expensive operation + storedCodenames[id] = codename; + } + if (codename && id === findId) { return codename; } } if (typeof val === 'object' && val !== null) { - foundCodename = this.tryFindCodenameForId(findId, val, foundCodename); + foundCodename = this.tryFindCodenameForId(findId, val, storedCodenames, foundCodename); } } } diff --git a/src/core/core.models.ts b/src/core/core.models.ts index 2db6d5b..6df3d62 100644 --- a/src/core/core.models.ts +++ b/src/core/core.models.ts @@ -72,3 +72,7 @@ export interface IImportItemResult { originalId?: string; importId?: string; } + +export interface IIdCodenameTranslationResult { + [key: string]: string; +} diff --git a/src/import/import.helper.ts b/src/import/import.helper.ts index 65b3ef7..763a19e 100644 --- a/src/import/import.helper.ts +++ b/src/import/import.helper.ts @@ -6,11 +6,13 @@ export class ImportHelper { // translate internal ids to codenames codenameTranslateHelper.replaceIdReferencesWithCodenames( sourceData.importData.contentTypes, - sourceData.importData + sourceData.importData, + {} ); codenameTranslateHelper.replaceIdReferencesWithCodenames( sourceData.importData.contentTypeSnippets, - sourceData.importData + sourceData.importData, + {} ); // flatten data diff --git a/src/import/import.service.ts b/src/import/import.service.ts index ff728f6..41c55d3 100644 --- a/src/import/import.service.ts +++ b/src/import/import.service.ts @@ -112,7 +112,7 @@ export class ImportService { importedItems.push(...importedLanguageVariants); if (this.config.enableLog) { - console.log(`Finished importig data`); + console.log(`Finished importing data`); } return importedItems; @@ -121,23 +121,28 @@ export class ImportService { private translateIds(source: IImportSource): void { codenameTranslateHelper.replaceIdReferencesWithCodenames( source.importData.contentTypes, - source.importData + source.importData, + {} ); codenameTranslateHelper.replaceIdReferencesWithCodenames( source.importData.contentTypeSnippets, - source.importData + source.importData, + {} ); codenameTranslateHelper.replaceIdReferencesWithCodenames( source.importData.assets, - source.importData + source.importData, + {} ); codenameTranslateHelper.replaceIdReferencesWithCodenames( source.importData.contentItems, - source.importData + source.importData, + {} ); codenameTranslateHelper.replaceIdReferencesWithCodenames( source.importData.languageVariants, - source.importData + source.importData, + {} ); }