UNPKG

3.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4/**
5 * Author: Brett Campbell (brett@hackcapital.com)
6 * Date: Saturday, 6th April 2019 10:39:58 pm
7 * Last Modified By: Brett Campbell (brett@hackcapital.com)
8 * Last Modified Time: Saturday, 6th April 2019 10:40:00 pm
9 *
10 * DESCRIPTION
11 *
12 */
13const sdk_1 = require("@cto.ai/sdk");
14const fs = tslib_1.__importStar(require("fs-extra"));
15const path = tslib_1.__importStar(require("path"));
16const base_1 = tslib_1.__importStar(require("../base"));
17const opConfig_1 = require("../constants/opConfig");
18const CustomErrors_1 = require("../errors/CustomErrors");
19const utils_1 = require("../utils");
20class 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({}, 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({}, inputs, { ops });
46 };
47 this.selectOpToBuild = async (inputs) => {
48 const { ops } = inputs;
49 if (ops.length === 1) {
50 return 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({}, 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}
85Build.description = 'Build your op for sharing.';
86Build.flags = {
87 help: base_1.flags.help({ char: 'h' }),
88};
89Build.args = [
90 { name: 'path', description: 'Path to the op you want to build.' },
91];
92exports.default = Build;