UNPKG

@amplience/dc-cli

Version:
122 lines (121 loc) 6.47 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handler = exports.processSchemas = exports.doUpdate = exports.doCreate = exports.storedSchemaMapper = exports.builder = exports.LOG_FILENAME = exports.desc = exports.command = void 0; const dc_management_sdk_js_1 = require("dc-management-sdk-js"); 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 table_1 = require("table"); const table_consts_1 = require("../../common/table/table.consts"); const chalk_1 = __importDefault(require("chalk")); const create_service_1 = require("./create.service"); const update_service_1 = require("./update.service"); const import_service_1 = require("../../services/import.service"); const resolve_schema_body_1 = require("../../services/resolve-schema-body"); const log_helpers_1 = require("../../common/log-helpers"); const progress_bar_1 = require("../../common/progress-bar/progress-bar"); exports.command = 'import <dir>'; exports.desc = 'Import Content Type Schemas'; const LOG_FILENAME = (platform = process.platform) => (0, log_helpers_1.getDefaultLogPath)('schema', 'import', platform); exports.LOG_FILENAME = LOG_FILENAME; const builder = (yargs) => { yargs.positional('dir', { describe: 'Directory containing Content Type Schema definitions', type: 'string' }); yargs.option('logFile', { type: 'string', default: exports.LOG_FILENAME, describe: 'Path to a log file to write to.', coerce: log_helpers_1.createLog }); }; exports.builder = builder; const storedSchemaMapper = (schema, storedSchemas) => { const found = storedSchemas.find(stored => stored.schemaId === schema.schemaId); const mutatedSchema = found ? { ...schema, id: found.id } : schema; return new dc_management_sdk_js_1.ContentTypeSchema(mutatedSchema); }; exports.storedSchemaMapper = storedSchemaMapper; const doCreate = async (hub, schema, log) => { try { const createdSchemaType = await (0, create_service_1.createContentTypeSchema)(schema.body || '', schema.validationLevel || dc_management_sdk_js_1.ValidationLevel.CONTENT_TYPE, hub); log.addAction('CREATE', `${createdSchemaType.id}`); return createdSchemaType; } catch (err) { throw new Error(`Error registering content type schema with body: ${schema.body}\n\n${err}`); } }; exports.doCreate = doCreate; const equals = (a, b) => a.id === b.id && a.schemaId === b.schemaId && a.body === b.body && a.validationLevel === b.validationLevel; const doUpdate = async (client, schema, log) => { try { let retrievedSchema = await client.contentTypeSchemas.get(schema.id || ''); if (equals(retrievedSchema, schema) && retrievedSchema.status !== dc_management_sdk_js_1.Status.ARCHIVED) { return { contentTypeSchema: retrievedSchema, updateStatus: import_service_1.UpdateStatus.SKIPPED }; } if (retrievedSchema.status === dc_management_sdk_js_1.Status.ARCHIVED) { try { retrievedSchema = await retrievedSchema.related.unarchive(); } catch (err) { throw new Error(`Error unable unarchive content type ${schema.id}: ${err.message}`); } } const updatedSchema = await (0, update_service_1.updateContentTypeSchema)(retrievedSchema, schema.body || '', schema.validationLevel || dc_management_sdk_js_1.ValidationLevel.CONTENT_TYPE); log.addAction('UPDATE', `${retrievedSchema.id} ${retrievedSchema.version} ${updatedSchema.version}`); return { contentTypeSchema: updatedSchema, updateStatus: import_service_1.UpdateStatus.UPDATED }; } catch (err) { throw new Error(`Error updating content type schema ${schema.schemaId || '<unknown>'}: ${err.message}`); } }; exports.doUpdate = doUpdate; const processSchemas = async (schemasToProcess, client, hub, log) => { const progress = (0, progress_bar_1.progressBar)(schemasToProcess.length, 0, { title: 'Importing content type schemas' }); progress.start(schemasToProcess.length, 0); const data = [[chalk_1.default.bold('ID'), chalk_1.default.bold('Schema ID'), chalk_1.default.bold('Result')]]; for (const schema of schemasToProcess) { let status; let contentTypeSchema; if (schema.id) { const result = await (0, exports.doUpdate)(client, schema, log); contentTypeSchema = result.contentTypeSchema; status = result.updateStatus === import_service_1.UpdateStatus.SKIPPED ? 'UP-TO-DATE' : 'UPDATED'; } else { contentTypeSchema = await (0, exports.doCreate)(hub, schema, log); status = 'CREATED'; } progress.increment(); data.push([contentTypeSchema.id || '', contentTypeSchema.schemaId || '', status]); } progress.stop(); log.appendLine((0, table_1.table)(data, table_consts_1.streamTableOptions)); }; exports.processSchemas = processSchemas; const handler = async (argv) => { const { dir, logFile } = argv; const client = (0, dynamic_content_client_factory_1.default)(argv); const hub = await client.hubs.get(argv.hubId); const log = logFile.open(); const schemas = (0, import_service_1.loadJsonFromDirectory)(dir, dc_management_sdk_js_1.ContentTypeSchema); const [resolvedSchemas, resolveSchemaErrors] = await (0, resolve_schema_body_1.resolveSchemaBody)(schemas, dir); if (Object.keys(resolveSchemaErrors).length > 0) { const errors = Object.entries(resolveSchemaErrors) .map(value => { const [filename, error] = value; return `* ${filename} -> ${error}`; }) .join('\n'); throw new Error(`Unable to resolve the body for the following files:\n${errors}`); } const storedSchemas = await (0, paginator_1.default)(hub.related.contentTypeSchema.list); const schemasToProcess = Object.values(resolvedSchemas).map(resolvedSchema => (0, exports.storedSchemaMapper)(resolvedSchema, storedSchemas)); await (0, exports.processSchemas)(schemasToProcess, client, hub, log); await log.close(); }; exports.handler = handler;