@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
115 lines • 3.88 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
const feature_utilities_1 = __importDefault(require("./feature.utilities"));
const methodDelegator = {
getAllFuncs(toCheck) {
const props = [];
let obj = toCheck;
do {
props.push(...Object.getOwnPropertyNames(obj));
} while ((obj = Object.getPrototypeOf(obj)));
return props.sort().filter((e, i, arr) => {
return e != arr[i + 1] && typeof toCheck[e] == 'function';
});
},
delegateFunctionCalls(from, to) {
const props = this.getAllFuncs(to);
for (const prop of props) {
//@ts-ignore
if (!from[prop]) {
//@ts-ignore
from[prop] = (...args) => {
//@ts-ignore
return to[prop](...args);
};
}
}
},
};
class OverrideActionDecorator {
blockedCommands;
optionOverrides;
ui;
actionCode;
get invocationMessage() {
return this.childAction.invocationMessage;
}
get commandAliases() {
return this.childAction.commandAliases;
}
childAction;
parent;
get optionsSchema() {
return this.childAction.optionsSchema;
}
getChild() {
return this.childAction;
}
constructor(options) {
const { action, feature, ui, blockedCommands, optionOverrides, actionCode, } = options;
if (!action || !action.execute) {
throw new SpruceError_1.default({
code: 'GENERIC',
friendlyMessage: `${feature.nameReadable} failed to load action.`,
});
}
this.childAction = action;
this.parent = feature;
this.blockedCommands = blockedCommands;
this.optionOverrides = optionOverrides;
this.ui = ui;
this.actionCode = actionCode;
methodDelegator.delegateFunctionCalls(this, action);
}
assertCommandIsNotBlocked() {
const commands = this.getCommands();
for (const commandStr of commands) {
if (this.blockedCommands?.[commandStr]) {
throw new SpruceError_1.default({
code: 'COMMAND_BLOCKED',
command: commandStr,
hint: this.blockedCommands[commandStr],
});
}
}
}
execute = async (optionsArg) => {
this.assertCommandIsNotBlocked();
const options = this.mixinOptionOverrides(optionsArg);
const response = await this.childAction.execute(options);
return response;
};
getCommands() {
return feature_utilities_1.default.generateCommandsIncludingAliases(this.parent.code, this.actionCode, this);
}
mixinOptionOverrides(optionsArgs) {
let { ...options } = optionsArgs;
const commands = this.getCommands();
let namespace;
try {
const pkg = this.parent.Service('pkg');
namespace = pkg.getSkillNamespace();
}
catch {
namespace = 'new skill';
}
for (const commandStr of commands) {
const overrides = this.optionOverrides?.[commandStr];
if (overrides) {
this.ui?.renderLine(`Overrides found in package.json of ${namespace}.`);
this.ui?.renderObject(overrides);
options = {
...options,
...overrides,
};
}
}
return options;
}
}
exports.default = OverrideActionDecorator;
//# sourceMappingURL=OverrideActionDecorator.js.map