1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const fuzzy_1 = tslib_1.__importDefault(require("fuzzy"));
|
5 | const base_1 = tslib_1.__importStar(require("../base"));
|
6 | const fs = tslib_1.__importStar(require("fs-extra"));
|
7 | const path = tslib_1.__importStar(require("path"));
|
8 | const CustomErrors_1 = require("../errors/CustomErrors");
|
9 | const opConfig_1 = require("../constants/opConfig");
|
10 | const utils_1 = require("../utils");
|
11 | class List extends base_1.default {
|
12 | constructor() {
|
13 | super(...arguments);
|
14 | this.opResults = [];
|
15 | this.getApiOps = async (inputs) => {
|
16 | try {
|
17 | const { data: opResults } = await this.services.api.find(`teams/${this.state.config.team.name}/ops`, {
|
18 | headers: {
|
19 | Authorization: this.accessToken,
|
20 | },
|
21 | });
|
22 | this.opResults = opResults;
|
23 | return Object.assign({}, inputs, { opResults });
|
24 | }
|
25 | catch (err) {
|
26 | this.debug('%0', err);
|
27 | throw new CustomErrors_1.APIError(err);
|
28 | }
|
29 | };
|
30 | this.getLocalOps = async (inputs) => {
|
31 | const localWorkflows = [];
|
32 | try {
|
33 | const manifest = await fs.readFile(path.join(process.cwd(), opConfig_1.OP_FILE), 'utf8');
|
34 | if (!manifest)
|
35 | return inputs;
|
36 | const { workflows = [], ops = [] } = utils_1.parseYaml(manifest);
|
37 | workflows.forEach(workflow => (workflow.local = true));
|
38 | const localCommands = ops.map(ops => (Object.assign({}, ops, { local: true })));
|
39 | return Object.assign({}, inputs, { opResults: [...inputs.opResults, ...localWorkflows, ...localCommands] });
|
40 | }
|
41 | catch (_a) {
|
42 | return Object.assign({}, inputs);
|
43 | }
|
44 | };
|
45 | this.filterOutGlueCodes = (inputs) => {
|
46 | try {
|
47 | const opResults = inputs.opResults.filter(input => input.type !== 'glue_code');
|
48 | this.opResults = opResults;
|
49 | return Object.assign({}, inputs, { opResults });
|
50 | }
|
51 | catch (err) {
|
52 | this.debug('%0', err);
|
53 | }
|
54 | };
|
55 | this.promptOps = async (inputs) => {
|
56 | if (inputs.opResults.length == 0) {
|
57 | this.log(this.ux.colors.whiteBright('ā Sorry you have no ops yet! If you want help with creating one, please go to: https://cto.ai/docs/getting-started'));
|
58 | process.exit();
|
59 | }
|
60 | const { selectedOp } = await this.ux.prompt({
|
61 | type: 'autocomplete',
|
62 | name: 'selectedOp',
|
63 | pageSize: 5,
|
64 | message: `\nSelect a ${this.ux.colors.multiBlue('\u2022Command')} or ${this.ux.colors.multiOrange('\u2022Workflow')} to run ${this.ux.colors.reset.green('ā')}\n${this.ux.colors.reset.dim('š = Public š = Private š„ = Local š Search:')} `,
|
65 | source: this._autocompleteSearch.bind(this),
|
66 | bottomContent: `\n \n${this.ux.colors.white(`Or, run ${this.ux.colors.callOutCyan('ops help')} for usage information.`)}`,
|
67 | });
|
68 | return Object.assign({}, inputs, { selectedOp });
|
69 | };
|
70 | this._autocompleteSearch = async (_, input = '') => {
|
71 | const { list, options } = this._fuzzyFilterParams();
|
72 | const fuzzyResult = fuzzy_1.default.filter(input, list, options);
|
73 | return fuzzyResult.map(result => result.original);
|
74 | };
|
75 | this._fuzzyFilterParams = () => {
|
76 | const list = this.opResults.map(op => {
|
77 | const name = this._formatOpOrWorkflowName(op);
|
78 | return {
|
79 | name: `${name} - ${op.description || op.publishDescription}`,
|
80 | value: op,
|
81 | };
|
82 | });
|
83 | const options = { extract: el => el.name };
|
84 | return { list, options };
|
85 | };
|
86 | this._formatOpOrWorkflowName = (op) => {
|
87 | const name = this.ux.colors.reset.white(op.name);
|
88 | if (op.type === opConfig_1.WORKFLOW_TYPE) {
|
89 | return `${this.ux.colors.reset(this.ux.colors.multiOrange('\u2022'))} ${this._formatOpOrWorkflowEmoji(op)} ${name}`;
|
90 | }
|
91 | else {
|
92 | return `${this.ux.colors.reset(this.ux.colors.multiBlue('\u2022'))} ${this._formatOpOrWorkflowEmoji(op)} ${name}`;
|
93 | }
|
94 | };
|
95 | this._formatOpOrWorkflowEmoji = (opOrWorkflow) => {
|
96 | if (opOrWorkflow.local) {
|
97 | return 'š„ ';
|
98 | }
|
99 | else if (opOrWorkflow.isPublic == false) {
|
100 | return 'š ';
|
101 | }
|
102 | else {
|
103 | return 'š ';
|
104 | }
|
105 | };
|
106 | this.showRunMessage = (inputs) => {
|
107 | const { selectedOp: { name, local }, } = inputs;
|
108 | let runCmd = 'ops run .';
|
109 | if (!local) {
|
110 | runCmd = `ops run ${name}`;
|
111 | }
|
112 | this.log(`\nš» Run ${this.ux.colors.green('$')} ${this.ux.colors.italic.dim(runCmd)} to test your op. ${local
|
113 | ? "(This points to the relative path where the 'ops.yml' file lives)"
|
114 | : ''}\n`);
|
115 | return inputs;
|
116 | };
|
117 | }
|
118 | async run() {
|
119 | try {
|
120 | await this.isLoggedIn();
|
121 | const listPipeline = utils_1.asyncPipe(this.getApiOps, this.getLocalOps, this.filterOutGlueCodes, this.promptOps, this.showRunMessage);
|
122 | await listPipeline({});
|
123 | }
|
124 | catch (err) {
|
125 | this.debug('%0', err);
|
126 | this.config.runHook('error', { err, accessToken: this.accessToken });
|
127 | }
|
128 | }
|
129 | }
|
130 | List.description = 'Lists the ops you have in your team';
|
131 | List.flags = {
|
132 | help: base_1.flags.help({ char: 'h' }),
|
133 | };
|
134 | exports.default = List;
|