UNPKG

@amplience/dc-cli

Version:
137 lines (136 loc) 6.35 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.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 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 progress_bar_1 = require("../../common/progress-bar/progress-bar"); const content_item_unpublishing_service_1 = require("../../common/publishing/content-item-unpublishing-service"); const get_content_items_by_ids_1 = require("../../common/content-item/get-content-items-by-ids"); exports.command = 'unpublish [id]'; exports.desc = 'Unublish Content Items'; const LOG_FILENAME = (platform = process.platform) => (0, log_helpers_1.getDefaultLogPath)('content-item', 'unpublish', platform); exports.LOG_FILENAME = LOG_FILENAME; const coerceLog = (logFile) => (0, log_helpers_1.createLog)(logFile, 'Content Items Unpublish Log'); exports.coerceLog = coerceLog; const builder = (yargs) => { yargs .positional('id', { type: 'string', describe: 'The ID of a content item to be unpublished. If id is not provided, this command will unpublish 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 unpublished.', requiresArg: false }) .option('folderId', { type: 'string', describe: 'The ID of a folder to search items in to be unpublished.', requiresArg: false }) .option('facet', { type: 'string', describe: "Unpublish 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." }) .alias('f', 'force') .option('f', { type: 'boolean', boolean: true, describe: 'If present, there will be no confirmation prompt before unpublishing the found content.' }) .alias('s', 'silent') .option('s', { type: 'boolean', boolean: true, describe: 'If present, no log file will be produced.' }) .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 }); }; exports.builder = builder; const processItems = async ({ contentItems, log }) => { log.appendLine(`Unpublishing ${contentItems.length} items.`); const unpublishingService = new content_item_unpublishing_service_1.ContentItemUnpublishingService(); const contentItemUnpublishJobs = []; const unpublishProgress = (0, progress_bar_1.progressBar)(contentItems.length, 0, { title: 'Unpublishing content items' }); for (const item of contentItems) { try { await unpublishingService.unpublish(item, contentItem => { contentItemUnpublishJobs.push(contentItem); log.addComment(`Initiated unpublish for "${item.label}"`); unpublishProgress.increment(); }); } catch (e) { log.appendLine(`\nFailed to initiate unpublish for ${item.label}: ${e.toString()}`); unpublishProgress.increment(); } } await unpublishingService.onIdle(); unpublishProgress.stop(); }; exports.processItems = processItems; const handler = async (argv) => { const { id, logFile, force, silent, hubId, repoId, folderId } = argv; const log = logFile.open(); const client = (0, dynamic_content_client_factory_1.default)(argv); const facet = (0, facet_1.withOldFilters)(argv.facet, argv); const allContent = !id && !facet && !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, unpublishing all content'); } let ids = []; if (id) { ids = Array.isArray(id) ? id : [id]; } const hub = await client.hubs.get(hubId); const contentItems = ids.length > 0 ? await (0, get_content_items_by_ids_1.getContentByIds)(client, ids) : await (0, fetch_content_1.getContent)(client, hub, facet, { repoId, folderId, status: dc_management_sdk_js_1.Status.ACTIVE, enrichItems: true }); const unpublishableContentItems = contentItems.filter(item => item.publishingStatus !== dc_management_sdk_js_1.ContentItemPublishingStatus.UNPUBLISHED && item.publishingStatus !== dc_management_sdk_js_1.ContentItemPublishingStatus.NONE); if (!unpublishableContentItems.length) { log.appendLine('Nothing found to unpublish, aborting'); return; } const missingContentItems = ids.length > 0 ? Boolean(ids.length !== unpublishableContentItems.length) : false; log.appendLine(`Found ${unpublishableContentItems.length} content items to unpublish\n`); if (!force) { const yes = await (0, confirm_all_content_1.confirmAllContent)('unpublish', 'content items', allContent, missingContentItems); if (!yes) { return; } } await (0, exports.processItems)({ contentItems: unpublishableContentItems, log }); log.appendLine(`Unpublish complete - please manually verify unpublish status`); await log.close(!silent); }; exports.handler = handler;