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

Commit

Permalink
feat: adds support for fixing language in target project & enables it…
Browse files Browse the repository at this point in the history
… by default for CLI restoration
  • Loading branch information
Enngage committed Apr 1, 2020
1 parent 99ac6d5 commit 0d0c911
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Install package globally:

### Execution

> We recommend restoring backups to clean (empty) projects. Restoration process may make changes to target project such as changing language codenames to match source project.
To backup a project run:

`kbm --action=backup --apiKey=xxx --projectId=xxx`
Expand Down Expand Up @@ -101,6 +103,7 @@ const run = async () => {
// called when any content is imported
console.log(`Imported: ${item.title} | ${item.type}`);
},
fixLanguages: true,
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true,
Expand Down Expand Up @@ -131,6 +134,7 @@ const run = async () => {
// called when any content is imported
console.log(`Imported: ${item.title} | ${item.type}`);
},
fixLanguages: true,
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true
Expand Down
1 change: 1 addition & 0 deletions src/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const restoreAsync = async (config: ICliFileConfig) => {
console.log(`Imported: ${item.title} | ${item.type}`);
}
},
fixLanguages: true,
projectId: config.projectId,
apiKey: config.apiKey,
enableLog: config.enableLog,
Expand Down
1 change: 1 addition & 0 deletions src/import/import.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IImportConfig {
enableLog: boolean;
onUnsupportedBinaryFile?: (binaryFile: IBinaryFile) => void;
onImport?: (item: IProcessedItem) => void;
fixLanguages: boolean;
process?: {
taxonomy?: (item: TaxonomyContracts.ITaxonomyContract) => boolean | Promise<boolean>;
contentTypeSnippet?: (
Expand Down
78 changes: 69 additions & 9 deletions src/import/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import {
LanguageVariantContracts,
LanguageVariantModels,
ManagementClient,
SharedModels,
TaxonomyContracts,
TaxonomyModels,
SharedModels,
} from '@kentico/kontent-management';

import {
translationHelper,
idTranslateHelper,
IImportItemResult,
ItemType,
translationHelper,
ValidImportContract,
ValidImportModel,
} from '../core';
Expand Down Expand Up @@ -144,11 +144,7 @@ export class ImportService {
translationHelper.replaceIdReferencesWithCodenames(source.importData.languages, source.importData, {});
translationHelper.replaceIdReferencesWithCodenames(source.importData.assets, source.importData, {});
translationHelper.replaceIdReferencesWithCodenames(source.importData.contentItems, source.importData, {});
translationHelper.replaceIdReferencesWithCodenames(
source.importData.languageVariants,
source.importData,
{}
);
translationHelper.replaceIdReferencesWithCodenames(source.importData.languageVariants, source.importData, {});
}

private removeSkippedItemsFromImport(source: IImportSource): void {
Expand Down Expand Up @@ -229,6 +225,62 @@ export class ImportService {
}
}

private async fixLanguageAsync(
currentLanguages: LanguageModels.LanguageModel[],
importLanguage: LanguageContracts.ILanguageModelContract
): Promise<void> {
// check if language with given codename already exists
const existingLanguage = currentLanguages.find(m => m.codename === importLanguage.codename);

if (existingLanguage) {
// activate inactive languages
if (!existingLanguage.isActive) {
console.log(
`Language '${existingLanguage.name}' with codename '${existingLanguage.codename}' is not active in target project. Activating language.`
);

await this.client
.modifyLanguage()
.byLanguageCodename(existingLanguage.codename)
.withData([
{
op: 'replace',
property_name: 'is_active',
value: true
}
])
.toPromise();
}
}

// fix codename when source & target languages do not match
if (importLanguage.is_default) {
const defaultExistingLanguage = currentLanguages.find(m => m.id === importLanguage.id);

if (!defaultExistingLanguage) {
throw Error(`Invalid default existing language. Language with id '${importLanguage.id}' was not found.`);
}
if (importLanguage.codename !== defaultExistingLanguage.codename) {
// languages do not match, change it
console.log(
`Default language '${importLanguage.name}' with codename '${importLanguage.codename}' does not match default language in target project. Changing language codename in target project from '${defaultExistingLanguage.codename}' codename to '${importLanguage.codename}'`
);

await this.client
.modifyLanguage()
.byLanguageCodename(defaultExistingLanguage.codename)
.withData([
{
op: 'replace',
property_name: 'codename',
value: importLanguage.codename
}
])
.toPromise();
}
}
}

private tryGetLanguage(
currentLanguages: LanguageModels.LanguageModel[],
importLanguage: LanguageContracts.ILanguageModelContract
Expand All @@ -246,8 +298,8 @@ export class ImportService {
if (importLanguage.id === this.defaultLanguageId) {
const defaultCurrentLanguage = currentLanguages.find(m => m.id === this.defaultLanguageId);

// verify that codenames matches, otherwise end program
if (defaultCurrentLanguage && defaultCurrentLanguage.codename !== importLanguage.codename) {
// default language codename is source project is different than target project
throw Error(
`Codename of default language from imported data does not match target project. The source language codename is '${importLanguage.codename}' while target is '${defaultCurrentLanguage.codename}'. Please update codename of default language in target project to be '${importLanguage.codename}`
);
Expand Down Expand Up @@ -282,9 +334,17 @@ export class ImportService {
>[] = [];

// get current languages in project
const currentLanguagesResponse = await this.client.listLanguages().toAllPromise();
let currentLanguagesResponse = await this.client.listLanguages().toAllPromise();

for (const language of languages) {
// fix language if necessary
if (this.config.fixLanguages) {
await this.fixLanguageAsync(currentLanguagesResponse.data.items, language);

// reload existing languages = they were fixed
currentLanguagesResponse = await this.client.listLanguages().toAllPromise();
}

const processedLanguageData = this.tryGetLanguage(currentLanguagesResponse.data.items, language);

if (processedLanguageData === 'noImport') {
Expand Down
1 change: 1 addition & 0 deletions src/samples/restore-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const run = async () => {
projectId: 'targetProjectId',
apiKey: 'targetProjectId',
enableLog: true,
fixLanguages: true,
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000' // id that items are assigned
});

Expand Down

0 comments on commit 0d0c911

Please sign in to comment.