@aomex/console 3.30.7 → 3.30.8-alpha.1030873.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +379 -331
- package/dist/index.js +739 -747
- package/dist/index.js.map +1 -1
- package/package.json +17 -15
package/dist/index.js
CHANGED
|
@@ -1,790 +1,782 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { I18n, Middleware, compose, createI18n, ensureI18nLanguage, internalDestroyServices, middleware, toDocument, validate } from "@aomex/common";
|
|
3
|
+
import { stripVTControlCharacters, styleText } from "node:util";
|
|
4
|
+
import symbols from "log-symbols";
|
|
5
|
+
import ansi from "ansi-escapes";
|
|
6
|
+
import { createLogUpdate } from "log-update";
|
|
7
|
+
import isUnicodeSupported from "is-unicode-supported";
|
|
8
|
+
import EventEmitter from "node:events";
|
|
9
|
+
import { hideBin } from "yargs/helpers";
|
|
10
|
+
import parse from "yargs-parser";
|
|
11
|
+
import yargs from "yargs";
|
|
12
|
+
import { toArray } from "@aomex/internal-tools";
|
|
13
|
+
import { getFileValues, pathToFiles } from "@aomex/internal-file-import";
|
|
1
14
|
// src/utils/meta.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
15
|
+
const version = "3.30.8-alpha.1030873.0";
|
|
16
|
+
const scriptName = "aomex";
|
|
5
17
|
// src/override/console-middleware.ts
|
|
6
|
-
import { Middleware } from "@aomex/common";
|
|
7
18
|
var ConsoleMiddleware = class extends Middleware {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
helpInjector;
|
|
20
|
+
constructor(args) {
|
|
21
|
+
const { fn, help = {} } = typeof args === "function" ? { fn: args } : args;
|
|
22
|
+
super(fn);
|
|
23
|
+
this.helpInjector = help;
|
|
24
|
+
}
|
|
25
|
+
help() {
|
|
26
|
+
return this.helpInjector;
|
|
27
|
+
}
|
|
17
28
|
};
|
|
18
29
|
Middleware.register("console", ConsoleMiddleware);
|
|
19
|
-
|
|
20
30
|
// src/utils/collect-console-document.ts
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
87
|
-
await Promise.all(
|
|
88
|
-
injectors.map((injector) => {
|
|
89
|
-
return injector.postDocument?.(document, { app, children: postDocumentLoop });
|
|
90
|
-
})
|
|
91
|
-
);
|
|
92
|
-
};
|
|
93
|
-
await postDocumentLoop(middlewareList);
|
|
94
|
-
return document;
|
|
31
|
+
const collectConsoleDocument = async (data) => {
|
|
32
|
+
const { document, middlewareList, app } = data;
|
|
33
|
+
const hooks = {};
|
|
34
|
+
const onDocumentLoop = async (middlewareList) => {
|
|
35
|
+
const injectors = middlewareList.map((middleware) => {
|
|
36
|
+
return middleware instanceof ConsoleMiddleware ? middleware["help"]() : {};
|
|
37
|
+
});
|
|
38
|
+
await Promise.all(injectors.map((injector) => {
|
|
39
|
+
return injector.onDocument?.(document, {
|
|
40
|
+
app,
|
|
41
|
+
children: onDocumentLoop,
|
|
42
|
+
collectCommand(commandName, middlewareList) {
|
|
43
|
+
hooks[commandName] = middlewareList;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}));
|
|
47
|
+
};
|
|
48
|
+
await onDocumentLoop(middlewareList);
|
|
49
|
+
const onDocumentItemLoop = async (commandName, middlewareList) => {
|
|
50
|
+
if (!commandName || !document[commandName]) return;
|
|
51
|
+
const injectors = middlewareList.map((middleware) => {
|
|
52
|
+
return middleware instanceof ConsoleMiddleware ? middleware["help"]() : {};
|
|
53
|
+
});
|
|
54
|
+
await Promise.all(injectors.map((injector) => {
|
|
55
|
+
return injector.onCommandItem?.(document[commandName], {
|
|
56
|
+
commandName,
|
|
57
|
+
doc: document,
|
|
58
|
+
app,
|
|
59
|
+
children: onDocumentItemLoop.bind(null, commandName)
|
|
60
|
+
});
|
|
61
|
+
}));
|
|
62
|
+
};
|
|
63
|
+
await Promise.all(Object.entries(hooks).map(([commandName, middlewareList]) => {
|
|
64
|
+
return onDocumentItemLoop(commandName, middlewareList);
|
|
65
|
+
}));
|
|
66
|
+
const postDocumentItemLoop = async (commandName, middlewareList) => {
|
|
67
|
+
if (!commandName || !document[commandName]) return;
|
|
68
|
+
const injectors = [...middlewareList].reverse().map((middleware) => {
|
|
69
|
+
return middleware instanceof ConsoleMiddleware ? middleware["help"]() : {};
|
|
70
|
+
});
|
|
71
|
+
await Promise.all(injectors.map((injector) => {
|
|
72
|
+
return injector.postCommandItem?.(document[commandName], {
|
|
73
|
+
commandName,
|
|
74
|
+
doc: document,
|
|
75
|
+
app,
|
|
76
|
+
children: postDocumentItemLoop.bind(null, commandName)
|
|
77
|
+
});
|
|
78
|
+
}));
|
|
79
|
+
};
|
|
80
|
+
await Promise.all(Object.entries(hooks).map(([commandName, middlewareList]) => {
|
|
81
|
+
return postDocumentItemLoop(commandName, middlewareList);
|
|
82
|
+
}));
|
|
83
|
+
const postDocumentLoop = async (middlewareList) => {
|
|
84
|
+
const injectors = [...middlewareList].reverse().map((middleware) => {
|
|
85
|
+
return middleware instanceof ConsoleMiddleware ? middleware["help"]() : {};
|
|
86
|
+
});
|
|
87
|
+
await Promise.all(injectors.map((injector) => {
|
|
88
|
+
return injector.postDocument?.(document, {
|
|
89
|
+
app,
|
|
90
|
+
children: postDocumentLoop
|
|
91
|
+
});
|
|
92
|
+
}));
|
|
93
|
+
};
|
|
94
|
+
await postDocumentLoop(middlewareList);
|
|
95
|
+
return document;
|
|
95
96
|
};
|
|
96
|
-
|
|
97
97
|
// src/utils/terminal.ts
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
import isUnicodeSupported from "is-unicode-supported";
|
|
103
|
-
import { stripVTControlCharacters } from "util";
|
|
104
|
-
import { createRequire } from "module";
|
|
105
|
-
var requireCJS = createRequire(import.meta.url);
|
|
98
|
+
const requireCJS = createRequire(import.meta.url);
|
|
99
|
+
/**
|
|
100
|
+
* 终端输出实用函数
|
|
101
|
+
*/
|
|
106
102
|
var Terminal = class {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
103
|
+
/**
|
|
104
|
+
* 打印文字,同`console.log`
|
|
105
|
+
*/
|
|
106
|
+
print(...messages) {
|
|
107
|
+
console.log(...messages);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 打印黄色警告信息,开头显示小图标 ⚠
|
|
111
|
+
*/
|
|
112
|
+
printWarning(...messages) {
|
|
113
|
+
console.warn(symbols.warning, ...messages.map((message) => this.style("yellow", String(message))));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 打印红色错误信息,开头显示小图标 ✖️
|
|
117
|
+
*/
|
|
118
|
+
printError(...messages) {
|
|
119
|
+
console.error(symbols.error, ...messages.map((message) => this.style("red", String(message))));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 打印绿色成功信息,开头显示小图标 ✔
|
|
123
|
+
*/
|
|
124
|
+
printSuccess(...messages) {
|
|
125
|
+
console.log(symbols.success, ...messages.map((message) => this.style("green", String(message))));
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 打印二维数组,呈现形式为表格
|
|
129
|
+
* ```typescript
|
|
130
|
+
* terminal.table([
|
|
131
|
+
* ['编号', '姓名', '性别'],
|
|
132
|
+
* [1, '张三', '男'],
|
|
133
|
+
* [2, '李四', '男'],
|
|
134
|
+
* [3, '翠花', '女'],
|
|
135
|
+
* ]);
|
|
136
|
+
* ```
|
|
137
|
+
* @link https://www.npmjs.com/package/table
|
|
138
|
+
*/
|
|
139
|
+
printTable(data, config) {
|
|
140
|
+
this.print(this.generateTable(data, config));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 使用二维数组生成表格字符串
|
|
144
|
+
* ```typescript
|
|
145
|
+
* const table = terminal.generateTable([
|
|
146
|
+
* ['编号', '姓名', '性别'],
|
|
147
|
+
* [1, '张三', '男'],
|
|
148
|
+
* [2, '李四', '男'],
|
|
149
|
+
* [3, '翠花', '女'],
|
|
150
|
+
* ]);
|
|
151
|
+
* ```
|
|
152
|
+
* @link https://www.npmjs.com/package/table
|
|
153
|
+
*/
|
|
154
|
+
generateTable(data, config) {
|
|
155
|
+
const { table } = requireCJS("table");
|
|
156
|
+
return table(data, config).replace(/\n$/, "");
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* 清除终端屏幕文字,鼠标移到第一行开头
|
|
160
|
+
*/
|
|
161
|
+
clearScreen() {
|
|
162
|
+
process.stdout.write(ansi.clearScreen);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 持续刷新终端文字。
|
|
166
|
+
*
|
|
167
|
+
* 文字可包含一些内置的占位符:
|
|
168
|
+
* - `%loading%` 加载动画
|
|
169
|
+
* - `%prepare%` 灰色准备小图标 ◼
|
|
170
|
+
* - `%success%` 绿色成功小图标 ✔
|
|
171
|
+
* - `%info%` 蓝色信息小图标 ℹ
|
|
172
|
+
* - `%warning%` 黄色警告小图标 ⚠
|
|
173
|
+
* - `%error%` 红色错误小图标 ✖️
|
|
174
|
+
* - `%skip%` 灰色跳过小图标 ↓
|
|
175
|
+
*
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const session = terminal.applySession();
|
|
178
|
+
*
|
|
179
|
+
* session.update('%loading% 加载中\n 猜猜我是谁');
|
|
180
|
+
* await sleep(1000);
|
|
181
|
+
* session.update('%success% 成功\n 猜中了');
|
|
182
|
+
* session.commit();
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
createSession() {
|
|
186
|
+
const supportUnicode = isUnicodeSupported();
|
|
187
|
+
const logSession = createLogUpdate(process.stdout);
|
|
188
|
+
const loadingFrames = supportUnicode ? [
|
|
189
|
+
"⠋",
|
|
190
|
+
"⠙",
|
|
191
|
+
"⠹",
|
|
192
|
+
"⠸",
|
|
193
|
+
"⠼",
|
|
194
|
+
"⠴",
|
|
195
|
+
"⠦",
|
|
196
|
+
"⠧",
|
|
197
|
+
"⠇",
|
|
198
|
+
"⠏"
|
|
199
|
+
] : [
|
|
200
|
+
"-",
|
|
201
|
+
"\\",
|
|
202
|
+
"|",
|
|
203
|
+
"/"
|
|
204
|
+
];
|
|
205
|
+
const skipSymbol = this.style("gray", supportUnicode ? "↓" : "-");
|
|
206
|
+
const prepareSymbol = this.style(["gray", "dim"], supportUnicode ? "◼" : ".");
|
|
207
|
+
let loadingFrameIndex = 0;
|
|
208
|
+
let cacheText = "";
|
|
209
|
+
let timer;
|
|
210
|
+
const replaceText = () => {
|
|
211
|
+
return cacheText.replaceAll("%loading%", loadingFrames[loadingFrameIndex]);
|
|
212
|
+
};
|
|
213
|
+
const reset = () => {
|
|
214
|
+
clearInterval(timer);
|
|
215
|
+
timer = void 0;
|
|
216
|
+
loadingFrameIndex = 0;
|
|
217
|
+
};
|
|
218
|
+
let prevText = "";
|
|
219
|
+
return {
|
|
220
|
+
/**
|
|
221
|
+
* 替换当前输出的文字
|
|
222
|
+
*/
|
|
223
|
+
update: (text) => {
|
|
224
|
+
if (prevText === text) return;
|
|
225
|
+
prevText = text;
|
|
226
|
+
cacheText = text.replaceAll("%success%", symbols.success).replaceAll("%error%", symbols.error).replaceAll("%warning%", symbols.warning).replaceAll("%info%", symbols.info).replaceAll("%skip%", skipSymbol).replaceAll("%prepare%", prepareSymbol);
|
|
227
|
+
logSession(replaceText());
|
|
228
|
+
if (text.includes("%loading%")) timer ||= setInterval(() => {
|
|
229
|
+
loadingFrameIndex += 1;
|
|
230
|
+
if (loadingFrameIndex === loadingFrames.length) loadingFrameIndex = 0;
|
|
231
|
+
logSession(replaceText());
|
|
232
|
+
}, 80);
|
|
233
|
+
else reset();
|
|
234
|
+
},
|
|
235
|
+
/**
|
|
236
|
+
* 持久化已经输出的文字,进入下一个session
|
|
237
|
+
*/
|
|
238
|
+
commit: () => {
|
|
239
|
+
reset();
|
|
240
|
+
logSession.done();
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
async runTasks(_tasks, opts = {}) {
|
|
245
|
+
const { concurrent = false } = opts;
|
|
246
|
+
const log = this.createSession();
|
|
247
|
+
const tasks = _tasks.map((task) => {
|
|
248
|
+
return {
|
|
249
|
+
...task,
|
|
250
|
+
suffix: "",
|
|
251
|
+
status: "prepare"
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
const reRender = () => {
|
|
255
|
+
log.update(tasks.map((task) => {
|
|
256
|
+
const suffix = task.status === "skip" ? this.style("gray", `[skipped]`) : task.suffix;
|
|
257
|
+
return `%${task.status}% ${task.title}${suffix ? " " + suffix : ""}`;
|
|
258
|
+
}).join("\n"));
|
|
259
|
+
};
|
|
260
|
+
reRender();
|
|
261
|
+
const timer = setInterval(reRender, 16);
|
|
262
|
+
const handleTask = async (ctx, task) => {
|
|
263
|
+
if (typeof task.skip === "function" ? await task.skip(ctx) : task.skip === true) {
|
|
264
|
+
task.status = "skip";
|
|
265
|
+
return { error: null };
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
task.status = "loading";
|
|
269
|
+
let statusChanged = false;
|
|
270
|
+
await task.task(ctx, {
|
|
271
|
+
get status() {
|
|
272
|
+
return task.status;
|
|
273
|
+
},
|
|
274
|
+
set status(status) {
|
|
275
|
+
task.status = status;
|
|
276
|
+
statusChanged = true;
|
|
277
|
+
},
|
|
278
|
+
get title() {
|
|
279
|
+
return task.title;
|
|
280
|
+
},
|
|
281
|
+
set title(t) {
|
|
282
|
+
task.title = t;
|
|
283
|
+
},
|
|
284
|
+
get suffix() {
|
|
285
|
+
return task.suffix;
|
|
286
|
+
},
|
|
287
|
+
set suffix(s) {
|
|
288
|
+
task.suffix = s;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
if (!statusChanged) task.status = "success";
|
|
292
|
+
} catch (e) {
|
|
293
|
+
task.status = "error";
|
|
294
|
+
task.suffix = this.style("red", e.message);
|
|
295
|
+
return { error: e };
|
|
296
|
+
}
|
|
297
|
+
return { error: null };
|
|
298
|
+
};
|
|
299
|
+
try {
|
|
300
|
+
const ctx = {};
|
|
301
|
+
if (concurrent) {
|
|
302
|
+
const result = await Promise.allSettled(tasks.map((task) => handleTask(ctx, task)));
|
|
303
|
+
const errors = [];
|
|
304
|
+
for (const item of result) if (item.status === "fulfilled" && item.value.error !== null) errors.push(item.value.error);
|
|
305
|
+
if (errors.length) return {
|
|
306
|
+
errors,
|
|
307
|
+
context: ctx
|
|
308
|
+
};
|
|
309
|
+
} else for (const task of tasks) {
|
|
310
|
+
const { error } = await handleTask(ctx, task);
|
|
311
|
+
if (error) return {
|
|
312
|
+
error,
|
|
313
|
+
context: ctx
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
context: ctx,
|
|
318
|
+
error: null
|
|
319
|
+
};
|
|
320
|
+
} finally {
|
|
321
|
+
clearInterval(timer);
|
|
322
|
+
reRender();
|
|
323
|
+
log.commit();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* 为字符串设置样式:
|
|
328
|
+
* - 文字颜色、背景色
|
|
329
|
+
* - 粗体、斜体
|
|
330
|
+
* - 下划线、删除线、上划线、双下划线
|
|
331
|
+
*
|
|
332
|
+
* ```typescript
|
|
333
|
+
* const errorMsg = terminal.style('red', '一些错误的内容');
|
|
334
|
+
* const warnMsg = terminal.style(['yellow', 'bold'], '严重警告文字');
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
style(color, string) {
|
|
338
|
+
return styleText(color, string);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* 去除ansi字符
|
|
342
|
+
* ```typescript
|
|
343
|
+
* terminal.stripStyle('\u001B[4mUnicorn\u001B[0m'); // Unicorn
|
|
344
|
+
* ```
|
|
345
|
+
*/
|
|
346
|
+
stripStyle(string) {
|
|
347
|
+
return stripVTControlCharacters(string);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
const terminal = new Terminal();
|
|
351
|
+
// src/utils/console-error.ts
|
|
352
|
+
/**
|
|
353
|
+
* CLI 指令异常。`app.on('error')` 收到的均为本类实例。
|
|
354
|
+
*
|
|
355
|
+
* 业务侧用 `new ConsoleError` 创建;其它错误经 {@link ConsoleError.from} 转换。
|
|
356
|
+
*/
|
|
357
|
+
var ConsoleError = class ConsoleError extends Error {
|
|
358
|
+
code;
|
|
359
|
+
constructor(message, properties) {
|
|
360
|
+
const { cause, code } = properties ?? {};
|
|
361
|
+
super(message ?? "Error", cause !== void 0 ? { cause } : void 0);
|
|
362
|
+
this.name = "ConsoleError";
|
|
363
|
+
if (code !== void 0) this.code = code;
|
|
364
|
+
Error.captureStackTrace?.(this, new.target);
|
|
365
|
+
}
|
|
366
|
+
/** 是否为自制 {@link ConsoleError}(覆盖 `Error.isError`,类型收窄为本类) */
|
|
367
|
+
static isError(value) {
|
|
368
|
+
return value instanceof ConsoleError;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* 将任意错误转为自制 {@link ConsoleError}。
|
|
372
|
+
*
|
|
373
|
+
* - 已是本类实例:原样返回
|
|
374
|
+
* - `Error`:迁移 `message` / `cause` / `name` / `stack`,以及字符串 `code`(Node `ErrnoException`)
|
|
375
|
+
* - 其余(含 plain object):不猜测字段,落成默认实例或仅用字符串 message
|
|
376
|
+
*/
|
|
377
|
+
static from(error) {
|
|
378
|
+
if (ConsoleError.isError(error)) return error;
|
|
379
|
+
if (error == null) return new ConsoleError();
|
|
380
|
+
if (typeof error === "string") return new ConsoleError(error);
|
|
381
|
+
if (!Error.isError(error)) return new ConsoleError();
|
|
382
|
+
const code = error.code;
|
|
383
|
+
const err = new ConsoleError(error.message || void 0, {
|
|
384
|
+
code: typeof code === "string" ? code : void 0,
|
|
385
|
+
cause: error.cause
|
|
386
|
+
});
|
|
387
|
+
if (error.name && error.name !== "Error") err.name = error.name;
|
|
388
|
+
if (error.stack) err.stack = error.stack;
|
|
389
|
+
return err;
|
|
390
|
+
}
|
|
351
391
|
};
|
|
352
|
-
var terminal = new Terminal();
|
|
353
|
-
|
|
354
|
-
// src/cli/app.ts
|
|
355
|
-
import EventEmitter from "events";
|
|
356
|
-
import { EOL } from "os";
|
|
357
|
-
import { styleText as styleText3 } from "util";
|
|
358
|
-
import { hideBin } from "yargs/helpers";
|
|
359
|
-
import { I18n as I18n4, compose, middleware as middleware2 } from "@aomex/common";
|
|
360
|
-
|
|
361
392
|
// src/cli/input.ts
|
|
362
|
-
import parse from "yargs-parser";
|
|
363
393
|
var ConsoleInput = class {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
394
|
+
app;
|
|
395
|
+
argv;
|
|
396
|
+
options;
|
|
397
|
+
command;
|
|
398
|
+
constructor(app, argv) {
|
|
399
|
+
this.app = app;
|
|
400
|
+
this.argv = argv;
|
|
401
|
+
const { _: commands, ...rawOptions } = parse(argv, { configuration: { "parse-numbers": false } });
|
|
402
|
+
this.options = rawOptions;
|
|
403
|
+
this.command = String(commands[0] || "");
|
|
404
|
+
}
|
|
405
|
+
parseArgv(alias) {
|
|
406
|
+
const { _: commands, ...rawOptions } = parse(this.argv, {
|
|
407
|
+
alias,
|
|
408
|
+
configuration: { "parse-numbers": false }
|
|
409
|
+
});
|
|
410
|
+
return rawOptions;
|
|
411
|
+
}
|
|
382
412
|
};
|
|
383
|
-
|
|
384
413
|
// src/cli/context.ts
|
|
385
414
|
var ConsoleContext = class {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
415
|
+
app;
|
|
416
|
+
input;
|
|
417
|
+
commandMatched = false;
|
|
418
|
+
constructor(app, input) {
|
|
419
|
+
this.app = app;
|
|
420
|
+
this.input = input;
|
|
421
|
+
}
|
|
422
|
+
/** 与构造时传入的 `logger` 相同,取自 app */
|
|
423
|
+
get logger() {
|
|
424
|
+
return this.app["logger"];
|
|
425
|
+
}
|
|
391
426
|
};
|
|
427
|
+
// src/utils/get-distance.ts
|
|
428
|
+
/**
|
|
429
|
+
Copyright (c) 2011 Andrei Mackenzie
|
|
392
430
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
431
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
432
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
433
|
+
the Software without restriction, including without limitation the rights to
|
|
434
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
435
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
436
|
+
subject to the following conditions:
|
|
397
437
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
if (a.length === 0) return b.length;
|
|
401
|
-
if (b.length === 0) return a.length;
|
|
402
|
-
const matrix = [];
|
|
403
|
-
let i;
|
|
404
|
-
for (i = 0; i <= b.length; i++) {
|
|
405
|
-
matrix[i] = [i];
|
|
406
|
-
}
|
|
407
|
-
let j;
|
|
408
|
-
for (j = 0; j <= a.length; j++) {
|
|
409
|
-
matrix[0][j] = j;
|
|
410
|
-
}
|
|
411
|
-
for (i = 1; i <= b.length; i++) {
|
|
412
|
-
for (j = 1; j <= a.length; j++) {
|
|
413
|
-
if (b.charAt(i - 1) === a.charAt(j - 1)) {
|
|
414
|
-
matrix[i][j] = matrix[i - 1][j - 1];
|
|
415
|
-
} else if (i > 1 && j > 1 && b.charAt(i - 2) === a.charAt(j - 1) && b.charAt(i - 1) === a.charAt(j - 2)) {
|
|
416
|
-
matrix[i][j] = matrix[i - 2][j - 2] + 1;
|
|
417
|
-
} else {
|
|
418
|
-
matrix[i][j] = Math.min(
|
|
419
|
-
matrix[i - 1][j - 1] + 1,
|
|
420
|
-
// substitution
|
|
421
|
-
Math.min(
|
|
422
|
-
matrix[i][j - 1] + 1,
|
|
423
|
-
// insertion
|
|
424
|
-
matrix[i - 1][j] + 1
|
|
425
|
-
)
|
|
426
|
-
);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
return matrix[b.length][a.length];
|
|
431
|
-
};
|
|
438
|
+
The above copyright notice and this permission notice shall be included in all
|
|
439
|
+
copies or substantial portions of the Software.
|
|
432
440
|
|
|
433
|
-
|
|
434
|
-
|
|
441
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
442
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
443
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
444
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
445
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
446
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
435
447
|
|
|
448
|
+
@link https://gist.github.com/andrei-m/982927
|
|
449
|
+
@link https://github.com/yargs/yargs/blob/main/lib/utils/levenshtein.ts
|
|
450
|
+
*/
|
|
451
|
+
const getDistance = (a, b) => {
|
|
452
|
+
if (a.length === 0) return b.length;
|
|
453
|
+
if (b.length === 0) return a.length;
|
|
454
|
+
const matrix = [];
|
|
455
|
+
let i = 0;
|
|
456
|
+
for (; i <= b.length; i++) matrix[i] = [i];
|
|
457
|
+
let j = 0;
|
|
458
|
+
for (; j <= a.length; j++) matrix[0][j] = j;
|
|
459
|
+
for (i = 1; i <= b.length; i++) for (j = 1; j <= a.length; j++) if (b.charAt(i - 1) === a.charAt(j - 1)) matrix[i][j] = matrix[i - 1][j - 1];
|
|
460
|
+
else if (i > 1 && j > 1 && b.charAt(i - 2) === a.charAt(j - 1) && b.charAt(i - 1) === a.charAt(j - 2)) matrix[i][j] = matrix[i - 2][j - 2] + 1;
|
|
461
|
+
else matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, Math.min(matrix[i][j - 1] + 1, matrix[i - 1][j] + 1));
|
|
462
|
+
return matrix[b.length][a.length];
|
|
463
|
+
};
|
|
436
464
|
// src/i18n/locales/zh-cn.ts
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
465
|
+
const zh = I18n.define({
|
|
466
|
+
command_not_found: "命令「{{command}}」不存在",
|
|
467
|
+
command_found_recommended: "命令「{{command}}」不存在,你是说「aomex {{recommended}}」吗?",
|
|
468
|
+
help: {
|
|
469
|
+
command: "[命令]",
|
|
470
|
+
option: "[选项]",
|
|
471
|
+
version: "显示 {{scriptName}} 的版本号",
|
|
472
|
+
no_usage: "找不到命令「{{command}}」的用法"
|
|
473
|
+
}
|
|
447
474
|
});
|
|
448
|
-
|
|
449
475
|
// src/i18n/locales/en-us.ts
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
476
|
+
const en = I18n.satisfies(zh).define({
|
|
477
|
+
command_not_found: "Command \"{{command}}\" does not exist",
|
|
478
|
+
command_found_recommended: "Command \"{{command}}\" does not exist. Did you mean \"aomex {{recommended}}\"?",
|
|
479
|
+
help: {
|
|
480
|
+
command: "[command]",
|
|
481
|
+
option: "[options]",
|
|
482
|
+
version: "Show the version of {{scriptName}}",
|
|
483
|
+
no_usage: "No usage found for command \"{{command}}\""
|
|
484
|
+
}
|
|
460
485
|
});
|
|
461
|
-
|
|
462
|
-
// src/i18n/locales/index.ts
|
|
463
|
-
var locales = {
|
|
464
|
-
zh_CN: zh,
|
|
465
|
-
en_US: en
|
|
466
|
-
};
|
|
467
|
-
|
|
468
486
|
// src/i18n/index.ts
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
487
|
+
const i18n = createI18n({
|
|
488
|
+
zh,
|
|
489
|
+
en,
|
|
490
|
+
load: (file) => import(new URL(`./locales/${file}.js`, import.meta.url).href)
|
|
472
491
|
});
|
|
473
|
-
|
|
474
492
|
// src/middleware/help-logger.ts
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
}
|
|
541
|
-
});
|
|
493
|
+
/** 规范码 → yargs/y18n 文件名;其余把 `-` 换成 `_`,由 y18n 自行回退到语种前缀。 */
|
|
494
|
+
function yargsLocale(language) {
|
|
495
|
+
if (language === "zh-HK") return "zh_TW";
|
|
496
|
+
return language.replaceAll("-", "_");
|
|
497
|
+
}
|
|
498
|
+
const helpLogger = (middlewareList) => {
|
|
499
|
+
return middleware.console(async (ctx, next) => {
|
|
500
|
+
const { input: { options, command } } = ctx;
|
|
501
|
+
if (command === "") {
|
|
502
|
+
ctx.commandMatched = true;
|
|
503
|
+
if (options["version"] || options["v"]) console.log(version);
|
|
504
|
+
else {
|
|
505
|
+
const cli = yargs([]).locale(yargsLocale(i18n.language)).scriptName(scriptName).usage(`${scriptName} ${i18n.t("help.command")} ${i18n.t("help.option")}`).describe("version", i18n.t("help.version", { scriptName })).alias("v", "version").alias("h", "help");
|
|
506
|
+
const document = {};
|
|
507
|
+
await collectConsoleDocument({
|
|
508
|
+
document,
|
|
509
|
+
middlewareList,
|
|
510
|
+
app: ctx.app
|
|
511
|
+
});
|
|
512
|
+
Object.entries(document).forEach(([commandName, commandItem]) => {
|
|
513
|
+
if (commandItem.showInHelp === false) return;
|
|
514
|
+
cli.command(styleText("yellow", commandName), commandItem.summary || "");
|
|
515
|
+
});
|
|
516
|
+
cli.showHelp("log");
|
|
517
|
+
}
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (options["help"] || options["h"]) {
|
|
521
|
+
ctx.commandMatched = true;
|
|
522
|
+
const document = {};
|
|
523
|
+
await collectConsoleDocument({
|
|
524
|
+
document,
|
|
525
|
+
middlewareList,
|
|
526
|
+
app: ctx.app
|
|
527
|
+
});
|
|
528
|
+
const commandItem = document[command];
|
|
529
|
+
if (!commandItem || commandItem.showInHelp === false) throw new ConsoleError(i18n.t("help.no_usage", { command }), { code: "NO_USAGE" });
|
|
530
|
+
const cli = yargs([]).locale(yargsLocale(i18n.language)).scriptName(scriptName).version(false).help(false);
|
|
531
|
+
const { description, parameters = [] } = commandItem;
|
|
532
|
+
cli.usage(`${scriptName} ${command} ${i18n.t("help.option")}${description ? "\n\n" + styleText("bold", description) : ""}`);
|
|
533
|
+
parameters.forEach((parameter) => {
|
|
534
|
+
cli.option(parameter.name, {
|
|
535
|
+
alias: parameter.alias,
|
|
536
|
+
description: parameter.description,
|
|
537
|
+
deprecated: parameter.deprecated,
|
|
538
|
+
type: parameter.type,
|
|
539
|
+
default: parameter.defaultValue
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
cli.showHelp("log");
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
await next();
|
|
546
|
+
if (!ctx.commandMatched) {
|
|
547
|
+
const recommendCommand = Object.entries(await collectConsoleDocument({
|
|
548
|
+
document: {},
|
|
549
|
+
middlewareList,
|
|
550
|
+
app: ctx.app
|
|
551
|
+
})).map(([commandName]) => [commandName, getDistance(command, commandName)]).filter((item) => item[1] <= 3).sort((a, b) => a[1] - b[1])[0];
|
|
552
|
+
throw new ConsoleError(recommendCommand ? i18n.t("command_found_recommended", {
|
|
553
|
+
command,
|
|
554
|
+
recommended: recommendCommand[0]
|
|
555
|
+
}) : i18n.t("command_not_found", { command }), { code: "COMMAND_NOT_FOUND" });
|
|
556
|
+
}
|
|
557
|
+
});
|
|
542
558
|
};
|
|
543
|
-
|
|
544
559
|
// src/cli/app.ts
|
|
545
560
|
var ConsoleApp = class extends EventEmitter {
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
561
|
+
options;
|
|
562
|
+
level = 0;
|
|
563
|
+
middlewareList;
|
|
564
|
+
logger;
|
|
565
|
+
constructor(options) {
|
|
566
|
+
super();
|
|
567
|
+
this.options = options;
|
|
568
|
+
this.middlewareList = options.mount || [];
|
|
569
|
+
this.logger = options.logger;
|
|
570
|
+
this.bindServicesShutdown(toArray(options.services));
|
|
571
|
+
}
|
|
572
|
+
bindServicesShutdown(services) {
|
|
573
|
+
process.once("beforeExit", () => {
|
|
574
|
+
internalDestroyServices(services);
|
|
575
|
+
});
|
|
576
|
+
const onSignal = () => {
|
|
577
|
+
internalDestroyServices(services).finally(() => {
|
|
578
|
+
process.exit(0);
|
|
579
|
+
});
|
|
580
|
+
};
|
|
581
|
+
process.once("SIGTERM", onSignal);
|
|
582
|
+
process.once("SIGINT", onSignal);
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* 执行指令。如果不传参数,则使用内置的`process.argv`
|
|
586
|
+
* ```typescript
|
|
587
|
+
* app.run('schedule:a')
|
|
588
|
+
* app.run('schedule:a', '-f', '--type', 'all')
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
async run(...commands) {
|
|
592
|
+
const currentLevel = this.level++;
|
|
593
|
+
const input = new ConsoleInput(this, commands.length ? commands : hideBin(process.argv));
|
|
594
|
+
const ctx = new ConsoleContext(this, input);
|
|
595
|
+
if (!this.listenerCount("error")) this.on("error", (err) => {
|
|
596
|
+
this.logger.error(err);
|
|
597
|
+
});
|
|
598
|
+
try {
|
|
599
|
+
await compose([
|
|
600
|
+
middleware.mixin(async (_, next) => {
|
|
601
|
+
const language = this.options.language;
|
|
602
|
+
await ensureI18nLanguage(language);
|
|
603
|
+
return I18n.provider(language, next);
|
|
604
|
+
}),
|
|
605
|
+
helpLogger(this.middlewareList),
|
|
606
|
+
...this.middlewareList
|
|
607
|
+
])(ctx);
|
|
608
|
+
return 0;
|
|
609
|
+
} catch (e) {
|
|
610
|
+
const err = ConsoleError.from(e);
|
|
611
|
+
this.emit("error", err, ctx, currentLevel);
|
|
612
|
+
return 1;
|
|
613
|
+
} finally {
|
|
614
|
+
--this.level;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
591
617
|
};
|
|
592
|
-
|
|
593
618
|
// src/middleware/options.ts
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
619
|
+
/**
|
|
620
|
+
* 接收并验证命令行的选项,并且保证类型安全
|
|
621
|
+
*
|
|
622
|
+
* ```typescript
|
|
623
|
+
* export const commander = new Commander();
|
|
624
|
+
*
|
|
625
|
+
* commander.create('my/schedule', {
|
|
626
|
+
* mount: [
|
|
627
|
+
* options({
|
|
628
|
+
* id: rule.int(),
|
|
629
|
+
* date: rule.date('YYYY-MM-DD'),
|
|
630
|
+
* }, {
|
|
631
|
+
* date: ['d'], // alias --date or -d
|
|
632
|
+
* });
|
|
633
|
+
* ],
|
|
634
|
+
* action: async (ctx) => {
|
|
635
|
+
* console.log(ctx.options);
|
|
636
|
+
* }
|
|
637
|
+
* });
|
|
638
|
+
* ```
|
|
639
|
+
*
|
|
640
|
+
*/
|
|
641
|
+
const options = (fields, aliasList) => {
|
|
642
|
+
return middleware.console({
|
|
643
|
+
fn: async (ctx, next) => {
|
|
644
|
+
let options = ctx.input.options;
|
|
645
|
+
if (aliasList && Object.keys(aliasList).length) options = ctx.input.parseArgv(aliasList);
|
|
646
|
+
ctx.options = await validate(options, fields);
|
|
647
|
+
return next();
|
|
648
|
+
},
|
|
649
|
+
help: { onCommandItem(commandItem) {
|
|
650
|
+
for (const [key, validator] of Object.entries(fields)) {
|
|
651
|
+
const docs = toDocument(validator);
|
|
652
|
+
const schema = docs.schema;
|
|
653
|
+
let dateType;
|
|
654
|
+
switch (schema?.type) {
|
|
655
|
+
case "array":
|
|
656
|
+
dateType = "array";
|
|
657
|
+
break;
|
|
658
|
+
case "integer":
|
|
659
|
+
case "number":
|
|
660
|
+
dateType = "number";
|
|
661
|
+
break;
|
|
662
|
+
case "boolean":
|
|
663
|
+
dateType = "boolean";
|
|
664
|
+
break;
|
|
665
|
+
case "string":
|
|
666
|
+
/**
|
|
667
|
+
* 命令行无法输入对象
|
|
668
|
+
* @see ObjectValidator.parseFromString()
|
|
669
|
+
*/
|
|
670
|
+
case "object": dateType = "string";
|
|
671
|
+
}
|
|
672
|
+
const alias = aliasList?.[key];
|
|
673
|
+
commandItem.parameters ||= [];
|
|
674
|
+
commandItem.parameters.push({
|
|
675
|
+
name: key,
|
|
676
|
+
alias: alias && toArray(alias),
|
|
677
|
+
type: dateType,
|
|
678
|
+
description: docs.description,
|
|
679
|
+
defaultValue: schema?.default,
|
|
680
|
+
deprecated: docs.deprecated
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
} }
|
|
684
|
+
});
|
|
646
685
|
};
|
|
647
|
-
|
|
648
|
-
// src/commander/commanders.ts
|
|
649
|
-
import { compose as compose3, middleware as middleware6 } from "@aomex/common";
|
|
650
|
-
import {
|
|
651
|
-
pathToFiles,
|
|
652
|
-
getFileValues
|
|
653
|
-
} from "@aomex/internal-file-import";
|
|
654
|
-
|
|
655
|
-
// src/commander/commander.ts
|
|
656
|
-
import { compose as compose2, middleware as middleware5 } from "@aomex/common";
|
|
657
|
-
import { toArray as toArray2 } from "@aomex/internal-tools";
|
|
658
|
-
|
|
659
686
|
// src/commander/builder.ts
|
|
660
|
-
import { middleware as middleware4 } from "@aomex/common";
|
|
661
687
|
var Builder = class {
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
match(command) {
|
|
675
|
-
return this.commands.includes(command);
|
|
676
|
-
}
|
|
688
|
+
docs;
|
|
689
|
+
commands;
|
|
690
|
+
middlewareList;
|
|
691
|
+
constructor(prefix, commands, options) {
|
|
692
|
+
this.docs = options.docs || {};
|
|
693
|
+
this.docs.showInHelp ??= true;
|
|
694
|
+
this.commands = commands.map((item) => prefix + item);
|
|
695
|
+
this.middlewareList = [...options.mount || [], middleware.console((ctx, _) => options.action(ctx))];
|
|
696
|
+
}
|
|
697
|
+
match(command) {
|
|
698
|
+
return this.commands.includes(command);
|
|
699
|
+
}
|
|
677
700
|
};
|
|
678
|
-
|
|
679
701
|
// src/commander/commander.ts
|
|
680
702
|
var Commander = class {
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
},
|
|
729
|
-
postDocument: async (_, { children }) => {
|
|
730
|
-
const totalMiddlewareList = [...this.middlewareList];
|
|
731
|
-
for (const builder of builders) {
|
|
732
|
-
totalMiddlewareList.push(...builder["middlewareList"]);
|
|
733
|
-
}
|
|
734
|
-
await children(totalMiddlewareList);
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
});
|
|
738
|
-
}
|
|
703
|
+
opts;
|
|
704
|
+
middlewareList;
|
|
705
|
+
prefix;
|
|
706
|
+
builders = [];
|
|
707
|
+
constructor(opts = {}) {
|
|
708
|
+
this.opts = opts;
|
|
709
|
+
this.prefix = opts.prefix || "";
|
|
710
|
+
this.middlewareList = opts.mount || [];
|
|
711
|
+
}
|
|
712
|
+
create(commandName, options) {
|
|
713
|
+
const builder = new Builder(this.prefix, toArray(commandName), options);
|
|
714
|
+
options.disabled !== true && this.builders.push(builder);
|
|
715
|
+
}
|
|
716
|
+
toMiddleware() {
|
|
717
|
+
const builders = [...this.builders];
|
|
718
|
+
const buildersLength = builders.length;
|
|
719
|
+
const commandFn = builders.map((builder) => {
|
|
720
|
+
return compose(this.middlewareList.concat(builder["middlewareList"]));
|
|
721
|
+
});
|
|
722
|
+
return middleware.console({
|
|
723
|
+
fn: (ctx, next) => {
|
|
724
|
+
const { input } = ctx;
|
|
725
|
+
const { command } = input;
|
|
726
|
+
for (let i = 0; i < buildersLength; ++i) if (builders[i].match(command)) {
|
|
727
|
+
ctx.commandMatched = true;
|
|
728
|
+
return commandFn[i](ctx, next);
|
|
729
|
+
}
|
|
730
|
+
return next();
|
|
731
|
+
},
|
|
732
|
+
help: {
|
|
733
|
+
onDocument: async (doc, { children, collectCommand }) => {
|
|
734
|
+
for (const builder of builders) for (const commandName of builder.commands) {
|
|
735
|
+
doc[commandName] = { ...builder.docs };
|
|
736
|
+
collectCommand(commandName, this.middlewareList.concat(builder["middlewareList"]));
|
|
737
|
+
}
|
|
738
|
+
const totalMiddlewareList = [...this.middlewareList];
|
|
739
|
+
for (const builder of builders) totalMiddlewareList.push(...builder["middlewareList"]);
|
|
740
|
+
await children(totalMiddlewareList);
|
|
741
|
+
},
|
|
742
|
+
postDocument: async (_, { children }) => {
|
|
743
|
+
const totalMiddlewareList = [...this.middlewareList];
|
|
744
|
+
for (const builder of builders) totalMiddlewareList.push(...builder["middlewareList"]);
|
|
745
|
+
await children(totalMiddlewareList);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
}
|
|
739
750
|
};
|
|
740
|
-
|
|
741
751
|
// src/commander/commanders.ts
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
},
|
|
767
|
-
async postDocument(_, { children }) {
|
|
768
|
-
await children(commanders2.map((commander) => commander["toMiddleware"]()));
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
});
|
|
752
|
+
const commanders = (options) => {
|
|
753
|
+
let composeFn;
|
|
754
|
+
let commanders;
|
|
755
|
+
let promise = Promise.resolve();
|
|
756
|
+
if (isCommanders(options)) commanders = options;
|
|
757
|
+
else promise = pathToFiles(options).then(async (files) => {
|
|
758
|
+
commanders = await getFileValues(files, (item) => !!item && item instanceof Commander);
|
|
759
|
+
});
|
|
760
|
+
return middleware.console({
|
|
761
|
+
fn: async (ctx, next) => {
|
|
762
|
+
if (!commanders) await promise;
|
|
763
|
+
composeFn ||= compose(commanders.map((commander) => commander["toMiddleware"]()));
|
|
764
|
+
return composeFn(ctx, next);
|
|
765
|
+
},
|
|
766
|
+
help: {
|
|
767
|
+
async onDocument(_, { children }) {
|
|
768
|
+
await promise;
|
|
769
|
+
await children(commanders.map((commander) => commander["toMiddleware"]()));
|
|
770
|
+
},
|
|
771
|
+
async postDocument(_, { children }) {
|
|
772
|
+
await children(commanders.map((commander) => commander["toMiddleware"]()));
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
});
|
|
772
776
|
};
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
};
|
|
776
|
-
export {
|
|
777
|
-
Builder,
|
|
778
|
-
Commander,
|
|
779
|
-
ConsoleApp,
|
|
780
|
-
ConsoleContext,
|
|
781
|
-
ConsoleInput,
|
|
782
|
-
ConsoleMiddleware,
|
|
783
|
-
collectConsoleDocument,
|
|
784
|
-
commanders,
|
|
785
|
-
options,
|
|
786
|
-
scriptName,
|
|
787
|
-
terminal,
|
|
788
|
-
version
|
|
777
|
+
const isCommanders = (options) => {
|
|
778
|
+
return Array.isArray(options) && options.some((item) => item instanceof Commander);
|
|
789
779
|
};
|
|
780
|
+
export { Builder, Commander, ConsoleApp, ConsoleContext, ConsoleError, ConsoleInput, ConsoleMiddleware, collectConsoleDocument, commanders, options, scriptName, terminal, version };
|
|
781
|
+
|
|
790
782
|
//# sourceMappingURL=index.js.map
|