diff --git a/src/commands/export.ts b/src/commands/export.ts index 29feb7da..2c283e00 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -88,7 +88,7 @@ const exportEntities = async (params: ExportEntitiesParams): Promise => { zip.file(`${def.name}.json`, result); } catch (err) { - console.error(`Could not export the project with environment id ${params.environmentId} due to ${err}`); + console.error(`Failed to export entity ${def.name} due to error ${err}. Stopping export...`); process.exit(1); } })); @@ -100,7 +100,7 @@ const exportEntities = async (params: ExportEntitiesParams): Promise => { await zip.generateAsync({ type: "nodebuffer" }) .then(content => fsPromises.writeFile(fileName, content)); - console.log(`All entities exported into ${fileName}.`); + console.log(`All entities from environment ${params.environmentId} were successfully exported into ${fileName}.`); }; const exportMetadata = async (environmentId: string) => { diff --git a/src/commands/import.ts b/src/commands/import.ts index 43f82c8b..cf8c6d11 100644 --- a/src/commands/import.ts +++ b/src/commands/import.ts @@ -1,7 +1,10 @@ +import { ManagementClient } from "@kontent-ai/management-sdk"; import * as fsPromises from "fs/promises"; import JSZip from "jszip"; import { RegisterCommand } from "../types/yargs.js"; +import { serially } from "../utils/requests.js"; +import { EntityDefinition } from "./importExportEntities/entityDefinition.js"; export const register: RegisterCommand = yargs => yargs.command({ command: "import ", @@ -25,13 +28,40 @@ export const register: RegisterCommand = yargs => yargs.command({ handler: args => importEntities(args), }); +const entityDefinitions: ReadonlyArray> = [ +]; + type ImportEntitiesParams = Readonly<{ environmentId: string; fileName: string; + apiKey: string; }>; const importEntities = async (params: ImportEntitiesParams) => { const root = await fsPromises.readFile(params.fileName).then(JSZip.loadAsync); + const client = new ManagementClient({ + environmentId: params.environmentId, + apiKey: params.apiKey, + }); + + console.log("Importing entities..."); + + await serially(entityDefinitions.map(def => async () => { + console.log(`Importing ${def.name}...`); + + try { + await root.file(`${def.name}.json`) + ?.async("string") + .then(def.deserializeEntities) + .then(e => def.importEntities(client, e, root)); + + console.log(`${def.name} imported`); + } + catch (err) { + console.error(`Failed to import entity ${def.name} due to error ${err}. Stopping import...`); + process.exit(1); + } - console.log("assets: ", await root.file("assets.json")?.async("string")); + console.log(`All entities were successfully imported into environment ${params.environmentId}.`); + })); };