UNPKG

@amplience/dc-cli

Version:
221 lines (220 loc) 10.3 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.builder = exports.coerceLog = exports.LOG_FILENAME = exports.desc = exports.command = void 0; const dynamic_content_client_factory_1 = __importDefault(require("../../services/dynamic-content-client-factory")); const archive_log_1 = require("../../common/archive/archive-log"); const confirm_all_content_1 = require("../../common/content-item/confirm-all-content"); const dc_management_sdk_js_1 = require("dc-management-sdk-js"); const log_helpers_1 = require("../../common/log-helpers"); const facet_1 = require("../../common/filter/facet"); const fetch_content_1 = require("../../common/filter/fetch-content"); const lodash_1 = require("lodash"); const get_content_items_by_ids_1 = require("../../common/content-item/get-content-items-by-ids"); const progress_bar_1 = require("../../common/progress-bar/progress-bar"); exports.command = 'unarchive [id]'; exports.desc = 'Unarchive Content Items'; const LOG_FILENAME = (platform = process.platform) => (0, log_helpers_1.getDefaultLogPath)('content-item', 'unarchive', platform); exports.LOG_FILENAME = LOG_FILENAME; const coerceLog = (logFile) => (0, log_helpers_1.createLog)(logFile, 'Content Items Unarchive Log'); exports.coerceLog = coerceLog; const builder = (yargs) => { yargs .positional('id', { type: 'string', describe: 'The ID of a content item to be unarchived. If id is not provided, this command will unarchive ALL content items through all content repositories in the hub.' }) .option('repoId', { type: 'string', describe: 'The ID of a content repository to search items in to be unarchived.', requiresArg: false }) .option('folderId', { type: 'string', describe: 'The ID of a folder to search items in to be unarchived.', requiresArg: false }) .option('facet', { type: 'string', describe: "Unarchive content matching the given facets. Provide facets in the format 'label:example name,locale:en-GB', spaces are allowed between values. A regex can be provided for text filters, surrounded with forward slashes. For more examples, see the readme." }) .option('revertLog', { type: 'string', describe: 'Path to a log file containing content items archived in a previous run of the archive command.\nWhen provided, archives all content items listed as ARCHIVE in the log file.', requiresArg: false }) .alias('f', 'force') .option('f', { type: 'boolean', boolean: true, describe: 'If present, there will be no confirmation prompt before unarchiving the found content.' }) .alias('s', 'silent') .option('s', { type: 'boolean', boolean: true, describe: 'If present, no log file will be produced.' }) .option('ignoreError', { type: 'boolean', boolean: true, describe: 'If present, unarchive requests that fail will not abort the process.' }) .option('logFile', { type: 'string', default: exports.LOG_FILENAME, describe: 'Path to a log file to write to.', coerce: exports.coerceLog }) .option('name', { type: 'string', hidden: true }) .option('schemaId', { type: 'string', hidden: true }) .option('ignoreSchemaValidation', { type: 'boolean', boolean: false, describe: 'Ignore content item schema validation during unarchive' }); }; exports.builder = builder; const getContentToUnarchiveWithIds = async ({ client, ids, revertItems }) => { let contentItemIds = ids; if (revertItems === null || revertItems === void 0 ? void 0 : revertItems.length) { contentItemIds = revertItems.map(item => item[0]); } const contentItems = await (0, get_content_items_by_ids_1.getContentByIds)(client, contentItemIds); const contentItemsWithRevert = contentItems.map(item => { const revertItem = revertItems === null || revertItems === void 0 ? void 0 : revertItems.find(revertItem => item.id === revertItem[0]); if (revertItem) { const [, key, keys] = revertItem; const deliveryKeys = (keys === null || keys === void 0 ? void 0 : keys.split(',')) || []; item.body._meta.deliveryKey = key || null; if (keys === null || keys === void 0 ? void 0 : keys.length) { item.body._meta.deliveryKeys = { values: deliveryKeys.map(k => ({ value: k })) }; } } return item; }); return contentItemsWithRevert.filter(item => item.status === dc_management_sdk_js_1.Status.ARCHIVED); }; const getContentToUnarchiveWithFacet = async ({ client, hubId, facet, repoId, folderId }) => { const hub = await client.hubs.get(hubId); const contentItems = await (0, fetch_content_1.getContent)(client, hub, facet, { repoId, folderId, status: dc_management_sdk_js_1.Status.ARCHIVED }); contentItems.forEach(item => { if (item instanceof dc_management_sdk_js_1.ContentItem) { delete item.body._meta.deliveryKey; delete item.body._meta.deliveryKeys; } if (item instanceof dc_management_sdk_js_1.FacetedContentItem) { if ('deliveryKey' in item) { delete item.deliveryKey; } if ('deliveryKeys' in item) { delete item.deliveryKeys; } } }); return contentItems; }; const processItems = async ({ contentItems, log, ignoreError, ignoreSchemaValidation }) => { var _a, _b, _c, _d, _e, _f, _g, _h; const progress = (0, progress_bar_1.progressBar)(contentItems.length, 0, { title: 'Unarchiving content items' }); const failedUnarchives = []; for (let i = 0; i < contentItems.length; i++) { try { const deliveryKey = (_b = (_a = contentItems[i].body) === null || _a === void 0 ? void 0 : _a._meta) === null || _b === void 0 ? void 0 : _b.deliveryKey; const deliveryKeys = (_d = (_c = contentItems[i].body) === null || _c === void 0 ? void 0 : _c._meta) === null || _d === void 0 ? void 0 : _d.deliveryKeys; contentItems[i] = await contentItems[i].related.unarchive(); if (((_f = (_e = contentItems[i].body) === null || _e === void 0 ? void 0 : _e._meta) === null || _f === void 0 ? void 0 : _f.deliveryKey) !== deliveryKey || !(0, lodash_1.isEqual)((_h = (_g = contentItems[i].body) === null || _g === void 0 ? void 0 : _g._meta) === null || _h === void 0 ? void 0 : _h.deliveryKeys, deliveryKeys)) { contentItems[i].body._meta.deliveryKey = deliveryKey || null; contentItems[i].body._meta.deliveryKeys = deliveryKeys; const updateParams = { ...(ignoreSchemaValidation ? { ignoreSchemaValidation: true } : {}) }; await contentItems[i].related.update(contentItems[i], updateParams); } log.addAction('UNARCHIVE', `${contentItems[i].id}\n`); progress.increment(); } catch (e) { failedUnarchives.push(contentItems[i]); progress.increment(); log.addComment(`UNARCHIVE FAILED: ${contentItems[i].id}`); log.addComment(e.toString()); if (ignoreError) { log.warn(`Failed to unarchive ${contentItems[i].label} (${contentItems[i].id}), continuing.`, e); } else { progress.stop(); log.error(`Failed to unarchive ${contentItems[i].label} (${contentItems[i].id}), aborting.`, e); break; } } } progress.stop(); return { failedUnarchives }; }; const handler = async (argv) => { const { id, logFile, force, silent, ignoreError, hubId, revertLog, repoId, folderId, ignoreSchemaValidation } = argv; const log = logFile.open(); const facet = (0, facet_1.withOldFilters)(argv.facet, argv); const client = (0, dynamic_content_client_factory_1.default)(argv); const allContent = !id && !facet && !revertLog && !folderId && !repoId; if (repoId && id) { log.appendLine('ID of content item is specified, ignoring repository ID'); } if (id && facet) { log.appendLine('Please specify either a facet or an ID - not both.'); return; } if (repoId && folderId) { log.appendLine('Folder is specified, ignoring repository ID'); } if (allContent) { log.appendLine('No filter was given, archiving all content'); } let ids = []; let revertItems = []; if (id) { ids = Array.isArray(id) ? id : [id]; } if (revertLog) { const log = await new archive_log_1.ArchiveLog().loadFromFile(revertLog); revertItems = log.getData('ARCHIVE').map(args => args.split(' ')); ids = revertItems.map(item => item[0]); } const contentItems = ids.length ? await getContentToUnarchiveWithIds({ client, ids, revertItems }) : await getContentToUnarchiveWithFacet({ client, hubId, facet, repoId, folderId }); if (!contentItems.length) { log.appendLine('Nothing found to unarchive, aborting'); return; } const missingContentItems = ids.length > 0 ? Boolean(ids.length !== contentItems.length) : false; logFile.appendLine(`Found ${contentItems.length} content items to unarchive`); if (!force) { const yes = await (0, confirm_all_content_1.confirmAllContent)('unarchive', 'content item', allContent, missingContentItems); if (!yes) { return; } } const { failedUnarchives } = await processItems({ contentItems, log, ignoreError, ignoreSchemaValidation }); const failedUnarchiveMsg = failedUnarchives.length ? `with ${failedUnarchives.length} failed archives - check logs for details` : ``; log.appendLine(`Unarchived content items ${failedUnarchiveMsg}`); await log.close(!silent); }; exports.handler = handler;