diff --git a/src/commands/export.ts b/src/commands/export.ts index b9c9ff2a..74d15f3e 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -15,7 +15,7 @@ import { languagesEntity } from "./importExportEntities/entities/languages.js"; import { languageVariantsEntity } from "./importExportEntities/entities/languageVariants.js"; import { previewUrlsExportEntity } from "./importExportEntities/entities/previewUrls.js"; import { rolesExportEntity } from "./importExportEntities/entities/roles.js"; -import { spacesExportEntity } from "./importExportEntities/entities/spaces.js"; +import { spacesEntity } from "./importExportEntities/entities/spaces.js"; import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js"; import { workflowsEntity } from "./importExportEntities/entities/workflows.js"; import { EntityExportDefinition } from "./importExportEntities/entityDefinition.js"; @@ -51,7 +51,7 @@ export const register: RegisterCommand = yargs => const entityDefinitions: ReadonlyArray> = [ collectionsEntity, - spacesExportEntity, + spacesEntity, taxonomiesEntity, languagesEntity, previewUrlsExportEntity, diff --git a/src/commands/import.ts b/src/commands/import.ts index 6f5ea53c..2de069b9 100644 --- a/src/commands/import.ts +++ b/src/commands/import.ts @@ -18,6 +18,7 @@ import { } from "./importExportEntities/entities/contentTypesSnippets.js"; import { languagesEntity } from "./importExportEntities/entities/languages.js"; import { languageVariantsEntity } from "./importExportEntities/entities/languageVariants.js"; +import { spacesEntity } from "./importExportEntities/entities/spaces.js"; import { taxonomiesEntity } from "./importExportEntities/entities/taxonomies.js"; import { workflowsEntity } from "./importExportEntities/entities/workflows.js"; import { EntityImportDefinition, ImportContext } from "./importExportEntities/entityDefinition.js"; @@ -60,6 +61,7 @@ const entityDefinitions: ReadonlyArray> = [ updateItemAndTypeReferencesInSnippetsImportEntity, updateItemAndTypeReferencesInTypesImportEntity, workflowsEntity, + spacesEntity, languageVariantsEntity, ]; @@ -112,4 +114,6 @@ const createInitialContext = (): ImportContext => ({ contentTypeContextByOldIds: new Map(), workflowIdsByOldIds: new Map(), worfklowStepsIdsWithTransitionsByOldIds: new Map(), + spaceIdsByOldIds: new Map(), + contentTypeIdsWithElementsByOldIds: new Map(), }); diff --git a/src/commands/importExportEntities/entities/assetFolders.ts b/src/commands/importExportEntities/entities/assetFolders.ts index fb38e61f..9797db2f 100644 --- a/src/commands/importExportEntities/entities/assetFolders.ts +++ b/src/commands/importExportEntities/entities/assetFolders.ts @@ -9,6 +9,10 @@ export const assetFoldersEntity: EntityDefinition { + if (!fileFolders.length) { + return; + } + const projectFolders = await client .modifyAssetFolders() .withData(fileFolders.map(createPatchToAddFolder)) diff --git a/src/commands/importExportEntities/entities/spaces.ts b/src/commands/importExportEntities/entities/spaces.ts index 0353a9d2..69e542fd 100644 --- a/src/commands/importExportEntities/entities/spaces.ts +++ b/src/commands/importExportEntities/entities/spaces.ts @@ -1,15 +1,38 @@ import { SpaceContracts } from "@kontent-ai/management-sdk"; +import { serially } from "../../../utils/requests.js"; import { EntityDefinition } from "../entityDefinition.js"; -export const spacesExportEntity: EntityDefinition> = { +export const spacesEntity: EntityDefinition> = { name: "spaces", fetchEntities: client => client.listSpaces().toPromise().then(res => res.rawData), serializeEntities: spaces => JSON.stringify(spaces), - importEntities: () => { - throw new Error("Not supported yet."); - }, - deserializeEntities: () => { - throw new Error("Not supported yet."); + deserializeEntities: JSON.parse, + importEntities: async (client, entities, context) => { + // does not add web_spotlight_root as it is not possible to activate Web Spotlight with MAPI + const newSpaces = await serially(entities.map(importSpace => () => + client + .addSpace() + .withData({ + name: importSpace.name, + codename: importSpace.codename, + }).toPromise() + )); + + return { + ...context, + spaceIdsByOldIds: new Map(entities + .map(oldSpace => { + const match = newSpaces.find(s => s.data.codename === oldSpace.codename); + + if (!match) { + throw new Error( + `Could not find space with codename ${oldSpace.codename} in the project. This should never happen`, + ); + } + + return [oldSpace.id, match.data.id]; + })), + }; }, }; diff --git a/src/commands/importExportEntities/entityDefinition.ts b/src/commands/importExportEntities/entityDefinition.ts index cc1d6034..871a05a4 100644 --- a/src/commands/importExportEntities/entityDefinition.ts +++ b/src/commands/importExportEntities/entityDefinition.ts @@ -75,6 +75,8 @@ export type ImportContext = Readonly<{ string, Readonly<{ selfId: string; oldTransitionIds: ReadonlyArray }> >; + contentTypeIdsWithElementsByOldIds: ReadonlyMap>; + spaceIdsByOldIds: IdsMap; }>; type IdsMap = ReadonlyMap;