ask-cli-x
Version:
Alexa Skills Kit (ASK) Command Line Interfaces
168 lines (167 loc) • 7.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractCommand = void 0;
const option_validator_1 = require("./option-validator");
const app_config_1 = __importDefault(require("../model/app-config"));
const messenger_1 = __importDefault(require("../view/messenger"));
const metrics_1 = __importDefault(require("../utils/metrics"));
/**
* Base class for ASK CLI command that provides option parsing, commander configuration and option validation at runtime.
*/
class AbstractCommand {
constructor(optionModel) {
// eslint-disable-next-line global-require
this._optionModel = optionModel || require("./option-model");
}
exit(statusCode) {
messenger_1.default.getInstance().dispose();
process.exit(statusCode || 0);
}
createCommand() {
new messenger_1.default({});
return (commander) => {
try {
// register command name and description
const commanderCopy = commander.command(this.name()).description(this.description());
// register command options
this._registerOptions(commanderCopy);
// register command action
this._registerAction(commanderCopy);
}
catch (err) {
messenger_1.default.getInstance().fatal(err);
this.exit(1);
}
};
}
_registerAction(commander) {
// the result commander's parse, args, contains a Commander object (args[0]), and an array of unrecognized user inputs (args[1])
commander.action(async (...args) => {
const commandInstance = args[0];
const remaining = args[1];
// set Messenger debug preferrance
messenger_1.default.getInstance().doDebug = commandInstance.debug;
// Start metric client
metrics_1.default.startAction(commandInstance._name, "");
messenger_1.default.getInstance().warn(`The 'askx' command and corresponding 'ask-cli-x' npm package are deprecated. To uninstall, run 'npm uninstall -g ask-cli-x'.
To use the latest ASK CLI, run 'npm install -g ask-cli'. Then use 'ask' commands, such as 'ask configure', 'ask new', and 'ask deploy'.
To use Alexa Conversations commands like 'askx compile', run 'npm install -g @alexa/acdl', and then use 'acc compile'.
For details, see https://developer.amazon.com/en-US/docs/alexa/conversations/acdl-set-up-ask-cli.html.`);
try {
this._validateOptions(commandInstance);
/**
* Since this code is ran for every command, we'll just be initiating appConfig here (no files created).
* Only `ask configure` command should have the eligibility to create the ASK config file (which is handled
* in the configure workflow).
*/
if (commandInstance._name !== "configure") {
new app_config_1.default();
}
}
catch (err) {
messenger_1.default.getInstance().error(err);
this.exit(1);
return;
}
// execute handler logic of each command; quit execution
try {
return await this.handle(commandInstance, remaining);
}
catch (error) {
metrics_1.default.sendData(error).then(() => {
this.exit(error ? 1 : 0);
});
}
});
}
_registerOptions(commander) {
const requiredOptions = this.requiredOptions();
if (requiredOptions && requiredOptions.length) {
for (const optionId of requiredOptions) {
commander = this._registerOption(commander, optionId, true);
}
}
const optionalOptions = this.optionalOptions();
if (optionalOptions && optionalOptions.length) {
for (const optionId of optionalOptions) {
commander = this._registerOption(commander, optionId, false);
}
}
return commander;
}
_registerOption(commander, optionId, required) {
const optionModel = this._optionModel[optionId];
// Check if given option name has a model defined. Refer to option-model.json for all available option models
if (!optionModel) {
throw new Error(`Unrecognized option ID: ${optionId}`);
}
return commander.option(AbstractCommand.buildOptionString(optionModel), `${required ? "[REQUIRED]" : "[OPTIONAL]"} ${optionModel.description}`);
}
_validateOptions(cmd) {
const requiredOptions = this.requiredOptions();
if (requiredOptions && requiredOptions.length) {
for (const optionId of requiredOptions) {
this._validateOption(cmd, optionId, true);
}
}
const optionalOptions = this.optionalOptions();
if (optionalOptions && optionalOptions.length) {
for (const optionId of optionalOptions) {
this._validateOption(cmd, optionId, false);
}
}
}
_validateOption(cmd, optionId, required) {
const optionModel = this._optionModel[optionId];
const optionKey = AbstractCommand.parseOptionKey(optionModel.name);
try {
if (required) {
(0, option_validator_1.validateRequiredOption)(cmd, optionKey);
}
if (cmd[optionKey]) {
// Validate string value for options that require string input
if (optionModel.stringInput === "REQUIRED") {
(0, option_validator_1.validateOptionString)(cmd, optionKey);
}
(0, option_validator_1.validateOptionRules)(cmd, optionKey, optionModel.rule);
}
}
catch (err) {
throw `Please provide valid input for option: ${optionModel.name}. ${err}`;
}
}
/**
* Build the usage string for an option
* @param {Object} optionModel
*/
static buildOptionString(optionModel) {
const optionStringArray = [];
if (optionModel.alias) {
optionStringArray.push(`-${optionModel.alias},`);
}
optionStringArray.push(`--${optionModel.name}`);
if (optionModel.stringInput === "REQUIRED") {
optionStringArray.push(`<${optionModel.name}>`);
}
else if (optionModel.stringInput === "OPTIONAL") {
optionStringArray.push(`[${optionModel.name}]`);
}
return optionStringArray.join(" ");
}
/**
* convert option name to option key
* Example: skill-id -> skillId
* @param name
*/
static parseOptionKey(name) {
const arr = name.split("-");
return arr.slice(1).reduce((end, element) => end + element.charAt(0).toUpperCase() + element.slice(1), arr[0]);
}
}
exports.AbstractCommand = AbstractCommand;
module.exports = {
AbstractCommand,
};