@auttam/easycli
Version:
A quick and easy way of creating cli for your npm package.
112 lines (111 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandCollection = exports.Command = void 0;
const collection_1 = require("./collection");
const config_error_1 = require("../errors/config-error");
const string_1 = require("../utility/string");
const base_config_1 = require("./base-config");
const param_config_1 = require("./param-config");
const option_config_1 = require("./option-config");
class Command extends base_config_1.Config {
constructor(config) {
if (!config.method)
throw new config_error_1.ConfigurationError('Command cannot be created, method name is required', config);
if (!config.name) {
config.name = config.method.endsWith('Command') ? config.method.substr(0, config.method.lastIndexOf('Command')) : config.method;
config.name = string_1.hyphenate(config.name);
}
super({
name: config.name,
propName: config.method,
help: config.help
});
this.params = new param_config_1.ParamCollection();
if (config.params) {
this.params.mergeByConfigs(config.params);
}
this.options = new option_config_1.OptionCollection();
if (config.options) {
this.options.addList(config.options);
}
}
toCommandConfig() {
return {
name: this.name,
method: this.propName,
help: this.help
};
}
merge(config) {
super.merge(config, { ignoreProps: ['options', 'params'] });
this.name = config.name || this.name;
if (config.params) {
this.params.mergeByConfigs(config.params);
}
if (config.options) {
this.options.addList(config.options);
}
}
}
exports.Command = Command;
class CommandCollection extends collection_1.Collection {
constructor() {
super(...arguments);
this._definedNames = {};
}
add(config) {
if (!config)
throw new config_error_1.ConfigurationError('Command configuration cannot be null or undefined');
if (super.hasKey(config.method)) {
var command = super.get(config.method);
command.merge(config);
super.update(command.propName, command);
}
else {
var command = new Command(config);
super.append(command.propName, command);
}
}
addByCommand(command) {
if (!command)
throw new config_error_1.ConfigurationError('Command cannot be null or undefined');
if (super.hasKey(command.propName)) {
super.update(command.propName, command);
}
else {
super.append(command.propName, command);
}
}
addMethod(name, methodSignature) {
var command = new Command({ method: name });
command.params.initByMethod(methodSignature);
this.addByCommand(command);
}
addList(configs) {
if (!configs || !configs.length)
return;
configs.forEach(config => this.add(config));
}
validate(item) {
if (this._definedNames[item.name] && this._definedNames[item.name] != item.propName) {
throw new config_error_1.ConfigurationError('Unable to add command, command with same name already exists', item);
}
}
itemAdded(item) {
this._definedNames[item.name] = item.propName;
}
getByName(name) {
return this.find(name, 'name');
}
hasMethod(methodName) {
if (!methodName)
return;
return this.hasKey(methodName);
}
verify() {
for (var command of this.getItems()) {
command.params.verify();
}
}
}
exports.CommandCollection = CommandCollection;