UNPKG

@cto.ai/ops

Version:

💻 CTO.ai - The CLI built for Teams 🚀

107 lines (106 loc) • 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); /** * Author: Brett Campbell (brett@hackcapital.com) * Date: Saturday, 6th April 2019 10:39:58 pm * Last Modified By: Brett Campbell (brett@hackcapital.com) * Last Modified Time: Saturday, 6th April 2019 10:40:00 pm * * DESCRIPTION * */ const cli_sdk_1 = require("@cto.ai/cli-sdk"); const path = tslib_1.__importStar(require("path")); const base_1 = tslib_1.__importStar(require("./../base")); const opConfig_1 = require("./../constants/opConfig"); const CustomErrors_1 = require("./../errors/CustomErrors"); const Docker_1 = require("../types/Docker"); class Build extends base_1.default { constructor() { super(...arguments); this.resolvePath = (opPath) => { return opPath ? path.resolve(process.cwd(), opPath) : process.cwd(); }; this.selectOpToBuild = async (ops) => { if (ops.length === 1) { return ops; } const { opsToBuild } = await cli_sdk_1.ux.prompt({ type: 'checkbox', name: 'opsToBuild', message: `\n Which workflows would you like to build ${cli_sdk_1.ux.colors.reset.green('→')}`, choices: ops.map(op => { return { value: op, name: `${op.name} - ${op.description}`, }; }), validate: input => input.length > 0, }); return opsToBuild; }; this.executeOpService = async (commands, opPath, config, options) => { await this.services.opService.opsBuildLoop(commands, opPath, config, options); return commands; }; } async run() { const { flags, args: { path }, } = this.parse(Build); try { await this.isLoggedIn(); const options = Object.assign({ nocache: flags.nocache }, Docker_1.PREFER_AMD64); const opPath = this.resolvePath(path); const inputManifest = await this.services.opService.getOpsFromFileSystem(opPath); const ops = [ ...inputManifest.ops, ...inputManifest.pipelines, ...inputManifest.services, ]; let opsToBuild; if (flags.ops) { opsToBuild = ops.filter(op => { return flags.ops.includes(op.name); }); } else { opsToBuild = await this.selectOpToBuild(ops); } if (!opsToBuild.length) { throw new CustomErrors_1.NoOpsToBuildFound(); } const opsConvertedToCommands = await this.services.opService.convertOpsToCommands(opsToBuild, this.state.config, opPath); const pipelineOps = opsConvertedToCommands.filter((op) => op.type === opConfig_1.JOB_TYPE); if (pipelineOps.length) { await this.executeOpService(pipelineOps, opPath, this.state.config, options); } const otherOps = opsConvertedToCommands.filter((op) => op.type !== opConfig_1.JOB_TYPE); if (otherOps.length) { await this.executeOpService(otherOps, opPath, this.state.config, Object.assign(Object.assign({}, options), Docker_1.PREFER_AMD64)); } return opsConvertedToCommands; } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: this.accessToken }); } return []; } } exports.default = Build; Build.description = 'Build your workflow for sharing.'; Build.flags = { help: base_1.flags.help({ char: 'h' }), nocache: base_1.flags.boolean({ default: false, description: 'Do not use cache when building the image', }), ops: base_1.flags.string({ description: 'List of workflows from ops.yml you want to build. example: \n ops build ./ops.yml --ops commandName serviceName pipelineName', helpValue: 'workflows', multiple: true, }), }; Build.args = [ { name: 'path', description: 'Path to the workflow you want to build.' }, ];