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

Commit

Permalink
adds support for more types and refines import/export process
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Jan 9, 2020
1 parent ddebf52 commit 5eb91f5
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 51 deletions.
Binary file modified output/test.zip
Binary file not shown.
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 @@ -38,16 +38,16 @@
],
"license": "MIT",
"dependencies": {
"@kentico/kontent-management": "0.3.6",
"@kentico/kontent-management": "0.3.9",
"jszip": "3.2.2",
"rxjs": "6.5.4",
"yargs": "15.1.0",
"flatted": "2.0.1"
},
"devDependencies": {
"@types/jszip": "3.1.6",
"@types/node": "13.1.4",
"@types/yargs": "13.0.4",
"@types/jszip": "3.1.7",
"@types/node": "13.1.5",
"@types/yargs": "15.0.0",
"standard-version": "7.0.1",
"ts-node": "8.5.4",
"tslint": "5.20.1",
Expand Down
64 changes: 63 additions & 1 deletion src/clean/clean.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IManagementClient, ManagementClient } from '@kentico/kontent-management';

import { ICleanConfig, ICleanResult } from './clean.models';
import { ItemType } from '../core';
import { ICleanConfig, ICleanResult } from './clean.models';

export class CleanService {
private readonly client: IManagementClient;
Expand All @@ -14,9 +14,11 @@ export class CleanService {
}

public async cleanAllAsync(): Promise<ICleanResult> {
await this.cleanContentItemsAsync();
await this.cleanContentTypesAsync();
await this.cleanContentTypeSnippetsAsync();
await this.cleanTaxonomiesAsync();
await this.cleanAssetsAsync();

return {
metadata: {
Expand Down Expand Up @@ -71,6 +73,66 @@ export class CleanService {
}
}

public async cleanAssetsAsync(): Promise<void> {
const assets = (await this.client.listAssets().toAllPromise()).data.items;

for (const asset of assets) {
await this.client
.deleteAsset()
.byAssetId(asset.id)
.toPromise()
.then(m => {
this.processItem(asset.fileName, 'asset', asset);
})
.catch(error => this.handleCleanError(error));
}
}

public async cleanContentItemsAsync(): Promise<void> {
const contentItems = (await this.client.listContentItems().toAllPromise()).data.items;

for (const contentItem of contentItems) {
await this.cleanLanguageVariantsAsync(contentItem.id);

await this.client
.deleteContentItem()
.byItemId(contentItem.id)
.toPromise()
.then(m => {
this.processItem(contentItem.name, 'contentItem', contentItem);
})
.catch(error => this.handleCleanError(error));
}
}

public async cleanLanguageVariantsAsync(contentItemId: string): Promise<void> {
const languageVariants = (
await this.client
.listLanguageVariantsOfItem()
.byItemId(contentItemId)
.toPromise()
).data.items;

for (const languageVariant of languageVariants) {
const languageId = languageVariant.language.id;
const itemId = contentItemId;

if (!languageId) {
throw Error(`Missing language id for item '${contentItemId}'`);
}

await this.client
.deleteLanguageVariant()
.byItemId(itemId)
.byLanguageId(languageId)
.toPromise()
.then(m => {
this.processItem(itemId, 'languageVariant', languageVariant);
})
.catch(error => this.handleCleanError(error));
}
}

private handleCleanError(error: any): void {
console.log(error);
}
Expand Down
12 changes: 9 additions & 3 deletions src/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import JSZip = require('jszip');
import yargs = require('yargs');

import { ExportService } from '../export';
import { CliAction } from '../core';
import { CliAction, codenameTranslateHelper } from '../core';
import { ImportService } from '../import';
import { CleanService } from '../clean';

Expand Down Expand Up @@ -46,7 +46,10 @@ const importService = new ImportService({
console.log('imported item: ' + item.title);
},
projectId: targetProjectId,
apiKey: targetApiKey
apiKey: targetApiKey,
skip: {
languages: true
}
});

const cleanService = new CleanService({
Expand All @@ -63,12 +66,15 @@ const backup = async () => {
const response = await exportService.exportAllAsync();
const data = JSON.stringify(response);

codenameTranslateHelper.replaceIdReferencesWithCodenames(response, response);
const data2 = JSON.stringify(response);

const zip = new JSZip();

const dataFolder = zip.folder('data');

dataFolder.file('test1.json', data);
dataFolder.file('test2.json', data);
dataFolder.file('test2.json', data2);

zip.generateAsync({ type: 'nodebuffer' }).then(content => {
fs.writeFile('./' + filename, content, wError => {
Expand Down
4 changes: 3 additions & 1 deletion src/core/codename-translate-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export class CodenameTranslateHelper {
const codename = (data as any).codename;

if (codename && !id) {
foundCodenames.push(codename);
if (!foundCodenames.includes(codename)) {
foundCodenames.push(codename);
}
}
}
if (key !== '0') {
Expand Down
45 changes: 41 additions & 4 deletions src/core/core.models.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
import { ContentTypeModels, ContentTypeSnippetModels, TaxonomyModels } from '@kentico/kontent-management';
import {
AssetContracts,
AssetModels,
ContentItemModels,
ContentTypeContracts,
ContentTypeModels,
ContentTypeSnippetContracts,
ContentTypeSnippetModels,
LanguageContracts,
LanguageModels,
LanguageVariantModels,
TaxonomyContracts,
TaxonomyModels,
LanguageVariantContracts,
ContentItemContracts,
} from '@kentico/kontent-management';

export type CliAction = 'backup' | 'restore' | 'clean';
export type ItemType = 'taxonomy' | 'contentType' | 'contentTypeSnippet';
export type ValidImportType =
export type ItemType =
| 'taxonomy'
| 'contentType'
| 'contentTypeSnippet'
| 'contentItem'
| 'languageVariant'
| 'language'
| 'asset';

export type ValidImportModel =
| ContentTypeModels.ContentType
| TaxonomyModels.Taxonomy
| ContentTypeSnippetModels.ContentTypeSnippet;
| ContentTypeSnippetModels.ContentTypeSnippet
| LanguageVariantModels.ContentItemLanguageVariant
| ContentItemModels.ContentItem
| LanguageModels.LanguageModel
| AssetModels.Asset;

export type ValidImportContract =
| ContentTypeContracts.IContentTypeContract
| TaxonomyContracts.ITaxonomyContract
| ContentTypeSnippetContracts.IContentTypeSnippetContract
| ContentItemContracts.IContentItemModelContract
| TaxonomyContracts.ITaxonomyContract
| AssetContracts.IAssetModelContract
| LanguageVariantContracts.ILanguageVariantModelContract
| LanguageContracts.ILanguageModelContract;

export interface IProcessedItem {
title: string;
Expand Down
14 changes: 13 additions & 1 deletion src/export/export.models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { ContentTypeContracts, ContentTypeSnippetContracts, TaxonomyContracts } from '@kentico/kontent-management';
import {
ContentItemContracts,
ContentTypeContracts,
ContentTypeSnippetContracts,
TaxonomyContracts,
LanguageVariantContracts,
LanguageContracts,
AssetContracts,
} from '@kentico/kontent-management';

export interface IExportConfig {
projectId: string;
Expand All @@ -9,6 +17,10 @@ export interface IExportData {
taxonomies: TaxonomyContracts.ITaxonomyContract[];
contentTypeSnippets: ContentTypeSnippetContracts.IContentTypeSnippetContract[];
contentTypes: ContentTypeContracts.IContentTypeContract[];
contentItems: ContentItemContracts.IContentItemModelContract[];
languageVariants: LanguageVariantContracts.ILanguageVariantModelContract[];
languages: LanguageContracts.ILanguageModelContract[];
assets: AssetContracts.IAssetModelContract[];
}

export interface IExportAllResult {
Expand Down
46 changes: 45 additions & 1 deletion src/export/export.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {
ContentItemContracts,
ContentTypeContracts,
ContentTypeSnippetContracts,
IManagementClient,
LanguageVariantContracts,
ManagementClient,
TaxonomyContracts,
AssetContracts,
LanguageContracts,
} from '@kentico/kontent-management';

import { IExportAllResult, IExportConfig, IExportData } from './export.models';
Expand All @@ -19,10 +23,16 @@ export class ExportService {
}

public async exportAllAsync(): Promise<IExportAllResult> {
const contentItems = await this.exportContentItemsAsync();

const data: IExportData = {
contentTypes: await this.exportContentTypesAsync(),
contentTypeSnippets: await this.exportContentTypeSnippetsAsync(),
taxonomies: await this.exportTaxonomiesAsync()
taxonomies: await this.exportTaxonomiesAsync(),
contentItems,
languageVariants: await this.exportLanguageVariantsAsync(contentItems.map(m => m.id)),
assets: await this.exportAssetsAsync(),
languages: await this.exportLanguagesAsync()
};

return {
Expand All @@ -34,6 +44,16 @@ export class ExportService {
};
}

public async exportAssetsAsync(): Promise<AssetContracts.IAssetModelContract[]> {
const response = await this.client.listAssets().toPromise();
return response.data.items.map(m => m._raw);
}

public async exportLanguagesAsync(): Promise<LanguageContracts.ILanguageModelContract[]> {
const response = await this.client.listLanguages().toPromise();
return response.data.items.map(m => m._raw);
}

public async exportTaxonomiesAsync(): Promise<TaxonomyContracts.ITaxonomyContract[]> {
const response = await this.client.listTaxonomies().toPromise();
return response.data.taxonomies.map(m => m._raw);
Expand All @@ -48,4 +68,28 @@ export class ExportService {
const response = await this.client.listContentTypes().toAllPromise();
return response.data.items.map(m => m._raw);
}

public async exportContentItemsAsync(): Promise<ContentItemContracts.IContentItemModelContract[]> {
const response = await this.client.listContentItems().toAllPromise();
return response.data.items.map(m => m._raw);
}

public async exportLanguageVariantsAsync(
contentItemIds: string[]
): Promise<LanguageVariantContracts.ILanguageVariantModelContract[]> {
const languageVariants: LanguageVariantContracts.ILanguageVariantModelContract[] = [];

for (const itemId of contentItemIds) {
languageVariants.push(
...(
await this.client
.listLanguageVariantsOfItem()
.byItemId(itemId)
.toPromise()
).data.items.map(m => m._raw)
);
}

return languageVariants;
}
}
Loading

0 comments on commit 5eb91f5

Please sign in to comment.