UNPKG

@dlillyatx/dc-cli

Version:
236 lines (235 loc) 9.37 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.filterContentItems = 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 paginator_1 = __importDefault(require("../../common/dc-management-sdk-js/paginator")); const archive_helpers_1 = require("../../common/archive/archive-helpers"); 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"); exports.command = 'archive [id]'; exports.desc = 'Archive Content Items'; exports.LOG_FILENAME = (platform = process.platform) => log_helpers_1.getDefaultLogPath('content-item', 'archive', platform); exports.coerceLog = (logFile) => log_helpers_1.createLog(logFile, 'Content Items Archive Log'); exports.builder = (yargs) => { yargs .positional('id', { type: 'string', describe: 'The ID of a content item to be archived. If id is not provided, this command will archive 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 archived.', requiresArg: false }) .option('folderId', { type: 'string', describe: 'The ID of a folder to search items in to be archived.', requiresArg: false }) .option('facet', { type: 'string', describe: "Archive 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 unarchived in a previous run of the unarchive command.\nWhen provided, archives all content items listed as UNARCHIVE 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 archiving 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, archive 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 }); }; exports.filterContentItems = async ({ revertLog, facet, contentItems }) => { try { let missingContent = false; if (revertLog != null) { const log = await new archive_log_1.ArchiveLog().loadFromFile(revertLog); const ids = log.getData('UNARCHIVE'); const contentItemsFiltered = contentItems.filter(contentItem => ids.indexOf(contentItem.id || '') != -1); missingContent = contentItems.length !== ids.length; return { contentItems: contentItemsFiltered, missingContent }; } if (facet != null) { const contentItemsFiltered = facet_1.applyFacet(contentItems, facet); return { contentItems: contentItemsFiltered, missingContent }; } return { contentItems, missingContent }; } catch (err) { console.log(err); return { contentItems: [], missingContent: false }; } }; exports.getContentItems = async ({ client, id, hubId, repoId, folderId, revertLog, facet }) => { try { const contentItems = []; if (id != null) { const itemIds = Array.isArray(id) ? id : [id]; const items = await Promise.all(itemIds.map(id => client.contentItems.get(id))); contentItems.push(...items); return { contentItems, missingContent: false }; } const hub = await client.hubs.get(hubId); const repoIds = typeof repoId === 'string' ? [repoId] : repoId || []; const folderIds = typeof folderId === 'string' ? [folderId] : folderId || []; const contentRepositories = await (repoId != null ? Promise.all(repoIds.map(id => client.contentRepositories.get(id))) : paginator_1.default(hub.related.contentRepositories.list)); const folders = folderId != null ? await Promise.all(folderIds.map(id => client.folders.get(id))) : []; folderId != null ? await Promise.all(folders.map(async (source) => { const items = await paginator_1.default(source.related.contentItems.list); contentItems.push(...items.filter(item => item.status == 'ACTIVE')); })) : await Promise.all(contentRepositories.map(async (source) => { const items = await paginator_1.default(source.related.contentItems.list, { status: dc_management_sdk_js_1.Status.ACTIVE }); contentItems.push(...items); })); return ((await exports.filterContentItems({ revertLog, facet, contentItems })) || { contentItems: [], missingContent: false }); } catch (err) { console.log(err); return { contentItems: [], missingContent: false }; } }; exports.processItems = async ({ contentItems, force, silent, logFile, allContent, missingContent, ignoreError }) => { if (contentItems.length == 0) { console.log('Nothing found to archive, aborting.'); return; } console.log('The following content items will be archived:'); contentItems.forEach((contentItem) => { console.log(` ${contentItem.label} (${contentItem.id})`); }); console.log(`Total: ${contentItems.length}`); if (!force) { const yes = await archive_helpers_1.confirmArchive('archive', 'content item', allContent, missingContent); if (!yes) { return; } } const log = logFile.open(); let successCount = 0; for (let i = 0; i < contentItems.length; i++) { try { const deliveryKey = contentItems[i].body._meta.deliveryKey; let args = contentItems[i].id; if (deliveryKey) { contentItems[i].body._meta.deliveryKey = null; contentItems[i] = await contentItems[i].related.update(contentItems[i]); args += ` ${deliveryKey}`; } await contentItems[i].related.archive(); log.addAction('ARCHIVE', `${args}`); successCount++; } catch (e) { log.addComment(`ARCHIVE FAILED: ${contentItems[i].id}`); log.addComment(e.toString()); if (ignoreError) { log.warn(`Failed to archive ${contentItems[i].label} (${contentItems[i].id}), continuing.`, e); } else { log.error(`Failed to archive ${contentItems[i].label} (${contentItems[i].id}), aborting.`, e); break; } } } await log.close(!silent); console.log(`Archived ${successCount} content items.`); }; exports.handler = async (argv) => { const { id, logFile, force, silent, ignoreError, hubId, revertLog, repoId, folderId } = argv; const client = dynamic_content_client_factory_1.default(argv); const facet = facet_1.withOldFilters(argv.facet, 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 exports.getContentItems({ client, id, hubId, repoId, folderId, revertLog, facet }); await exports.processItems({ contentItems, force, silent, logFile, allContent, missingContent, ignoreError }); };