UNPKG

1.68 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const command_1 = require("@anycli/command");
4const cli_ux_1 = require("cli-ux");
5const engine_1 = require("./engine");
6class Command extends command_1.default {
7 constructor() {
8 super(...arguments);
9 this.engine = new engine_1.default();
10 }
11 async run() {
12 await this.engine.load(this.config);
13 let id = this.argv[0];
14 if (id === '-h' || this.argv.includes('--help'))
15 return this.showHelp();
16 if (!id)
17 id = 'help';
18 await this.engine.runHook('init', { id });
19 const cachedCommand = this.engine.findCommand(id);
20 if (!cachedCommand)
21 return this.commandNotFound(id);
22 this.debug('found command', cachedCommand.id);
23 const command = await cachedCommand.load();
24 this.debug('loaded command', command.id);
25 await command.run(this.argv.slice(1), { config: this.config });
26 }
27 async commandNotFound(id) {
28 await this.engine.runHook('command_not_found', { id });
29 throw new Error(`command not found: ${id}`);
30 }
31 showHelp() {
32 let id = this.argv.find(a => !a.startsWith('-'));
33 const HHelp = require('@anycli/plugin-help').default;
34 let help = new HHelp(this.config);
35 if (!id) {
36 let rootHelp = help.root();
37 cli_ux_1.default.info(rootHelp);
38 }
39 else {
40 let command = this.engine.findCommand(id, true);
41 let commandHelp = help.command(command);
42 cli_ux_1.default.info(commandHelp);
43 }
44 }
45}
46Command.type = 'engine';
47exports.default = Command;