@adonisjs/ace
Version:
Commandline apps framework used by AdonisJs
61 lines (60 loc) • 2.13 kB
JavaScript
;
/*
* @adonisjs/ace
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateCommand = void 0;
const utils_1 = require("@poppinss/utils");
/**
* Validates the command static properties to ensure that all the
* values are correctly defined for a command to be executed.
*/
function validateCommand(command, commandPath) {
if (!command.name) {
throw new utils_1.Exception(`Invalid command"${commandPath ? ` ${commandPath}` : ''}". Make sure the command is exported using the "export default"`);
}
/**
* Ensure command has a name, a boot method and args property
*/
if (!command.commandName || typeof command.boot !== 'function') {
throw new utils_1.Exception(`Invalid command "${command.name}". Make sure to define the static property "commandName"`);
}
/**
* Boot command
*/
command.boot();
/**
* Ensure command has args and flags after the boot method
*/
if (!Array.isArray(command.args) || !Array.isArray(command.flags)) {
throw new utils_1.Exception(`Invalid command "${command.name}". Make sure it extends the BaseCommand`);
}
let optionalArg;
/**
* Validate for optional args and spread args
*/
command.args.forEach((arg, index) => {
/**
* Ensure optional arguments comes after required
* arguments
*/
if (optionalArg && arg.required) {
throw new utils_1.Exception(`Optional argument "${optionalArg.name}" must be after the required argument "${arg.name}"`);
}
/**
* Ensure spread arg is the last arg
*/
if (arg.type === 'spread' && command.args.length > index + 1) {
throw new utils_1.Exception(`Spread argument "${arg.name}" must be at last position`);
}
if (!arg.required) {
optionalArg = arg;
}
});
}
exports.validateCommand = validateCommand;