@amplience/dc-cli
Version:
Dynamic Content CLI Tool
83 lines (82 loc) • 3.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaCloneStep = void 0;
const clone_hub_step_1 = require("../model/clone-hub-step");
const path_1 = require("path");
const export_1 = require("../../content-type-schema/export");
const import_1 = require("../../content-type-schema/import");
const dynamic_content_client_factory_1 = __importDefault(require("../../../services/dynamic-content-client-factory"));
const paginator_1 = __importDefault(require("../../../common/dc-management-sdk-js/paginator"));
const dc_management_sdk_js_1 = require("dc-management-sdk-js");
class SchemaCloneStep {
getId() {
return clone_hub_step_1.CloneHubStepId.Schema;
}
getName() {
return 'Clone Content Type Schemas';
}
async run(state) {
try {
await (0, export_1.handler)({
dir: (0, path_1.join)(state.path, 'schema'),
force: true,
logFile: state.logFile,
...state.from
});
}
catch (e) {
state.logFile.appendLine(`ERROR: Could not export schemas. \n${e}`);
return false;
}
try {
await (0, import_1.handler)({
dir: (0, path_1.join)(state.path, 'schema'),
logFile: state.logFile,
...state.to
});
}
catch (e) {
state.logFile.appendLine(`ERROR: Could not import schemas. \n${e}`);
return false;
}
return true;
}
async revert(state) {
const client = (0, dynamic_content_client_factory_1.default)(state.to);
const hub = await client.hubs.get(state.to.hubId);
const types = await (0, paginator_1.default)(hub.related.contentTypes.list);
const revertLog = state.revertLog;
const toArchive = revertLog.getData('CREATE', this.getName());
const toUpdate = revertLog.getData('UPDATE', this.getName());
for (const id of toArchive) {
try {
const schema = await client.contentTypeSchemas.get(id);
if (schema.status === dc_management_sdk_js_1.Status.ACTIVE) {
await schema.related.archive();
}
}
catch (e) {
state.logFile.appendLine(`Could not archive ${id}. Continuing...`);
}
}
for (const id of toUpdate) {
const updateArgs = id.split(' ');
try {
const schema = await client.contentTypeSchemas.getByVersion(updateArgs[0], Number(updateArgs[1]));
await schema.related.update(schema);
const typeToSync = types.find(type => type.contentTypeUri === schema.schemaId);
if (typeToSync) {
typeToSync.related.contentTypeSchema.update();
}
}
catch (e) {
state.logFile.appendLine(`Error while updating ${id}. Continuing...`);
}
}
return true;
}
}
exports.SchemaCloneStep = SchemaCloneStep;