@amplience/dc-cli
Version:
Dynamic Content CLI Tool
93 lines (92 loc) • 3.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeCloneStep = void 0;
const clone_hub_step_1 = require("../model/clone-hub-step");
const path_1 = require("path");
const export_1 = require("../../content-type/export");
const import_1 = require("../../content-type/import");
const dynamic_content_client_factory_1 = __importDefault(require("../../../services/dynamic-content-client-factory"));
const fs_1 = require("fs");
class TypeCloneStep {
getId() {
return clone_hub_step_1.CloneHubStepId.Type;
}
getName() {
return 'Clone Content Types';
}
async run(state) {
try {
state.logFile.appendLine(`Exporting existing types from destination.`);
await (0, export_1.handler)({
dir: (0, path_1.join)(state.path, 'oldType'),
force: true,
logFile: state.logFile,
...state.to
});
}
catch (e) {
state.logFile.appendLine(`ERROR: Could not export existing destination types. \n${e}`);
return false;
}
try {
state.logFile.appendLine(`Exporting types from source.`);
await (0, export_1.handler)({
dir: (0, path_1.join)(state.path, 'type'),
force: true,
logFile: state.logFile,
...state.from
});
}
catch (e) {
state.logFile.appendLine(`ERROR: Could not export types. \n${e}`);
return false;
}
try {
await (0, import_1.handler)({
dir: (0, path_1.join)(state.path, 'type'),
sync: true,
logFile: state.logFile,
...state.to
});
}
catch (e) {
state.logFile.appendLine(`ERROR: Could not import types. \n${e}`);
return false;
}
return true;
}
async revert(state) {
const client = (0, dynamic_content_client_factory_1.default)(state.to);
const toArchive = state.revertLog.getData('CREATE', this.getName());
const toUpdate = state.revertLog.getData('UPDATE', this.getName());
for (let i = 0; i < toArchive.length; i++) {
try {
const type = await client.contentTypes.get(toArchive[i]);
await type.related.archive();
state.logFile.addAction('ARCHIVE', toArchive[i]);
}
catch (e) {
state.logFile.appendLine(`Couldn't archive content type ${toArchive[i]}. Continuing...`);
}
}
if (toUpdate.length > 0 && (0, fs_1.existsSync)((0, path_1.join)(state.path, 'oldType'))) {
try {
await (0, import_1.handler)({
dir: (0, path_1.join)(state.path, 'oldType'),
sync: true,
logFile: state.logFile,
...state.to
}, toUpdate.map(item => item.split(' ')[0]));
}
catch (e) {
state.logFile.appendLine(`ERROR: Could not import old types. \n${e}`);
return false;
}
}
return true;
}
}
exports.TypeCloneStep = TypeCloneStep;