UNPKG

@cto.ai/ops

Version:

💻 CTO.ai Ops - The CLI built for Teams 🚀

173 lines (172 loc) 7.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const base_1 = tslib_1.__importStar(require("../base")); const sdk_1 = require("@cto.ai/sdk"); const utils_1 = require("../utils"); const env_1 = require("../constants/env"); const CustomErrors_1 = require("../errors/CustomErrors"); const ErrorTemplate_1 = require("../errors/ErrorTemplate"); class Remove extends base_1.default { constructor() { super(...arguments); this.validateOpNameAndVersion = (inputs) => { let { op } = inputs; if (op.charAt(0) === '@' && op.includes('/')) { const [field1, field2] = op.split('/'); let opTeamName = field1 ? field1.substring(1) : inputs.config.team.name; const [opName, opVersion] = field2 ? field2.split(':') : [undefined, undefined]; if (!opName || !opVersion) { throw new CustomErrors_1.InvalidRemoveOpFormat(); } return Object.assign(Object.assign({}, inputs), { opTeamName, opName, opVersion }); } else { const [opName, opVersion] = op.split(':'); if (!opName || !opVersion) { throw new CustomErrors_1.InvalidRemoveOpFormat(); } const opTeamName = inputs.config.team.name; return Object.assign(Object.assign({}, inputs), { opTeamName, opName, opVersion }); } }; this.getApiOpsOrWorkflows = async (inputs) => { try { const { opName, opVersion, opTeamName, config: { tokens: { accessToken }, }, } = inputs; const { data: opOrWorkflow, } = await this.services.api .find(`/private/teams/${opTeamName}/ops/${opName}/versions/${opVersion}`, { headers: { Authorization: accessToken, }, }) .catch(err => { if (err.error[0].code === 404) { throw new CustomErrors_1.NoOpsFound(`${opName}:${opVersion}`, opTeamName); } throw err; }); return Object.assign(Object.assign({}, inputs), { opOrWorkflow }); } catch (err) { if (err instanceof ErrorTemplate_1.ErrorTemplate) { throw err; } this.debug('%O', err); throw new CustomErrors_1.APIError(err); } }; this.promptDeleteDescription = async (inputs) => { const { opOrWorkflow } = inputs; if (opOrWorkflow.teamName !== inputs.config.team.name) { return inputs; } const { deleteDescription } = await sdk_1.ux.prompt({ type: 'input', name: 'deleteDescription', message: `\nProvide a description for why ${opOrWorkflow.name}:${opOrWorkflow.version} is being deleted ${sdk_1.ux.colors.reset.green('→')}\n\n ${sdk_1.ux.colors.white('✍️ Description:')}`, afterMessage: sdk_1.ux.colors.reset.green('✓'), validate: input => { if (input === '') { return 'You need to provide a delete description of your op before continuing'; } if (input.length > 255) { return 'Sorry, the maximum length for a delete description is 255 characters'; } return true; }, }); return Object.assign(Object.assign({}, inputs), { deleteDescription }); }; this.confirmRemove = async (inputs) => { const { opOrWorkflow: { name, teamName, version }, } = inputs; const { confirmRemove } = await sdk_1.ux.prompt({ type: 'confirm', name: 'confirmRemove', suffix: false, message: `Are you sure you want to remove @${teamName}/${name}:${version}?`, }); return Object.assign(Object.assign({}, inputs), { confirmRemove }); }; this.removeApiOpOrWorkflow = async (inputs) => { try { if (!inputs.confirmRemove) return inputs; const { opOrWorkflow: { teamName, name, version }, deleteDescription, config: { team: { name: ownTeamName }, tokens: { accessToken }, }, } = inputs; this.log(`\n 🗑 Removing op ${name}:${version}...`); if (teamName === inputs.config.team.name) { await this.services.api.remove(`/private/teams/${teamName}/ops/${name}/versions`, version, { query: { deleteDescription }, headers: { Authorization: accessToken }, }); return inputs; } // remove added op await this.services.api.remove(`/private/teams/${ownTeamName}/ops/refs`, null, { query: { opTeamName: teamName, opName: name, versionName: version }, headers: { Authorization: accessToken }, }); return inputs; } catch (err) { this.debug('%O', err); throw new CustomErrors_1.CannotDeleteOp(err); } }; this.logMessage = (inputs) => { const { opOrWorkflow: { version, name, teamName }, confirmRemove, } = inputs; if (!confirmRemove) return inputs; this.log(`\n ⚡️ ${sdk_1.ux.colors.bold(`@${teamName}/${name}:${version}`)} has been successfully ${sdk_1.ux.colors.green('removed')} from your ops!`); this.log(`\n To publish again run: ${sdk_1.ux.colors.green('$')} ${sdk_1.ux.colors.dim('ops publish <path>')}\n`); return inputs; }; this.sendAnalytics = async (inputs) => { const { opOrWorkflow: { id, name, description }, config: { user: { email, username }, team: { id: teamId }, tokens: { accessToken }, }, } = inputs; this.services.analytics.track({ userId: email, teamId, cliEvent: 'Ops CLI Remove', event: 'Ops CLI Remove', properties: { email, username, id, name, description, image: `${env_1.OPS_REGISTRY_HOST}/${name}`, }, }, accessToken); return inputs; }; } async run() { const { args: { op }, } = this.parse(Remove); const { config } = this.state; try { await this.isLoggedIn(); const removePipeline = utils_1.asyncPipe(this.validateOpNameAndVersion, this.getApiOpsOrWorkflows, this.promptDeleteDescription, this.confirmRemove, this.removeApiOpOrWorkflow, this.sendAnalytics, this.logMessage); await removePipeline({ op, config }); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: config.tokens.accessToken, }); } } } exports.default = Remove; Remove.description = 'Remove an Op from your team.'; Remove.args = [ { name: 'op', description: 'The name and version of the command or workflow you want to remove. E.g. my-command:0.1.0', required: true, }, ]; Remove.flags = { help: base_1.flags.help({ char: 'h' }), };