@wocker/core
Version:
Core of the Wocker
68 lines (67 loc) • 3.25 kB
JavaScript
;
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 });
exports.ApplicationContext = void 0;
const cli_1 = require("@kearisp/cli");
const AsyncStorage_1 = require("./AsyncStorage");
class ApplicationContext {
constructor(module, container) {
this.module = module;
this.container = container;
}
get(typeOrToken) {
const module = this.container.getModule(this.module);
if (!module) {
throw new Error("Module not found");
}
const res = module.get(typeOrToken);
if (!res) {
throw new Error("Instance not found");
}
return res;
}
run(args) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve) => {
AsyncStorage_1.AsyncStorage.run(this.container, () => {
const cli = this.get(cli_1.Cli);
cli.command("").action(() => {
for (const [, module] of this.container.modules) {
for (const [, container] of module.controllers) {
if (!container.description) {
continue;
}
console.info(`${container.description}:`);
const spaceLength = container.commands.reduce((space, route) => {
return route.commandNames.reduce((space, command) => {
return Math.max(space, command.length + 2);
}, space);
}, 0);
for (const route of container.commands) {
if (!route.description) {
continue;
}
for (const commandName of route.commandNames) {
const space = " ".repeat(Math.max(0, spaceLength - commandName.length));
console.info(` ${commandName} ${space} ${route.description}`);
}
}
console.info("");
}
}
});
return resolve(cli.run(args));
});
});
});
}
}
exports.ApplicationContext = ApplicationContext;