1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const sdk_1 = require("@cto.ai/sdk");
|
14 | const fs = tslib_1.__importStar(require("fs-extra"));
|
15 | const path = tslib_1.__importStar(require("path"));
|
16 | const base_1 = tslib_1.__importStar(require("../base"));
|
17 | const opConfig_1 = require("../constants/opConfig");
|
18 | const CustomErrors_1 = require("../errors/CustomErrors");
|
19 | const utils_1 = require("../utils");
|
20 | class Build extends base_1.default {
|
21 | constructor() {
|
22 | super(...arguments);
|
23 | this.resolvePath = (inputs) => {
|
24 | let { opPath } = inputs;
|
25 | if (!opPath)
|
26 | throw new CustomErrors_1.MissingRequiredArgument('ops [command]');
|
27 | opPath = path.resolve(process.cwd(), opPath);
|
28 | return Object.assign(Object.assign({}, inputs), { opPath });
|
29 | };
|
30 | this.getOpsFromFileSystem = async (inputs) => {
|
31 | const { opPath } = inputs;
|
32 | const manifest = await fs
|
33 | .readFile(path.join(opPath, opConfig_1.OP_FILE), 'utf8')
|
34 | .catch(err => {
|
35 | this.debug('%O', err);
|
36 | throw new CustomErrors_1.FileNotFoundError(err, opPath, opConfig_1.OP_FILE);
|
37 | });
|
38 | if (!manifest) {
|
39 | throw new CustomErrors_1.NoLocalOpsFound();
|
40 | }
|
41 | const { ops } = utils_1.parseYaml(manifest);
|
42 | if (!ops) {
|
43 | throw new CustomErrors_1.NoLocalOpsFound();
|
44 | }
|
45 | return Object.assign(Object.assign({}, inputs), { ops });
|
46 | };
|
47 | this.selectOpToBuild = async (inputs) => {
|
48 | const { ops } = inputs;
|
49 | if (ops.length === 1) {
|
50 | return Object.assign(Object.assign({}, inputs), { opsToBuild: ops });
|
51 | }
|
52 | const { opsToBuild } = await sdk_1.ux.prompt({
|
53 | type: 'checkbox',
|
54 | name: 'opsToBuild',
|
55 | message: `\n Which ops would you like to build ${sdk_1.ux.colors.reset.green('→')}`,
|
56 | choices: ops.map(op => {
|
57 | return {
|
58 | value: op,
|
59 | name: `${op.name} - ${op.description}`,
|
60 | };
|
61 | }),
|
62 | validate: input => input.length > 0,
|
63 | });
|
64 | return Object.assign(Object.assign({}, inputs), { opsToBuild });
|
65 | };
|
66 | this.executeOpService = async (inputs) => {
|
67 | const { opsToBuild, opPath, config } = inputs;
|
68 | await this.services.opService.opsBuildLoop(opsToBuild, opPath, config);
|
69 | return inputs;
|
70 | };
|
71 | }
|
72 | async run() {
|
73 | const { args: { path }, } = this.parse(Build);
|
74 | try {
|
75 | await this.isLoggedIn();
|
76 | const buildPipeline = utils_1.asyncPipe(this.resolvePath, this.getOpsFromFileSystem, this.selectOpToBuild, this.executeOpService);
|
77 | await buildPipeline({ opPath: path, config: this.state.config });
|
78 | }
|
79 | catch (err) {
|
80 | this.debug('%O', err);
|
81 | this.config.runHook('error', { err, accessToken: this.accessToken });
|
82 | }
|
83 | }
|
84 | }
|
85 | exports.default = Build;
|
86 | Build.description = 'Build your op for sharing.';
|
87 | Build.flags = {
|
88 | help: base_1.flags.help({ char: 'h' }),
|
89 | };
|
90 | Build.args = [
|
91 | { name: 'path', description: 'Path to the op you want to build.' },
|
92 | ];
|