@rezakalfane/dc-cli
Version:
Dynamic Content CLI Tool
95 lines (94 loc) • 3.86 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const file_log_1 = require("../../common/file-log");
const log_helpers_1 = require("../../common/log-helpers");
const fs_1 = require("fs");
const util_1 = require("util");
const dynamic_content_client_factory_1 = __importDefault(require("../../services/dynamic-content-client-factory"));
const publish_queue_1 = require("../../common/import/publish-queue");
exports.command = 'import <baseRepo> <filePath>';
exports.desc = 'Import Hierarchies';
exports.LOG_FILENAME = (platform = process.platform) => log_helpers_1.getDefaultLogPath('hierarchies', 'import', platform);
exports.builder = (yargs) => {
yargs
.positional('baseRepo', {
type: 'string',
requiresArg: true,
describe: 'Import matching the given repository to the import base directory, by ID.'
})
.positional('filePath', {
describe: 'Source file path containing Hierarchy definition',
requiresArg: true,
type: 'string'
})
.option('logFile', {
type: 'string',
default: exports.LOG_FILENAME,
describe: 'Path to a log file to write to.'
})
.alias('f', 'force')
.option('f', {
type: 'boolean',
boolean: true,
describe: 'Overwrite Hierarchy without asking.'
})
.option('publish', {
type: 'boolean',
boolean: true,
describe: 'Publish content items.'
});
};
async function importHierarchy(repo, publishable, node, parentId = null) {
const contentItem = {
body: node.contentItem.body,
label: node.contentItem.label,
locale: "en-US",
hierarchy: node.contentItem.hierarchy
};
if (parentId) {
contentItem.body._meta.hierarchy.parentId = parentId;
contentItem.hierarchy.parentId = parentId;
}
const newContent = await repo.related.contentItems.create(contentItem);
publishable.push(newContent);
const newContentItemId = newContent.id;
console.log(`Imported ${node.label} - ID: ${newContentItemId}, parent: ${parentId}`);
const children = node.children;
if (children.length > 0) {
await Promise.all(children.map((item) => importHierarchy(repo, publishable, item, newContentItemId)));
}
}
exports.handler = async (argv) => {
const { filePath: sourceFile, logFile, baseRepo, force, answer = true } = argv;
const log = typeof logFile === 'string' || logFile == null ? new file_log_1.FileLog(logFile) : logFile;
let publishable = [];
try {
const client = dynamic_content_client_factory_1.default(argv);
const repo = await client.contentRepositories.get(baseRepo);
const exportedHierarchy = await util_1.promisify(fs_1.readFile)(sourceFile, { encoding: 'utf8' });
const hierarchy = JSON.parse(exportedHierarchy);
await importHierarchy(repo, publishable, hierarchy);
if (argv.publish) {
const pubQueue = new publish_queue_1.PublishQueue(argv);
log.appendLine(`Publishing ${publishable.length} items.`);
await Promise.all(publishable.map((item) => pubQueue.publish(item)));
log.appendLine(`Waiting for all publishes to complete...`);
await pubQueue.waitForAll();
log.appendLine(`Finished publishing, with ${pubQueue.failedJobs.length} failed publishes total.`);
pubQueue.failedJobs.forEach(job => {
log.appendLine(` - ${job.item.label}`);
});
}
log.appendLine('Done!');
if (log) {
await log.close();
}
process.stdout.write('\n');
}
catch (e) {
console.log(e);
}
};