UNPKG

@wocker/core

Version:
66 lines (65 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ControllerWrapper = void 0; require("reflect-metadata"); const InstanceWrapper_1 = require("./InstanceWrapper"); const Route_1 = require("./Route"); const env_1 = require("../env"); class ControllerWrapper extends InstanceWrapper_1.InstanceWrapper { constructor(module, type) { super(module, type); this.commands = []; this.completions = []; if (!this._type) { return; } this.description = Reflect.getMetadata(env_1.DESCRIPTION_METADATA, this._type) || ""; for (const method of Object.getOwnPropertyNames(this._type.prototype)) { const route = new Route_1.Route(this._type, method); if (route.isCommand) { this.commands.push(route); } else if (route.isCompletion) { this.completions.push(route); } else { // TODO: Log } } } getCompletionCommands(name, command) { const completions = this.completions.filter((route) => { return route.completions.filter((completion) => { return completion.name === name && completion.command === command; }).length > 0; }); if (completions.length > 0) { return completions; } return this.completions.filter((route) => { return route.completions.filter((completion) => { return completion.name === name && !completion.command; }).length > 0; }); } run(route, input) { const args = []; route.args.forEach((arg, index) => { switch (arg.type) { case "param": args[index] = input.argument(arg.name); break; case "option": if (route.designTypes[index] === Array) { args[index] = input.options(arg.name); } else { args[index] = input.option(arg.name); } break; } }); return this.instance[route.method](...args); } } exports.ControllerWrapper = ControllerWrapper;