1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.Dispatcher = undefined;
|
7 |
|
8 | require('cli-engine-config');
|
9 |
|
10 | require('cli-engine-command');
|
11 |
|
12 | var _path = require('path');
|
13 |
|
14 | var _path2 = _interopRequireDefault(_path);
|
15 |
|
16 | var _util = require('./util');
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | const debug = require('debug')('cli:dispatcher');
|
21 |
|
22 | class CommandManagerBase {
|
23 | constructor(config) {
|
24 | this.config = config;
|
25 | }
|
26 | async findCommand(id) {
|
27 | return null;
|
28 | }
|
29 |
|
30 | async listTopics() {
|
31 | return [];
|
32 | }
|
33 |
|
34 | require(p) {
|
35 | return (0, _util.undefault)(require(p));
|
36 | }
|
37 | }
|
38 |
|
39 | class BuiltinCommandManager extends CommandManagerBase {
|
40 | async findCommand(id) {
|
41 | const builtins = {
|
42 | version: 'version',
|
43 | help: 'newhelp'
|
44 | };
|
45 |
|
46 | let p = builtins[id];
|
47 | if (p) {
|
48 | p = _path2.default.join(__dirname, 'commands', p);
|
49 | return this.require(p);
|
50 | }
|
51 | }
|
52 | }
|
53 |
|
54 | class CLICommandManager extends CommandManagerBase {
|
55 | async findCommand(id) {
|
56 | let root = this.config.commandsDir;
|
57 | if (!root) return;
|
58 | let p;
|
59 | try {
|
60 | debug(`finding ${id} command`);
|
61 | p = require.resolve(_path2.default.join(root, ...id.split(':')));
|
62 | } catch (err) {
|
63 | if (err.code !== 'MODULE_NOT_FOUND') throw err;
|
64 | }
|
65 | if (p) return this.require(p);
|
66 | }
|
67 | }
|
68 |
|
69 | class PluginCommandManager extends CommandManagerBase {
|
70 | async findCommand(id) {
|
71 | const { default: Plugins } = require('./plugins');
|
72 | let plugins = new Plugins(this.config);
|
73 | await plugins.load();
|
74 | let Command = await plugins.findCommand(id || this.config.defaultCommand || 'help');
|
75 | if (!Command) return;
|
76 | Command.plugin = await plugins.findPluginWithCommand(Command.id);
|
77 | return Command;
|
78 | }
|
79 | }
|
80 |
|
81 | class Dispatcher {
|
82 |
|
83 | constructor(config) {
|
84 | this.config = config;
|
85 | this.managers = [new CLICommandManager(config), new BuiltinCommandManager(config)];
|
86 | if (this.config.userPlugins) {
|
87 | this.managers.unshift(new PluginCommandManager(config));
|
88 | }
|
89 | }
|
90 |
|
91 | async findCommand(id) {
|
92 | if (!id) return;
|
93 | for (let manager of this.managers) {
|
94 | let Command = await manager.findCommand(id);
|
95 | if (Command) return Command;
|
96 | }
|
97 | }
|
98 |
|
99 | findTopic(id) {
|
100 | return null;
|
101 |
|
102 | }
|
103 |
|
104 | async listTopics() {
|
105 | let arrs = await Promise.all(this.managers.map(m => m.listTopics()));
|
106 | return arrs.reduce((next, res) => res.concat(next), []);
|
107 | }
|
108 |
|
109 | get cmdAskingForHelp() {
|
110 | for (let arg of this.config.argv) {
|
111 | if (['--help', '-h'].includes(arg)) return true;
|
112 | if (arg === '--') return false;
|
113 | }
|
114 | return false;
|
115 | }
|
116 | }
|
117 | exports.Dispatcher = Dispatcher; |
\ | No newline at end of file |