UNPKG

5.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const assync_1 = require("assync");
5const cli_ux_1 = require("cli-ux");
6const ts_lodash_1 = require("ts-lodash");
7const deps_1 = require("./deps");
8const topic_1 = require("./topic");
9class CommandManager {
10 constructor(config) {
11 this.config = config;
12 this.managers = [this.config.plugins];
13 this.debug = require('debug')('cli:command');
14 this.hooks = new deps_1.default.Hooks(this.config);
15 }
16 run(argv) {
17 return tslib_1.__awaiter(this, void 0, void 0, function* () {
18 yield this.hooks.run('init');
19 const id = argv[2] || 'help';
20 yield this.load();
21 if (this.cmdAskingForHelp(argv)) {
22 this.debug('cmdAskingForHelp');
23 return this.help(argv.slice(2));
24 }
25 let cmd = yield this.findCommand(id);
26 if (!cmd) {
27 let topic = yield this.findTopic(id);
28 if (topic)
29 return this.help([id]);
30 else
31 return this.notFound(id);
32 }
33 yield this.hooks.run('prerun', { argv, Command: cmd });
34 this.debug('running %s', cmd.id);
35 let result;
36 try {
37 result = yield cmd.run(argv);
38 }
39 catch (err) {
40 if (err.showHelp)
41 return this.help(argv.slice(2));
42 throw err;
43 }
44 this.debug('exited normally');
45 return result;
46 });
47 }
48 help(argv) {
49 return tslib_1.__awaiter(this, void 0, void 0, function* () {
50 yield deps_1.default.Help.run(argv, this.config);
51 });
52 }
53 notFound(id) {
54 return tslib_1.__awaiter(this, void 0, void 0, function* () {
55 yield deps_1.default.NotFound.run([id], this.config);
56 });
57 }
58 commands() {
59 return tslib_1.__awaiter(this, void 0, void 0, function* () {
60 yield this.load();
61 return ts_lodash_1.default.sortBy(this.result.allCommands, 'id');
62 });
63 }
64 rootCommands() {
65 return tslib_1.__awaiter(this, void 0, void 0, function* () {
66 yield this.load();
67 let commands = Object.keys(this.result.commands).map(k => this.result.commands[k]);
68 return ts_lodash_1.default.sortBy(commands, 'id');
69 });
70 }
71 findTopic(id) {
72 return tslib_1.__awaiter(this, void 0, void 0, function* () {
73 yield this.load();
74 return this.result.findTopic(id);
75 });
76 }
77 findCommand(id, must) {
78 return tslib_1.__awaiter(this, void 0, void 0, function* () {
79 yield this.load();
80 let cmd = this.result.findCommand(id);
81 if (!cmd && must)
82 throw new Error(`${id} not found`);
83 return cmd;
84 });
85 }
86 topics() {
87 return tslib_1.__awaiter(this, void 0, void 0, function* () {
88 yield this.load();
89 return this.result.allTopics;
90 });
91 }
92 rootTopics() {
93 return tslib_1.__awaiter(this, void 0, void 0, function* () {
94 yield this.load();
95 return this.result.subtopics;
96 });
97 }
98 load() {
99 return tslib_1.__awaiter(this, void 0, void 0, function* () {
100 if (this.result)
101 return;
102 this.debug('load');
103 this.result = new topic_1.RootTopic();
104 yield this.hooks.run('init');
105 let managers = yield this.submanagers();
106 let loads = managers.filter(m => m.load).map(m => m.load().catch(err => {
107 if (err.code === 'ENOCOMMANDS')
108 this.debug(err);
109 else
110 cli_ux_1.default.warn(err);
111 }));
112 for (let r of loads) {
113 let result = yield r;
114 if (result)
115 this.addResult(result);
116 }
117 cli_ux_1.default.action.stop();
118 });
119 }
120 submanagers() {
121 return tslib_1.__awaiter(this, void 0, void 0, function* () {
122 if (this._submanagers)
123 return this._submanagers;
124 const fetch = (managers) => tslib_1.__awaiter(this, void 0, void 0, function* () {
125 const submanagers = yield assync_1.default(managers)
126 .filter(m => !!m.submanagers)
127 .map(m => Promise.resolve(m.submanagers()).then(fetch))
128 .flatMap()
129 .catch(err => {
130 cli_ux_1.default.warn(err);
131 return [];
132 });
133 return [...(managers || []), ...(submanagers || [])];
134 });
135 this._submanagers = yield fetch(this.managers);
136 return this._submanagers;
137 });
138 }
139 addResult(result) {
140 if (!result)
141 return;
142 this.result.addTopics(result.topics);
143 this.result.addCommands(result.commands);
144 }
145 cmdAskingForHelp(argv) {
146 for (let arg of argv) {
147 if (arg === '--help')
148 return true;
149 if (arg === '--')
150 return false;
151 }
152 return false;
153 }
154}
155exports.CommandManager = CommandManager;