This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prepares core functionality for importing data (ordering, extra…
…cting depds..)
- Loading branch information
Showing
16 changed files
with
253 additions
and
44 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
export class CodenameTranslateHelper { | ||
public replaceIdReferencesWithCodenames(data: any, allData: any): void { | ||
if (data) { | ||
if (Array.isArray(data)) { | ||
for (const arrayItem of data) { | ||
this.replaceIdReferencesWithCodenames(arrayItem, allData); | ||
} | ||
} 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; | ||
|
||
if (!codename) { | ||
// replace id with codename | ||
const foundCodename = this.tryFindCodenameForId(id, allData); | ||
|
||
if (foundCodename) { | ||
// remove id prop | ||
delete data.id; | ||
|
||
// set codename prop | ||
data.codename = foundCodename; | ||
} | ||
} | ||
} | ||
if (key !== '0') { | ||
this.replaceIdReferencesWithCodenames(val, allData); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public extractReferencedCodenames(data: any, allData: any, foundCodenames: string[]): void { | ||
if (data) { | ||
if (Array.isArray(data)) { | ||
for (const arrayItem of data) { | ||
this.extractReferencedCodenames(arrayItem, allData, foundCodenames); | ||
} | ||
} else { | ||
for (const key of Object.keys(data)) { | ||
const val = (data as any)[key]; | ||
if (key.toLowerCase() === 'codename') { | ||
const id = (data as any).id; | ||
const codename = (data as any).codename; | ||
|
||
if (codename && !id) { | ||
foundCodenames.push(codename); | ||
} | ||
} | ||
if (key !== '0') { | ||
this.extractReferencedCodenames(val, allData, foundCodenames); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public tryFindCodenameForId(findId: string, data: any, foundCodename?: string): string | undefined { | ||
if (data) { | ||
if (Array.isArray(data)) { | ||
for (const arrayItem of data) { | ||
foundCodename = this.tryFindCodenameForId(findId, arrayItem, foundCodename); | ||
} | ||
} 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; | ||
|
||
if (codename && id === findId) { | ||
return codename; | ||
} | ||
} | ||
if (key !== '0') { | ||
foundCodename = this.tryFindCodenameForId(findId, val, foundCodename); | ||
} | ||
} | ||
} | ||
} | ||
return foundCodename; | ||
} | ||
} | ||
|
||
export const codenameTranslateHelper = new CodenameTranslateHelper(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ContentTypeModels, ContentTypeSnippetModels, TaxonomyModels } from '@kentico/kontent-management'; | ||
|
||
export type CliAction = 'backup' | 'restore' | 'clean'; | ||
export type ItemType = 'taxonomy' | 'contentType' | 'contentTypeSnippet'; | ||
export type ValidImportType = | ||
| ContentTypeModels.ContentType | ||
| TaxonomyModels.Taxonomy | ||
| ContentTypeSnippetModels.ContentTypeSnippet; | ||
|
||
export interface IProcessedItem { | ||
title: string; | ||
type: ItemType; | ||
data: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './core.models'; | ||
export * from './codename-translate-helper'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { codenameTranslateHelper } from '../core'; | ||
import { IExportData } from '../export'; | ||
import { IImportData, IPreparedImportItem } from './import.models'; | ||
|
||
export class ImportHelper { | ||
public prepareImportData(exportData: IExportData): IImportData { | ||
// translate internal ids to codenames | ||
codenameTranslateHelper.replaceIdReferencesWithCodenames(exportData, exportData); | ||
|
||
// flatten data | ||
const items = this.flattenExportData(exportData); | ||
|
||
// order items so that they can be imported (e.g. first import item and then item that depends on it) | ||
this.orderItemsByDeps(items); | ||
|
||
return { | ||
orderedImportItems: items | ||
}; | ||
} | ||
|
||
private orderItemsByDeps(items: IPreparedImportItem[]): IPreparedImportItem[] { | ||
for (const item of items) { | ||
// set deps of item | ||
item.deps.push(...this.getDependenciesOfItem(item, items)); | ||
} | ||
|
||
const sortedItems = items.sort((a, b) => { | ||
if (a.codename === b.codename) { | ||
return 0; | ||
} | ||
|
||
// order items so that dependent items are first | ||
if (a.deps.includes(b.codename)) { | ||
return 1; | ||
} else { | ||
return -1; | ||
} | ||
}); | ||
|
||
return sortedItems; | ||
} | ||
|
||
private getDependenciesOfItem( | ||
item: IPreparedImportItem, | ||
allItems: IPreparedImportItem[] | ||
): string[] { | ||
const deps: string[] = []; | ||
|
||
// get referenced codenames in item | ||
codenameTranslateHelper.extractReferencedCodenames(item, allItems, deps); | ||
|
||
// filter codename of the item itself | ||
const filteredDeps = deps.filter(m => m !== item.codename); | ||
|
||
return filteredDeps; | ||
} | ||
|
||
private flattenExportData(exportData: IExportData): IPreparedImportItem[] { | ||
return [ | ||
...exportData.taxonomies.map(m => { | ||
return <IPreparedImportItem> { | ||
codename: m.codename, | ||
deps: [], | ||
item: m, | ||
type: 'taxonomy' | ||
}; | ||
}), | ||
...exportData.contentTypeSnippets.map(m => { | ||
return <IPreparedImportItem> { | ||
codename: m.codename, | ||
deps: [], | ||
item: m, | ||
type: 'contentTypeSnippet' | ||
}; | ||
}), | ||
...exportData.contentTypes.map(m => { | ||
return <IPreparedImportItem> { | ||
codename: m.codename, | ||
deps: [], | ||
item: m, | ||
type: 'contentType' | ||
}; | ||
}) | ||
]; | ||
} | ||
} | ||
|
||
export const importHelper = new ImportHelper(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './import.models'; | ||
export * from './import.service'; | ||
export * from './import.helper'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Public API | ||
export * from './models'; | ||
export * from './core'; | ||
export * from './export'; | ||
export * from './import'; | ||
export * from './clean'; |
Oops, something went wrong.