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

Commit

Permalink
feat: adds project validation checks & force CLI option to export/imp…
Browse files Browse the repository at this point in the history
…ort project with issues, creates all dummy content types & snippets before creating elements
  • Loading branch information
Enngage committed Jan 23, 2020
1 parent ee7a1c2 commit b7b8dc5
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 77 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "wip-backup-manager",
"version": "0.0.1",
"description": "This utility enables backup & restore of Kentico Kontent projects",
"preferGlobal": true,
"bin": {
"kbm": "./_commonjs/src/cli/app.js"
},
Expand Down Expand Up @@ -34,7 +35,8 @@
"Kentico Kontent",
"Kontent backup manager",
"Kontent restore project",
"Kontent backup project"
"Kontent backup project",
"Kontent import"
],
"license": "MIT",
"dependencies": {
Expand Down
76 changes: 69 additions & 7 deletions src/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import yargs = require('yargs');

import { CleanService } from '../clean';
import { ICliFileConfig } from '../core';
import { ExportService } from '../export';
import { ImportService } from '../import';
import { ExportService, IExportAllResult } from '../export';
import { IImportSource, ImportService } from '../import';
import { ZipService } from '../zip';
import { ProjectContracts } from '@kentico/kontent-management';

const argv = yargs.argv;

Expand All @@ -33,9 +34,27 @@ const backup = async (config: ICliFileConfig) => {
enableLog: config.enableLog
});

const response = await exportService.exportAllAsync();
const report = await exportService.exportProjectValidationAsync();

await zipService.createZipAsync(response.data, response.metadata);
if (canExport(report, config)) {
const response = await exportService.exportAllAsync();
await zipService.createZipAsync(response);
} else {
console.log(`Project contains following inconsistencies:`);
for (const issue of report.type_issues) {
console.log(`Type ${issue.type.codename} has issues: ${issue.issues.map(m => m.messages).join(',')}`);
}
for (const issue of report.variant_issues) {
console.log(
`Variant ${issue.item.codename} (${issue.language.codename}) has issues: ${issue.issues
.map(m => m.messages)
.join(',')}`
);
}

console.log(`To export data regardless of issues, set 'force' config parameter to true`);
console.log(`Export failed. See reasons above`);
}
};

const clean = async (config: ICliFileConfig) => {
Expand All @@ -58,8 +77,6 @@ const restore = async (config: ICliFileConfig) => {
enableLog: config.enableLog
});

const data = await zipService.extractZipAsync();

const importService = new ImportService({
onImport: item => {
if (config.enableLog) {
Expand All @@ -78,7 +95,27 @@ const restore = async (config: ICliFileConfig) => {
}
});

await importService.importFromSourceAsync(data);
const data = await zipService.extractZipAsync();

if (canImport(data, config)) {
await importService.importFromSourceAsync(data);
} else {
console.log(`Project contains following inconsistencies:`);
for (const issue of data.validation.type_issues) {
console.log(`Type ${issue.type.codename} has issues: ${issue.issues.map(m => m.messages).join(',')}`);
}
for (const issue of data.validation.variant_issues) {
console.log(
`Variant ${issue.item.codename} (${issue.language.codename}) has issues: ${issue.issues
.map(m => m.messages)
.join(',')}`
);
}

console.log(`To import data regardless of issues, set 'force' config parameter to true, however keep in mind that without
fixing the issues, the import will likely fail.`);
console.log(`Import failed. See reasons above`);
}
};

const validateConfig = (config: any) => {
Expand Down Expand Up @@ -121,4 +158,29 @@ const process = async () => {
}
};

const canExport = (projectReport: ProjectContracts.IProjectReportResponseContract, config: ICliFileConfig) => {
const projectHasIssues = projectReport.variant_issues.length > 0 || projectReport.type_issues.length > 0;
if (!projectHasIssues) {
return true;
}

if (config.force === true) {
return true;
}

return false;
};

const canImport = (importData: IImportSource, config: ICliFileConfig) => {
if (!importData.metadata.isInconsistentExport) {
return true;
}

if (config.force === true) {
return true;
}

return false;
};

process();
3 changes: 2 additions & 1 deletion src/cli/sample-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"zipFilename": "backup",
"action": "backup",
"enableLog": true,
"importLanguages": false
"importLanguages": false,
"force": false
}
1 change: 1 addition & 0 deletions src/core/core.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ICliFileConfig {
zipFilename: string;
enableLog: boolean;
importLanguages: boolean;
force: boolean;
}

export type CliAction = 'backup' | 'restore' | 'clean';
Expand Down
3 changes: 3 additions & 0 deletions src/export/export.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
LanguageContracts,
LanguageVariantContracts,
TaxonomyContracts,
ProjectContracts,
} from '@kentico/kontent-management';

import { IProcessedItem } from '../core';
Expand All @@ -31,9 +32,11 @@ export interface IExportData {
export interface IExportMetadata {
projectId: string;
timestamp: Date;
isInconsistentExport: boolean;
}

export interface IExportAllResult {
metadata: IExportMetadata;
data: IExportData;
validation: ProjectContracts.IProjectReportResponseContract;
}
14 changes: 13 additions & 1 deletion src/export/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AssetContracts,
LanguageContracts,
AssetFolderContracts,
ProjectContracts,
} from '@kentico/kontent-management';

import { IExportAllResult, IExportConfig, IExportData } from './export.models';
Expand All @@ -26,6 +27,7 @@ export class ExportService {

public async exportAllAsync(): Promise<IExportAllResult> {
const contentTypes = await this.exportContentTypesAsync();
const projectValidation = await this.exportProjectValidationAsync();

const data: IExportData = {
contentTypes,
Expand All @@ -41,12 +43,22 @@ export class ExportService {
return {
metadata: {
timestamp: new Date(),
projectId: this.config.projectId
projectId: this.config.projectId,
isInconsistentExport: projectValidation.type_issues.length > 0 || projectValidation.variant_issues.length > 0
},
validation: projectValidation,
data
};
}

public async exportProjectValidationAsync(): Promise<ProjectContracts.IProjectReportResponseContract> {
const response = await this.client.validateProjectContent()
.forProjectId(this.config.projectId)
.toPromise();

return response.rawData;
}

public async exportAssetsAsync(): Promise<AssetContracts.IAssetModelContract[]> {
const response = await this.client.listAssets().toAllPromise();
response.data.items.forEach(m => this.processItem(m.fileName, 'asset', m));
Expand Down
9 changes: 9 additions & 0 deletions src/import/import.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
LanguageContracts,
LanguageVariantContracts,
TaxonomyContracts,
ProjectContracts,
} from '@kentico/kontent-management';

import { IProcessedItem, ItemType } from '../core';
Expand Down Expand Up @@ -61,6 +62,8 @@ export interface IImportSource {
languages: LanguageContracts.ILanguageModelContract[];
assets: AssetContracts.IAssetModelContract[];
};
metadata: IImportMetadata;
validation: ProjectContracts.IProjectReportResponseContract;
assetFolders: AssetFolderContracts.IAssetFolderContract[];
binaryFiles: IBinaryFile[];
}
Expand All @@ -76,3 +79,9 @@ export interface IFlattenedFolder {
externalId?: string;
id: string;
}

export interface IImportMetadata {
projectId: string;
timestamp: Date;
isInconsistentExport: boolean;
}
Loading

0 comments on commit b7b8dc5

Please sign in to comment.