Skip to content

Commit

Permalink
Implement import function
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Dec 4, 2023
1 parent 641bfcb commit 450cae5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const exportEntities = async (params: ExportEntitiesParams): Promise<void> => {
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);
}
}));
Expand All @@ -100,7 +100,7 @@ const exportEntities = async (params: ExportEntitiesParams): Promise<void> => {
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) => {
Expand Down
32 changes: 31 additions & 1 deletion src/commands/import.ts
Original file line number Diff line number Diff line change
@@ -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 <fileName> <environmentId>",
Expand All @@ -25,13 +28,40 @@ export const register: RegisterCommand = yargs => yargs.command({
handler: args => importEntities(args),
});

const entityDefinitions: ReadonlyArray<EntityDefinition<any>> = [
];

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}.`);
}));
};

0 comments on commit 450cae5

Please sign in to comment.