1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const command_1 = require("@dxcli/command");
|
4 | const config_1 = require("@dxcli/config");
|
5 | const cli_ux_1 = require("cli-ux");
|
6 | const engine_1 = require("./engine");
|
7 | exports.Engine = engine_1.default;
|
8 | class Command extends command_1.default {
|
9 | async run() {
|
10 | cli_ux_1.default.config.errlog = this.config.errlog;
|
11 | const id = this.argv[0];
|
12 | await this.engine.runHook('init', { id });
|
13 | const cachedCommand = this.engine.findCommand(id);
|
14 | if (!cachedCommand)
|
15 | return this.commandNotFound(id);
|
16 | this.debug('found command', cachedCommand.id);
|
17 | const command = await cachedCommand.load();
|
18 | this.debug('loaded command', command.id);
|
19 | await command.run(this.argv.slice(1), { config: this.config });
|
20 | }
|
21 | async init(argv, opts) {
|
22 | const root = opts.root || module.parent.filename;
|
23 | this.argv = argv;
|
24 | this.config = await config_1.read({ root });
|
25 | this.initDebug(this.config);
|
26 | this.engine = new engine_1.default();
|
27 | this.config.engine = this.engine;
|
28 | await this.engine.load(this.config);
|
29 | }
|
30 | async commandNotFound(id) {
|
31 | await this.engine.runHook('command_not_found', { id });
|
32 | throw new Error(`command not found: ${id}`);
|
33 | }
|
34 | }
|
35 | function run(argv = process.argv.slice(2), opts = {}) {
|
36 | return Command.run(argv, opts);
|
37 | }
|
38 | exports.run = run;
|
39 | exports.default = run;
|