UNPKG

@amplience/dc-cli

Version:
94 lines (93 loc) 4.22 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.processWebhooks = exports.builder = exports.coerceLog = exports.LOG_FILENAME = exports.desc = exports.command = void 0; const log_helpers_1 = require("../../common/log-helpers"); const dynamic_content_client_factory_1 = __importDefault(require("../../services/dynamic-content-client-factory")); const export_service_1 = require("../../services/export.service"); const question_helpers_1 = require("../../common/question-helpers"); const progress_bar_1 = require("../../common/progress-bar/progress-bar"); const get_webhooks_by_ids_1 = require("../../common/webhooks/get-webhooks-by-ids"); const get_all_webhook_1 = require("../../common/webhooks/get-all-webhook"); exports.command = 'delete [id]'; exports.desc = 'Delete Webhook'; const LOG_FILENAME = (platform = process.platform) => (0, log_helpers_1.getDefaultLogPath)('webhook', 'delete', platform); exports.LOG_FILENAME = LOG_FILENAME; const coerceLog = (logFile) => (0, log_helpers_1.createLog)(logFile, 'Webhook Delete Log'); exports.coerceLog = coerceLog; const builder = (yargs) => { yargs .positional('id', { type: 'string', describe: 'The ID of the webhook to be deleted. If id is not provided, this command will delete ALL webhooks in the hub.' }) .alias('f', 'force') .option('f', { type: 'boolean', boolean: true, describe: 'If present, there will be no confirmation prompt before deleting the found webhooks.' }) .option('logFile', { type: 'string', default: exports.LOG_FILENAME, describe: 'Path to a log file to write to.', coerce: exports.coerceLog }); }; exports.builder = builder; const processWebhooks = async (webhooksToDelete, log) => { const failedWebhooks = []; const progress = (0, progress_bar_1.progressBar)(webhooksToDelete.length, 0, { title: `Deleting ${webhooksToDelete.length} webhook/s.` }); for (const webhook of webhooksToDelete) { try { await webhook.related.delete(); log.addComment(`Successfully deleted "${webhook.label}"`); progress.increment(); } catch (e) { failedWebhooks.push(webhook); log.addComment(`Failed to delete ${webhook.label}: ${e.toString()}`); progress.increment(); } } progress.stop(); return { failedWebhooks }; }; exports.processWebhooks = processWebhooks; const handler = async (argv) => { const { id, logFile, force } = argv; const log = logFile.open(); const client = (0, dynamic_content_client_factory_1.default)(argv); const allWebhooks = !id; const hub = await client.hubs.get(argv.hubId); let ids = []; if (id) { ids = Array.isArray(id) ? id : [id]; } const webhooksToDelete = ids.length > 0 ? await (0, get_webhooks_by_ids_1.getWebhooksByIds)(hub, ids) : await (0, get_all_webhook_1.getAllWebhooks)(hub); if (webhooksToDelete.length === 0) { (0, export_service_1.nothingExportedExit)(log, 'No webhooks to delete from this hub, exiting.'); return; } if (!force) { const baseMessage = 'This action cannot be undone. Are you sure you want to continue? (Y/n)\n'; const yes = await (0, question_helpers_1.asyncQuestion)(allWebhooks ? `Providing no ID/s will permanently delete ALL webhooks! ${baseMessage}` : `${webhooksToDelete.length} webhook/s will be permanently deleted. ${baseMessage}`); if (!yes) { return; } } log.addComment(`Deleting ${webhooksToDelete.length} webhook/s.`); const { failedWebhooks } = await (0, exports.processWebhooks)(webhooksToDelete, log); const failedWebhooksMessage = failedWebhooks.length ? `with ${failedWebhooks.length} failed webhooks - check logs for details` : ``; log.appendLine(`Webhooks delete complete ${failedWebhooksMessage}`); await log.close(); }; exports.handler = handler;