Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: refactors code to use external_id instead of codenames in conte…
Browse files Browse the repository at this point in the history
…nt types / snippets & taxonomies
  • Loading branch information
Enngage committed Feb 25, 2020
1 parent 80b2b76 commit 9c5ff72
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 173 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
],
"license": "MIT",
"dependencies": {
"@kentico/kontent-management": "0.3.17",
"@kentico/kontent-management": "0.3.18",
"jszip": "3.2.2",
"rxjs": "6.5.4",
"yargs": "15.1.0",
"flatted": "2.0.1"
},
"devDependencies": {
"@types/jszip": "3.1.7",
"@types/node": "13.1.8",
"@types/yargs": "15.0.1",
"@types/node": "13.7.4",
"@types/yargs": "15.0.3",
"standard-version": "7.1.0",
"ts-node": "8.6.2",
"tslint": "6.0.0",
"typescript": "3.7.5"
"typescript": "3.8.2"
}
}
2 changes: 0 additions & 2 deletions src/core/core.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export interface ICliFileConfig {
export type CliAction = 'backup' | 'restore' | 'clean';
export type ItemType =
| 'taxonomy'
| 'dummyContentType'
| 'contentType'
| 'dummyContentTypeSnippet'
| 'contentTypeSnippet'
| 'contentItem'
| 'languageVariant'
Expand Down
2 changes: 1 addition & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './core.models';
export * from './codename-translate-helper';
export * from './translation-helper';
export * from './id-translate-helper';
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
import { IIdCodenameTranslationResult } from './core.models';

export class CodenameTranslateHelper {
public replaceIdReferencesWithCodenames(
data: any,
allData: any,
storedCodenames: IIdCodenameTranslationResult
): void {
export class TranslationHelper {
public replaceIdReferencesWithExternalId(data: any): void {
if (data) {
if (Array.isArray(data)) {
for (const arrayItem of data) {
this.replaceIdReferencesWithCodenames(arrayItem, allData, storedCodenames);
this.replaceIdReferencesWithExternalId(arrayItem);
}
} else {
for (const key of Object.keys(data)) {
const val = (data as any)[key];
if (key.toLowerCase() === 'id') {
const id = (data as any).id;
const codename = (data as any).codename;
const externalId = (data as any).external_id;

if (!codename) {
// replace id with codename
const foundCodename = this.tryFindCodenameForId(id, allData, storedCodenames);

if (foundCodename) {
// remove id prop
delete data.id;

// set codename prop
data.codename = foundCodename;
}
if (!externalId && id) {
data.external_id = id;
delete data.id;
}
}

if (typeof val === 'object' && val !== null) {
this.replaceIdReferencesWithCodenames(val, allData, storedCodenames);
this.replaceIdReferencesWithExternalId(val);
}
}
}
}
}

public extractReferencedCodenames(data: any, allData: any, foundCodenames: string[]): void {
public replaceIdReferencesWithCodenames(
data: any,
allData: any,
storedCodenames: IIdCodenameTranslationResult
): void {
if (data) {
if (Array.isArray(data)) {
for (const arrayItem of data) {
this.extractReferencedCodenames(arrayItem, allData, foundCodenames);
this.replaceIdReferencesWithCodenames(arrayItem, allData, storedCodenames);
}
} else {
for (const key of Object.keys(data)) {
const val = (data as any)[key];
if (key.toLowerCase() === 'codename') {
if (key.toLowerCase() === 'id') {
const id = (data as any).id;
const codename = (data as any).codename;

if (codename && !id) {
if (!foundCodenames.includes(codename)) {
foundCodenames.push(codename);
if (!codename) {
// replace id with codename
const foundCodename = this.tryFindCodenameForId(id, allData, storedCodenames);

if (foundCodename) {
// remove id prop
delete data.id;

// set codename prop
data.codename = foundCodename;
}
}
}

if (typeof val === 'object' && val !== null) {
this.extractReferencedCodenames(val, allData, foundCodenames);
this.replaceIdReferencesWithCodenames(val, allData, storedCodenames);
}
}
}
Expand All @@ -73,7 +74,6 @@ export class CodenameTranslateHelper {
storedCodenames: IIdCodenameTranslationResult,
foundCodename?: string
): string | undefined {

// try looking up codename in stored references
const storedCodename = storedCodenames[findId];

Expand All @@ -89,17 +89,26 @@ export class CodenameTranslateHelper {
} else {
for (const key of Object.keys(data)) {
const val = (data as any)[key];
let candidateId: string | undefined;

if (key.toLowerCase() === 'id') {
const id = (data as any).id;
candidateId = (data as any).id;
}

if (key.toLocaleLowerCase() === 'external_id') {
candidateId = (data as any).external_id;
}

if (candidateId) {
const codename = (data as any).codename;

if (id && codename) {
if (codename) {
// store id -> codename mapping so that we don't have to always
// search for it as its expensive operation
storedCodenames[id] = codename;
storedCodenames[candidateId] = codename;
}

if (codename && id === findId) {
if (codename && candidateId === findId) {
return codename;
}
}
Expand All @@ -113,4 +122,4 @@ export class CodenameTranslateHelper {
}
}

export const codenameTranslateHelper = new CodenameTranslateHelper();
export const translationHelper = new TranslationHelper();
Loading

0 comments on commit 9c5ff72

Please sign in to comment.