UNPKG

7.9 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 opConfig_1 = require("../constants/opConfig");
10class Remove extends base_1.default {
11 constructor() {
12 super(...arguments);
13 // removeTypePrompt = async (
14 // opName: string,
15 // ): Promise<Pick<RemoveInputs, 'opName' | 'removeType'>> => {
16 // const { removeType } = await this.ux.prompt<{ removeType: OpTypes }>({
17 // type: 'list',
18 // name: 'removeType',
19 // message: `Start by selecting either private ${this.ux.colors.multiBlue(
20 // titleCase(COMMAND),
21 // )} or ${this.ux.colors.multiOrange(
22 // titleCase(WORKFLOW),
23 // )} to remove ${this.ux.colors.reset.green('→')}`,
24 // choices: [
25 // {
26 // name: `${this.ux.colors.multiBlue('\u2022')} ${titleCase(COMMAND)}`,
27 // value: COMMAND,
28 // },
29 // {
30 // name: `${this.ux.colors.multiOrange('\u2022')} ${titleCase(
31 // WORKFLOW,
32 // )}`,
33 // value: WORKFLOW,
34 // },
35 // ],
36 // })
37 // return { opName, removeType }
38 // }
39 this.promptFilter = async (opName) => {
40 if (!opName) {
41 const answers = await sdk_1.ux.prompt({
42 type: 'input',
43 name: 'opName',
44 message: `\n Please type in the name of the private op you want to remove ${sdk_1.ux.colors.reset.green('→')}\n ${sdk_1.ux.colors.reset(sdk_1.ux.colors.secondary(`Leave blank to list all private ops you can remove`))} \n\n 🗑 ${sdk_1.ux.colors.reset.white('Name or Description')}`,
45 });
46 opName = answers.opName;
47 }
48 return { opName };
49 };
50 this.getApiOpsOrWorkflows = async (inputs) => {
51 const { opName } = inputs;
52 let apiOps;
53 if (!opName) {
54 try {
55 ;
56 ({ data: apiOps } = await this.services.api.find(`teams/${this.state.config.team.name}/ops`, {
57 headers: {
58 Authorization: this.accessToken,
59 },
60 }));
61 }
62 catch (err) {
63 this.debug('%O', err);
64 throw new CustomErrors_1.APIError(err);
65 }
66 }
67 else {
68 let apiOp;
69 try {
70 ;
71 ({ data: apiOp } = await this.services.api.find(`teams/${this.state.config.team.name}/ops/${opName}`, {
72 headers: {
73 Authorization: this.accessToken,
74 },
75 }));
76 }
77 catch (err) {
78 this.debug('%O', err);
79 throw new CustomErrors_1.APIError(err);
80 }
81 if (!apiOp) {
82 throw new CustomErrors_1.NoOpsFound(opName);
83 }
84 apiOps = [apiOp];
85 }
86 return Object.assign({}, inputs, { apiOps });
87 };
88 this.selectOpOrWorkflow = async (inputs) => {
89 const { apiOps, removeType } = inputs;
90 let opOrWorkflow;
91 if (!apiOps || !apiOps.length) {
92 throw new CustomErrors_1.NoResultsFoundForDeletion(inputs.opName);
93 }
94 if (apiOps.length === 1) {
95 opOrWorkflow = apiOps[0];
96 }
97 else {
98 const { selected } = await sdk_1.ux.prompt({
99 type: 'list',
100 name: 'selected',
101 pageSize: 100,
102 message: `\n 🗑 Which private op would you like to remove?`,
103 choices: apiOps.map(l => {
104 return {
105 name: `${sdk_1.ux.colors.callOutCyan(l.name)} ${sdk_1.ux.colors.white(l.description)} | id: ${sdk_1.ux.colors.white(l.id.toLowerCase())}`,
106 value: l,
107 };
108 }),
109 });
110 opOrWorkflow = selected;
111 }
112 return Object.assign({}, inputs, { opOrWorkflow });
113 };
114 this.confirmRemove = async (inputs) => {
115 const { opOrWorkflow: { name }, } = inputs;
116 const { confirmRemove } = await sdk_1.ux.prompt({
117 type: 'confirm',
118 name: 'confirmRemove',
119 message: `Are you sure you want to remove ${name}?`,
120 });
121 return Object.assign({}, inputs, { confirmRemove });
122 };
123 this.removeApiOpOrWorkflow = async (inputs) => {
124 try {
125 if (!inputs.confirmRemove)
126 return inputs;
127 const { opOrWorkflow: { id, type }, } = inputs;
128 this.log('\n 🗑 Removing from registry...');
129 await this.services.api.remove(opConfig_1.getEndpointFromOpType(type), id, {
130 headers: { Authorization: this.accessToken },
131 });
132 return inputs;
133 }
134 catch (err) {
135 this.debug('%O', err);
136 if (err.error[0].code === 5029) {
137 throw new CustomErrors_1.CannotDeleteOps(err);
138 }
139 throw new CustomErrors_1.APIError(err);
140 }
141 };
142 this.logMessage = (inputs) => {
143 const { opOrWorkflow: { id, name }, confirmRemove, } = inputs;
144 if (!confirmRemove)
145 return inputs;
146 this.log(`\n ⚡️ ${sdk_1.ux.colors.bold(`${name}:${id}`)} has been ${sdk_1.ux.colors.green('removed')} from the registry!`);
147 this.log(`\n To publish again run: ${sdk_1.ux.colors.green('$')} ${sdk_1.ux.colors.dim('ops publish <path>')}\n`);
148 return inputs;
149 };
150 this.sendAnalytics = (user) => (inputs) => {
151 const { email, username } = user;
152 const { opOrWorkflow: { id, name, description }, } = inputs;
153 this.services.analytics.track({
154 userId: email,
155 event: 'Ops CLI Remove',
156 properties: {
157 email,
158 username,
159 // type: removeType,
160 id,
161 name,
162 description,
163 image: `${env_1.OPS_REGISTRY_HOST}/${name}`,
164 },
165 }, this.accessToken);
166 };
167 }
168 async run() {
169 const { args: { opName }, } = this.parse(Remove);
170 try {
171 await this.isLoggedIn();
172 const removePipeline = utils_1.asyncPipe(
173 // this.removeTypePrompt,
174 this.promptFilter, this.getApiOpsOrWorkflows, this.selectOpOrWorkflow, this.confirmRemove, this.removeApiOpOrWorkflow, this.logMessage, this.sendAnalytics(this.user));
175 await removePipeline(opName);
176 }
177 catch (err) {
178 this.debug('%O', err);
179 this.config.runHook('error', { err, accessToken: this.accessToken });
180 }
181 }
182}
183Remove.description = 'Remove an op from a team.';
184Remove.args = [
185 {
186 name: 'opName',
187 description: 'A part of the name or description of the command or workflow you want to remove.',
188 },
189];
190Remove.flags = {
191 help: base_1.flags.help({ char: 'h' }),
192};
193exports.default = Remove;