UNPKG

7.82 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const base_1 = tslib_1.__importStar(require("../base"));
5const sdk_1 = require("@cto.ai/sdk");
6const utils_1 = require("../utils");
7const env_1 = require("../constants/env");
8const CustomErrors_1 = require("../errors/CustomErrors");
9const ErrorTemplate_1 = require("../errors/ErrorTemplate");
10class Remove extends base_1.default {
11 constructor() {
12 super(...arguments);
13 this.validateOpNameAndVersion = (inputs) => {
14 let { op } = inputs;
15 if (op.charAt(0) === '@' && op.includes('/')) {
16 const [field1, field2] = op.split('/');
17 let opTeamName = field1 ? field1.substring(1) : inputs.config.team.name;
18 const [opName, opVersion] = field2
19 ? field2.split(':')
20 : [undefined, undefined];
21 if (!opName || !opVersion) {
22 throw new CustomErrors_1.InvalidRemoveOpFormat();
23 }
24 return Object.assign(Object.assign({}, inputs), { opTeamName, opName, opVersion });
25 }
26 else {
27 const [opName, opVersion] = op.split(':');
28 if (!opName || !opVersion) {
29 throw new CustomErrors_1.InvalidRemoveOpFormat();
30 }
31 const opTeamName = inputs.config.team.name;
32 return Object.assign(Object.assign({}, inputs), { opTeamName, opName, opVersion });
33 }
34 };
35 this.getApiOpsOrWorkflows = async (inputs) => {
36 try {
37 const { opName, opVersion, opTeamName, config: { tokens: { accessToken }, }, } = inputs;
38 const { data: opOrWorkflow, } = await this.services.api
39 .find(`/private/teams/${opTeamName}/ops/${opName}/versions/${opVersion}`, {
40 headers: {
41 Authorization: accessToken,
42 },
43 })
44 .catch(err => {
45 if (err.error[0].code === 404) {
46 throw new CustomErrors_1.NoOpsFound(`${opName}:${opVersion}`, opTeamName);
47 }
48 throw err;
49 });
50 return Object.assign(Object.assign({}, inputs), { opOrWorkflow });
51 }
52 catch (err) {
53 if (err instanceof ErrorTemplate_1.ErrorTemplate) {
54 throw err;
55 }
56 this.debug('%O', err);
57 throw new CustomErrors_1.APIError(err);
58 }
59 };
60 this.promptDeleteDescription = async (inputs) => {
61 const { opOrWorkflow } = inputs;
62 if (opOrWorkflow.teamName !== inputs.config.team.name) {
63 return inputs;
64 }
65 const { deleteDescription } = await sdk_1.ux.prompt({
66 type: 'input',
67 name: 'deleteDescription',
68 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:')}`,
69 afterMessage: sdk_1.ux.colors.reset.green('✓'),
70 validate: input => {
71 if (input === '') {
72 return 'You need to provide a delete description of your op before continuing';
73 }
74 if (input.length > 255) {
75 return 'Sorry, the maximum length for a delete description is 255 characters';
76 }
77 return true;
78 },
79 });
80 return Object.assign(Object.assign({}, inputs), { deleteDescription });
81 };
82 this.confirmRemove = async (inputs) => {
83 const { opOrWorkflow: { name, teamName, version }, } = inputs;
84 const { confirmRemove } = await sdk_1.ux.prompt({
85 type: 'confirm',
86 name: 'confirmRemove',
87 suffix: false,
88 message: `Are you sure you want to remove @${teamName}/${name}:${version}?`,
89 });
90 return Object.assign(Object.assign({}, inputs), { confirmRemove });
91 };
92 this.removeApiOpOrWorkflow = async (inputs) => {
93 try {
94 if (!inputs.confirmRemove)
95 return inputs;
96 const { opOrWorkflow: { teamName, name, version }, deleteDescription, config: { team: { name: ownTeamName }, tokens: { accessToken }, }, } = inputs;
97 this.log(`\n 🗑 Removing op ${name}:${version}...`);
98 if (teamName === inputs.config.team.name) {
99 await this.services.api.remove(`/private/teams/${teamName}/ops/${name}/versions`, version, {
100 query: { deleteDescription },
101 headers: { Authorization: accessToken },
102 });
103 return inputs;
104 }
105 // remove added op
106 await this.services.api.remove(`/private/teams/${ownTeamName}/ops/refs`, null, {
107 query: { opTeamName: teamName, opName: name, versionName: version },
108 headers: { Authorization: accessToken },
109 });
110 return inputs;
111 }
112 catch (err) {
113 this.debug('%O', err);
114 throw new CustomErrors_1.CannotDeleteOp(err);
115 }
116 };
117 this.logMessage = (inputs) => {
118 const { opOrWorkflow: { version, name, teamName }, confirmRemove, } = inputs;
119 if (!confirmRemove)
120 return inputs;
121 this.log(`\n ⚡️ ${sdk_1.ux.colors.bold(`@${teamName}/${name}:${version}`)} has been successfully ${sdk_1.ux.colors.green('removed')} from your ops!`);
122 this.log(`\n To publish again run: ${sdk_1.ux.colors.green('$')} ${sdk_1.ux.colors.dim('ops publish <path>')}\n`);
123 return inputs;
124 };
125 this.sendAnalytics = async (inputs) => {
126 const { opOrWorkflow: { id, name, description }, config: { user: { email, username }, team: { id: teamId }, tokens: { accessToken }, }, } = inputs;
127 this.services.analytics.track({
128 userId: email,
129 teamId,
130 cliEvent: 'Ops CLI Remove',
131 event: 'Ops CLI Remove',
132 properties: {
133 email,
134 username,
135 id,
136 name,
137 description,
138 image: `${env_1.OPS_REGISTRY_HOST}/${name}`,
139 },
140 }, accessToken);
141 return inputs;
142 };
143 }
144 async run() {
145 const { args: { op }, } = this.parse(Remove);
146 const { config } = this.state;
147 try {
148 await this.isLoggedIn();
149 const removePipeline = utils_1.asyncPipe(this.validateOpNameAndVersion, this.getApiOpsOrWorkflows, this.promptDeleteDescription, this.confirmRemove, this.removeApiOpOrWorkflow, this.sendAnalytics, this.logMessage);
150 await removePipeline({ op, config });
151 }
152 catch (err) {
153 this.debug('%O', err);
154 this.config.runHook('error', {
155 err,
156 accessToken: config.tokens.accessToken,
157 });
158 }
159 }
160}
161exports.default = Remove;
162Remove.description = 'Remove an Op from your team.';
163Remove.args = [
164 {
165 name: 'op',
166 description: 'The name and version of the command or workflow you want to remove. E.g. my-command:0.1.0',
167 required: true,
168 },
169];
170Remove.flags = {
171 help: base_1.flags.help({ char: 'h' }),
172};