monitme
Version:
Powerful task for developers
43 lines (34 loc) • 1.39 kB
JavaScript
const argv = process.argv;
const cli = {
command: argv[2] ? (argv[2].startsWith('-') ? '' : argv[2]) : '',
args: argv.filter((v, i) => i > 2 && !v.startsWith('-')),
options: argv.filter((v, i) => i > 1 && v.startsWith('-'))
};
const entries = [];
function Command(name, description) {
this.name = name;
this.options = {};
entries.push({ command: name, description, options: [] });
}
Command.prototype.option = function(name, description) {
var names = name.split(',').map(x => x.trim());
entries.filter(x => x.name = this.name).forEach((_, index) => entries[index].options.push({ name, description }));
var ok = cli.options.some(v => names.indexOf(v) >= 0);
names.forEach(name => this.options[name.replace('--', '').replace('-', '')] = ok);
return this;
}
/**
* @param action {((args: string[], options: any) => void)}
*/
Command.prototype.action = function(action) {
if (cli.command == this.name && !command.RAN) {
action.call(this, cli.args, this.options);
command.RAN = true;
}
}
const command = (name, description) => new Command(name, description);
command.log = (...args) => console.log(args.join(' '));
command.chalk = require('chalk').default;
command.command = (...args) => new command(...args);
command.rl = require('readline-sync');
module.exports = command;