UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

97 lines 3.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bootSkillOptions_schema_1 = __importDefault(require("./../../../.spruce/schemas/spruceCli/v2020_07_22/bootSkillOptions.schema")); const SpruceError_1 = __importDefault(require("../../../errors/SpruceError")); const AbstractAction_1 = __importDefault(require("../../AbstractAction")); class BootAction extends AbstractAction_1.default { optionsSchema = bootSkillOptions_schema_1.default; commandAliases = ['boot']; invocationMessage = 'Booting skill... ⚡️'; onDataHandler; onErrorHandler; async execute(options) { const command = this.Service('command'); let script = 'boot'; this.onDataHandler = options.onData; this.onErrorHandler = options.onError; if (options.local) { script += '.local'; } let runningPromise; let bootPromise = new Promise((resolve, reject) => { runningPromise = this.boot(command, script, resolve, reject); }); const hints = ['Skill booted succesfully!', 'Skill torn down cleanly!']; const meta = { isBooted: false, kill: command.kill.bind(command), pid: command.pid(), promise: runningPromise, bootPromise, }; return new Promise((resolve, reject) => { bootPromise = bootPromise .then(() => { meta.isBooted = true; return { meta, hints }; }) .catch((err) => { reject(err); return err; }); if (!options.shouldReturnImmediately) { void bootPromise.then(() => resolve({ meta, hints })); } else { meta.bootPromise = bootPromise; resolve({ meta, hints, }); } }); } async boot(command, script, resolve, reject) { let isBooted = false; try { const results = await command.execute(`yarn ${script}`, { onData: (data) => { this.onDataHandler?.(data); if (!isBooted && data.search(':: Skill booted') > -1) { isBooted = true; resolve(undefined); } }, onError: (data) => { this.onErrorHandler?.(data); }, }); if (!isBooted) { isBooted = true; resolve(undefined); } return results; } catch (err) { let mappedErr = err; if (mappedErr.message.search(/Error: cannot find module.*?build\/index/gi) > -1) { mappedErr = new SpruceError_1.default({ code: 'BOOT_ERROR', friendlyMessage: 'You must build your skill before you can boot it!', }); } if (!isBooted) { reject(mappedErr); } else { throw mappedErr; } } return null; } } exports.default = BootAction; //# sourceMappingURL=BootAction.js.map