UNPKG

@triviality/core

Version:
34 lines (28 loc) 980 B
import { ConsoleCommand } from './ConsoleCommand'; import { ConsoleInput } from './ConsoleInput'; import { ConsoleOutput } from './ConsoleOutput'; import { ProcessInput } from './ProcessInput'; import { ProcessOutput } from './ProcessOutput'; export class ConsoleService { private commandMap = new Map<string, ConsoleCommand>(); constructor(commands: ConsoleCommand[], private input: ConsoleInput = new ProcessInput(), private output: ConsoleOutput = new ProcessOutput()) { commands.forEach((command: any) => { this.commandMap.set(command.name(), command); }); } public async handle() { const name = this.input.getArg(0, ''); if (name.trim() === '') { this.output.info('No command given'); return; } const command = this.commandMap.get(name); if (!command) { this.output.error(`Missing command for ${name}`); return; } await command.execute(this.input, this.output); } }