UNPKG

@amplience/dc-cli

Version:
160 lines (159 loc) 7.84 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 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 question_helpers_1 = require("../../common/question-helpers"); const content_item_publishing_service_1 = require("../../common/publishing/content-item-publishing-service"); const content_item_publishing_job_service_1 = require("../../common/publishing/content-item-publishing-job-service"); const PublishingJobStatus_1 = require("dc-management-sdk-js/build/main/lib/model/PublishingJobStatus"); const progress_bar_1 = require("../../common/progress-bar/progress-bar"); const get_content_items_by_ids_1 = require("../../common/content-item/get-content-items-by-ids"); const dedupe_content_items_1 = require("../../common/content-item/dedupe-content-items"); exports.command = 'publish [id]'; exports.desc = 'Publish Content Items'; const LOG_FILENAME = (platform = process.platform) => (0, log_helpers_1.getDefaultLogPath)('content-item', 'publish', platform); exports.LOG_FILENAME = LOG_FILENAME; const coerceLog = (logFile) => (0, log_helpers_1.createLog)(logFile, 'Content Items Publish Log'); exports.coerceLog = coerceLog; const builder = (yargs) => { yargs .positional('id', { type: 'string', describe: 'The ID of a content item to be published. If id is not provided, this command will publish 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 published.', requiresArg: false }) .option('folderId', { type: 'string', describe: 'The ID of a folder to search items in to be published.', requiresArg: false }) .option('facet', { type: 'string', describe: "Publish 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 publishing 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 ({ client, contentItems, force, log }) => { const dedupedContentItems = (0, dedupe_content_items_1.dedupeContentItems)(contentItems); log.appendLine(`Publishing ${dedupedContentItems.length} item(s) (ignoring ${contentItems.length - dedupedContentItems.length} duplicate child item(s))`); const publishingService = new content_item_publishing_service_1.ContentItemPublishingService(); const contentItemPublishJobs = []; const publishProgress = (0, progress_bar_1.progressBar)(dedupedContentItems.length, 0, { title: 'Publishing content items' }); for (const item of dedupedContentItems) { try { await publishingService.publish(item, (contentItem, publishingJob) => { contentItemPublishJobs.push([contentItem, publishingJob]); log.addComment(`Initiated publish for "${item.label}"`); publishProgress.increment(); }); } catch (e) { log.appendLine(`\nFailed to initiate publish for ${item.label}: ${e.toString()}`); publishProgress.increment(); } } await publishingService.onIdle(); publishProgress.stop(); const checkPublishJobs = async () => await (0, question_helpers_1.asyncQuestion)('All publishes have been requested, would you like to wait for all publishes to complete? (y/n)'); if (force || (await checkPublishJobs())) { log.appendLine(`Checking publishing state for ${contentItemPublishJobs.length} items.`); const checkPublishProgress = (0, progress_bar_1.progressBar)(contentItemPublishJobs.length, 0, { title: 'Content items publishes complete' }); const publishingJobService = new content_item_publishing_job_service_1.ContentItemPublishingJobService(client); for (const [contentItem, publishingJob] of contentItemPublishJobs) { publishingJobService.check(publishingJob, async (resolvedPublishingJob) => { log.addComment(`Finished checking publish job for ${contentItem.label}`); if (resolvedPublishingJob.state === PublishingJobStatus_1.PublishingJobStatus.FAILED) { log.appendLine(`\nFailed to publish ${contentItem.label}: ${resolvedPublishingJob.publishErrorStatus}`); } checkPublishProgress.increment(); }); } await publishingJobService.onIdle(); checkPublishProgress.stop(); } }; 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, publishing 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 }); if (!contentItems.length) { log.appendLine('Nothing found to publish, aborting'); return; } const missingContentItems = ids.length > 0 ? Boolean(ids.length !== contentItems.length) : false; log.appendLine(`Found ${contentItems.length} content items to publish (including duplicate child items)\n`); if (!force) { const yes = await (0, confirm_all_content_1.confirmAllContent)('publish', 'content items', allContent, missingContentItems); if (!yes) { return; } } await processItems({ client, contentItems, force, log }); log.appendLine(`Publishing complete`); await log.close(!silent); }; exports.handler = handler;