UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

111 lines 4.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const schema_1 = require("@sprucelabs/schema"); const SpruceError_1 = __importDefault(require("../errors/SpruceError")); const commander_utility_1 = __importDefault(require("../utilities/commander.utility")); const ui_utility_1 = __importDefault(require("../utilities/ui.utility")); const feature_utilities_1 = __importDefault(require("./feature.utilities")); class FeatureCommandAttacher { program; ui; actionExecuter; pkg; constructor(options) { const { program, ui: term, actionExecuter, pkgService } = options; this.program = program; this.ui = term; this.pkg = pkgService; this.actionExecuter = actionExecuter; } async attachFeature(feature) { const actionCodes = await feature.getAvailableActionCodes(); for (const actionCode of actionCodes) { this.attachCode(actionCode, feature); } } attachCode(code, feature) { let commandStr = feature_utilities_1.default.generateCommand(feature.code, code); const action = this.actionExecuter.Action(feature.code, code); const aliases = action.commandAliases ? [...action.commandAliases] : []; if (aliases.length > 0) { commandStr = aliases.shift(); } let command = this.program.command(commandStr); if (aliases.length > 0) { command = command.aliases(aliases); } command = command.action(async (...args) => { this.clearAndRenderMasthead(action); const startTime = new Date().getTime(); const options = commander_utility_1.default.mapIncomingToOptions(...args, feature.optionsSchema ?? action.optionsSchema); const results = await action.execute({ ...options, }); const endTime = new Date().getTime(); const totalTime = endTime - startTime; this.clearAndRenderResults({ featureCode: feature.code, actionCode: code, totalTime, results, action, }); return results; }); const description = action.optionsSchema?.description; if (description) { command = command.description(description); } const schema = action.optionsSchema; if (schema) { this.attachOptions(command, schema); } } clearAndRenderResults(options) { const { featureCode, actionCode, results, totalTime, action } = options; this.ui.stopLoading(); this.ui.clear(); this.ui.renderActionSummary({ namespace: this.pkg.doesExist() ? this.pkg.getSkillNamespace() : undefined, featureCode, actionCode, totalTime, action, ...results, headline: results.headline ?? `${actionCode} finished!`, }); } clearAndRenderMasthead(action) { ui_utility_1.default.renderActionMastHead(this.ui, action); } attachOptions(command, schema) { const entity = schema_1.SchemaEntityFactory.Entity(schema); let theProgram = command; const fields = entity.getNamedFields(); const aliases = feature_utilities_1.default.generateOptionAliases(schema); fields.forEach(({ field, name }) => { try { theProgram = theProgram.option(aliases[name], field.hint, field.definition.defaultValue ? `${field.definition.defaultValue}` : undefined); } catch (err) { throw new SpruceError_1.default({ //@ts-ignore code: 'FAILED_TO_ATTACH_COMMAND', fieldName: name, id: entity.schemaId, originalError: err, friendlyMessage: `Could not attach option ${aliases[name]} from ${entity.schemaId}.${name} to the command`, }); } }); } } exports.default = FeatureCommandAttacher; //# sourceMappingURL=FeatureCommandAttacher.js.map