UNPKG

@amplience/dc-cli

Version:
212 lines (211 loc) 8.69 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.processItems = exports.getContentItems = exports.builder = 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"); 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 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.' }) .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 getContentItems = async ({ client, id, hubId, repoId, folderId, revertLog, facet }) => { try { let contentItems = []; let revertItems = []; if (revertLog != null) { const log = await new archive_log_1.ArchiveLog().loadFromFile(revertLog); revertItems = log.getData('ARCHIVE').map(args => args.split(' ')); id = revertItems.map(item => item[0]); } if (id != null) { const itemIds = Array.isArray(id) ? id : [id]; const items = []; for (let i = 0; i < itemIds.length; i++) { const id = itemIds[i]; try { const contentItem = await client.contentItems.get(id); items.push(contentItem); if (revertItems.length == itemIds.length) { contentItem.body._meta.deliveryKey = revertItems[i][1]; } } catch { } } contentItems.push(...items.filter(item => item.status === dc_management_sdk_js_1.Status.ARCHIVED)); return { contentItems, missingContent: contentItems.length != itemIds.length }; } const hub = await client.hubs.get(hubId); contentItems = await (0, fetch_content_1.getContent)(client, hub, facet, { repoId, folderId, status: dc_management_sdk_js_1.Status.ARCHIVED }); contentItems.forEach(item => delete item.body._meta.deliveryKey); return { contentItems, missingContent: false }; } catch (err) { console.log(err); return { contentItems: [], missingContent: false }; } }; exports.getContentItems = getContentItems; const processItems = async ({ contentItems, force, silent, logFile, allContent, missingContent, ignoreError, ignoreSchemaValidation }) => { if (contentItems.length == 0) { console.log('Nothing found to unarchive, aborting.'); return; } console.log('The following content items will be unarchived:'); contentItems.forEach((contentItem) => { console.log(` ${contentItem.label} (${contentItem.id})`); }); console.log(`Total: ${contentItems.length}`); if (!force) { const yes = await (0, confirm_all_content_1.confirmAllContent)('unarchive', 'content item', allContent, missingContent); if (!yes) { return; } } const timestamp = Date.now().toString(); const log = new archive_log_1.ArchiveLog(`Content Items Unarchive Log - ${timestamp}\n`); let successCount = 0; for (let i = 0; i < contentItems.length; i++) { try { const deliveryKey = contentItems[i].body._meta.deliveryKey; contentItems[i] = await contentItems[i].related.unarchive(); if (contentItems[i].body._meta.deliveryKey != deliveryKey) { contentItems[i].body._meta.deliveryKey = deliveryKey; const updateParams = { ...(ignoreSchemaValidation ? { ignoreSchemaValidation: true } : {}) }; await contentItems[i].related.update(contentItems[i], updateParams); } log.addAction('UNARCHIVE', `${contentItems[i].id}\n`); successCount++; } catch (e) { 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 { log.error(`Failed to unarchive ${contentItems[i].label} (${contentItems[i].id}), aborting.`, e); break; } } } if (!silent && logFile) { await log.writeToFile(logFile.replace('<DATE>', timestamp)); } console.log(`Unarchived ${successCount} content items.`); }; exports.processItems = processItems; const handler = async (argv) => { const { id, logFile, force, silent, ignoreError, hubId, revertLog, repoId, folderId, ignoreSchemaValidation } = argv; 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) { console.log('ID of content item is specified, ignoring repository ID'); } if (id && facet) { console.log('Please specify either a facet or an ID - not both.'); return; } if (repoId && folderId) { console.log('Folder is specified, ignoring repository ID'); } if (allContent) { console.log('No filter was given, archiving all content'); } const { contentItems, missingContent } = await (0, exports.getContentItems)({ client, id, hubId, repoId, folderId, revertLog, facet }); await (0, exports.processItems)({ contentItems, force, silent, logFile, allContent, missingContent, ignoreError, ignoreSchemaValidation }); }; exports.handler = handler;