@sudoo/coco
Version:
:ocean: A simple command line tool framework
125 lines (124 loc) • 4.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../command/util");
const declare_1 = require("../event/declare");
const declare_2 = require("../panic/declare");
class Coco {
constructor() {
this._rootCommand = null;
this._commands = [];
this._options = [];
this._eventListeners = new Map();
}
static create() {
return new Coco();
}
rootCommand(command) {
this._rootCommand = command;
return this;
}
getRootCommand() {
return this._rootCommand;
}
command(command, ...commands) {
this._commands.push(command, ...commands);
return this;
}
commands(commands) {
this._commands.push(...commands);
return this;
}
globalOption(option) {
this._options.push(option);
return this;
}
getCommands() {
return this._commands;
}
on(event, listener) {
if (this._eventListeners.has(event)) {
const listeners = this._eventListeners.get(event);
this._eventListeners.set(event, [...listeners, listener]);
}
else {
this._eventListeners.set(event, [listener]);
}
return this;
}
remove(event, remove) {
if (this._eventListeners.has(event)) {
const listeners = this._eventListeners.get(event);
const newListeners = [];
for (const current of listeners) {
if (current !== remove) {
newListeners.push(current);
}
}
this._eventListeners.set(event, newListeners);
}
return this;
}
removeAll(event) {
if (this._eventListeners.has(event)) {
this._eventListeners.set(event, []);
}
return this;
}
emit(event, ...args) {
return __awaiter(this, void 0, void 0, function* () {
if (this._eventListeners.has(event)) {
const listeners = this._eventListeners.get(event);
yield Promise.all(listeners.map((listener) => listener(...args)));
}
return;
});
}
go(argv) {
return __awaiter(this, void 0, void 0, function* () {
const args = [...argv];
if (args.length < 2) {
throw declare_2.panic.code(declare_2.ERROR_CODE.INVALID_ARGV, argv.join(' '));
}
const environment = args.shift();
const executer = args.shift();
yield this.emit(declare_1.CORE_EVENT.SYSTEM_CALL_INFO, environment, executer);
if (util_1.isCallingRoot(args)) {
if (this._rootCommand) {
this._rootCommand.execute(args, this._options);
}
return;
}
const matched = this._marchCommand(args);
if (matched.length > 1) {
throw declare_2.panic.code(declare_2.ERROR_CODE.MULTIPLE_COMMAND_MATCHED, matched.map((command) => command.simulate).join(', '));
}
if (matched.length === 0) {
yield this.emit(declare_1.CORE_EVENT.FAILED);
return;
}
const firstMatched = matched[0];
yield firstMatched.execute(args, this._options);
yield this.emit(declare_1.CORE_EVENT.SUCCEED);
return;
});
}
_marchCommand(argv) {
const commands = [];
for (const command of this._commands) {
if (command.match(argv)) {
commands.push(command);
}
}
return commands;
}
}
exports.Coco = Coco;