mixcli 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,410 @@
1
+ import { LiteEvent } from 'flex-tools/events/liteEvent';
2
+ import { Option, Command } from 'commander';
3
+ import { PromptObject } from 'prompts';
4
+ import * as flex_tools from 'flex-tools';
5
+
6
+ type PromptType = "text" | "password" | "invisible" | "number" | "confirm" | "list" | "toggle" | "select" | "multiselect" | "autocomplete" | "date" | "autocompleteMultiselect";
7
+ type PromptParam = 'auto' | boolean | PromptType | PromptObject;
8
+ type InputPromptParam = PromptParam | ((value: any) => PromptParam) | boolean;
9
+ type PromptParamDefaultValue = string | boolean | string[];
10
+ interface PromptChoice {
11
+ title: string;
12
+ value?: any;
13
+ disabled?: boolean | undefined;
14
+ selected?: boolean | undefined;
15
+ description?: string | undefined;
16
+ }
17
+ interface IPromptableOptions {
18
+ required?: boolean;
19
+ optional?: boolean;
20
+ default?: PromptParamDefaultValue;
21
+ choices?: (PromptChoice | any)[];
22
+ prompt?: InputPromptParam;
23
+ validate?: (value: any) => boolean;
24
+ }
25
+ interface IPromptable {
26
+ name(): string;
27
+ description?: string;
28
+ flags: string;
29
+ promptChoices?: PromptChoice[];
30
+ argChoices?: string[];
31
+ variadic?: boolean;
32
+ defaultValue?: PromptParamDefaultValue;
33
+ input?: any;
34
+ required?: boolean;
35
+ validate?: (value: any) => boolean;
36
+ getPrompt(inputValue?: any): PromptObject | undefined;
37
+ }
38
+ /**
39
+ * 负责生成prompt对象
40
+ *
41
+ */
42
+ declare class PromptManager {
43
+ args: InputPromptParam;
44
+ private _promptable;
45
+ constructor(promptable: IPromptable, promptArgs?: InputPromptParam);
46
+ /**
47
+ * 返回输入的是否是有效的prompt类型
48
+ * @param type
49
+ * @returns
50
+ */
51
+ isValid(type: any): boolean;
52
+ /**
53
+ * 推断是否需要提示
54
+ *
55
+ */
56
+ isNeed(input: any, defaultValue?: any): boolean;
57
+ /**
58
+ * 返回生成prompt对象
59
+ *
60
+ * @param inputValue 从命令行输入的值
61
+ */
62
+ get(inputValue?: any): PromptObject<string> | undefined;
63
+ /**
64
+ * 推断prompt类型
65
+ *
66
+ * @param inputValue 从命令行输入的值
67
+ */
68
+ infer(inputValue?: any): string;
69
+ }
70
+
71
+ interface MixedOptionParams extends IPromptableOptions {
72
+ hidden?: boolean;
73
+ defaultDescription?: string;
74
+ conflicts?: string | string[];
75
+ env?: string;
76
+ argParser?: <T>(value: string, previous: T) => T;
77
+ hideHelp?: boolean;
78
+ mandatory?: boolean;
79
+ implies?: {
80
+ [key: string]: any;
81
+ };
82
+ }
83
+ declare class MixOption extends Option implements IPromptable {
84
+ prompt?: PromptManager;
85
+ promptChoices?: PromptChoice[];
86
+ private _validate?;
87
+ constructor(flags: string, description?: string | undefined, optsOrDefault?: any);
88
+ validate(value: any): boolean;
89
+ choices(values: (PromptChoice | string)[]): void;
90
+ private resetChoices;
91
+ addChoice(value: PromptChoice | string): void;
92
+ removeChoice(value: any): void;
93
+ clearChoice(): void;
94
+ /**
95
+ * 返回选项的提示对象
96
+ *
97
+ * @remarks
98
+ *
99
+ *
100
+ *
101
+ * @param inputValue
102
+ * @returns
103
+ */
104
+ getPrompt(inputValue?: any): PromptObject | undefined;
105
+ }
106
+
107
+ type ICommandHookListener = ({ args, options, command, }: {
108
+ args: any[];
109
+ options: Record<string, any>;
110
+ command: MixCommand;
111
+ }) => void | Promise<void>;
112
+ type BeforeCommandHookListener = ({ args, options, command, }: {
113
+ args: any[];
114
+ options: Record<string, any>;
115
+ command: MixCommand;
116
+ }) => void | Promise<void>;
117
+ type AfterCommandHookListener = ({ value, args, options, command, }: {
118
+ value: any;
119
+ args: any[];
120
+ options: Record<string, any>;
121
+ command: MixCommand;
122
+ }) => void | Promise<void>;
123
+ interface ActionOptions {
124
+ id: string;
125
+ at: "replace" | "before" | "after" | "preappend" | "append" | number;
126
+ enhance: boolean;
127
+ }
128
+ interface ActionRegistry extends Omit<ActionOptions, "at"> {
129
+ fn: Function;
130
+ }
131
+ type OriginalAction = (...args: any[]) => void | Promise<void>;
132
+ type EnhanceAction = ({ args, options, value, command, }: {
133
+ args: any[];
134
+ options: Record<string, any>;
135
+ value: any;
136
+ command: MixCommand;
137
+ }) => void | Promise<any>;
138
+ declare const BREAK: unique symbol;
139
+ declare class MixCommand extends Command {
140
+ private _beforeHooks;
141
+ private _afterHooks;
142
+ private _customPrompts;
143
+ private _optionValues;
144
+ private _actions;
145
+ private _enable_prompts;
146
+ constructor(name?: string);
147
+ /**
148
+ * 是否是根命令
149
+ */
150
+ get isRoot(): boolean;
151
+ get actions(): ActionRegistry[];
152
+ get beforeHooks(): [BeforeCommandHookListener, boolean][];
153
+ get afterHooks(): [AfterCommandHookListener, boolean][];
154
+ get fullname(): string;
155
+ /**
156
+ * 返回根命令
157
+ */
158
+ root(): MixCommand;
159
+ action(fn: EnhanceAction, options: ActionOptions): this;
160
+ action(fn: OriginalAction): this;
161
+ /**
162
+ * 读取命令配置值,包括父命令提供的配置选项
163
+ * @param command
164
+ */
165
+ private getOptionValues;
166
+ /**
167
+ * 本函数在运行时子类进行action生成该命令的action
168
+ */
169
+ private getWrapperedAction;
170
+ /**
171
+ * 向上查找所有祖先命令
172
+ */
173
+ private getAncestorCommands;
174
+ /***
175
+ * 将所有actions包装成一个链式调用的函数
176
+ */
177
+ private wrapperChainActions;
178
+ /**
179
+ * 当传入--work-dirs时用来处理工作目录
180
+ */
181
+ private wrapperWorkDirsAction;
182
+ getOption(name: string): MixOption;
183
+ /**
184
+ * 添加一个Before钩子
185
+ * @param listener
186
+ * @param scope =false时代表只在本命令执行,=true时代表在本命令及其子命令执行
187
+ * @returns
188
+ */
189
+ before(listener: BeforeCommandHookListener, scope?: boolean): this;
190
+ private executeBeforeHooks;
191
+ /**
192
+ * 添加一个After钩子
193
+ * @param listener
194
+ * @param scope =false时代表只在本命令执行,=true时代表在本命令及其子命令执行
195
+ * @returns
196
+ */
197
+ after(listener: AfterCommandHookListener, scope?: boolean): this;
198
+ private executeAfterHooks;
199
+ private preActionHook;
200
+ private isEnablePrompts;
201
+ /**
202
+ * 生成选项自动提示
203
+ *
204
+ * @remarks
205
+ * FlexCli要求所有未提供默认值的Option自动生成提示
206
+ *
207
+ * - 未提供默认值,并且是必选的参数Option
208
+ * - 指定了choices但未提供有效值的Option
209
+ *
210
+ */
211
+ private generateAutoPrompts;
212
+ option(flags: string, description?: string | undefined, defaultValue?: any): this;
213
+ /**
214
+ * 添加提示
215
+ *
216
+ * @remarks
217
+ *
218
+ * 添加一些自定义提示
219
+ *
220
+ *
221
+ * @param questions
222
+ * @param show 是否显示提示信息,auto表示只有在用户没有提供option的值时才显示提示信息,always表示总是显示提示信息,never表示不显示提示信息
223
+ * @returns
224
+ */
225
+ prompt(questions: PromptObject | PromptObject[]): this;
226
+ /**
227
+ *
228
+ * 选择命令并执行
229
+ *
230
+ * @remorks
231
+ *
232
+ * 当命令具有多个子命令时,并且没有提供默认子命令时,提示用户选择一个子命令
233
+ *
234
+ */
235
+ selectCommands(): Promise<void>;
236
+ /**
237
+ * 禁用/启用所有提示
238
+ */
239
+ disablePrompts(): this;
240
+ enablePrompts(): this;
241
+ }
242
+
243
+ interface MixCliOptions {
244
+ name: string;
245
+ title?: string | (string | boolean | number)[];
246
+ description?: string;
247
+ version?: string;
248
+ logo?: string;
249
+ before?: (thisCommand: Command, actionCommand: Command) => void;
250
+ after?: (thisCommand: Command, actionCommand: Command) => void;
251
+ include?: string | RegExp | string[] | RegExp[];
252
+ exclude?: string | RegExp | string[] | RegExp[];
253
+ cliDir?: string;
254
+ context?: Record<string, any>;
255
+ prompt?: 'auto' | boolean;
256
+ }
257
+ type MixCliCommand = (cli: MixCli) => MixCommand | MixCommand[] | void;
258
+ type MixCliEvents = "register";
259
+ declare class MixCli extends LiteEvent<any, MixCliEvents> {
260
+ options: Required<MixCliOptions>;
261
+ root: Command;
262
+ private findSignals;
263
+ constructor(options?: MixCliOptions);
264
+ get context(): Record<string, any>;
265
+ get name(): string;
266
+ /**
267
+ * 是否禁用了所有的交互提示
268
+ */
269
+ get isDisabledPrompts(): any;
270
+ /**
271
+ * 扫描当前工程的依赖,加载匹配include的依赖下的命令
272
+ */
273
+ private installCommands;
274
+ /**
275
+ * 创建根命令
276
+ *
277
+ */
278
+ private createRootCommand;
279
+ /**
280
+ * 添加帮助选项
281
+ *
282
+ * @param text 帮助文本
283
+ * @param position 显示位置,可选值:before|after|beforeAll|afterAll
284
+ * @param fixIndent 是否自动修正缩进,如果为true,则会自动修正缩进,当显示多行时文本时,会自动修正缩进
285
+ *
286
+ */
287
+ addHelp(text: string, { pos, alignIndent }: {
288
+ pos: 'before' | 'after' | 'beforeAll' | 'afterAll';
289
+ alignIndent?: boolean | number;
290
+ }): void;
291
+ /**
292
+ * 注册一个命令
293
+ * @param cmd
294
+ */
295
+ register(cmd: MixCliCommand): void;
296
+ hasCommand(name: string): boolean;
297
+ /**
298
+ * 根据命令名称查找命令
299
+ *
300
+ * @remarks
301
+ *
302
+ * find("dev")
303
+ * find("dev.microservice") 支持多级命令
304
+ * find("abc",DevCommand) 允许指定从DevCommand下开始查找abc命令
305
+ *
306
+ * @param name
307
+ */
308
+ get(name: string): MixCommand | undefined;
309
+ /**
310
+ * 查找一个命令
311
+ *
312
+ * 如果命令不存在,则等待命令注册后再返回
313
+ *
314
+ * 在多包场景下,如果命令在其他包中注册并且该包中的命令还没注册,则会等待命令注册后再返回
315
+ *
316
+ * @param name
317
+ * @returns
318
+ */
319
+ find(name: string): Promise<MixCommand | undefined>;
320
+ /**
321
+ * 判断命令是否存在
322
+ *
323
+ * @param name
324
+ * @returns
325
+ */
326
+ exists(name: string): boolean;
327
+ /**
328
+ * 运行命令行程序
329
+ */
330
+ run(): void;
331
+ /**
332
+ * 创建一个命令
333
+ *
334
+ *
335
+ */
336
+ create(): void;
337
+ }
338
+
339
+ /**
340
+ *
341
+ * 在控制台输出一个字符串
342
+ * 本方法会将字符串中的每一行空格去掉
343
+ *
344
+ * @remarks
345
+ *
346
+ * outputStr(String.raw`
347
+ * a
348
+ * b`)
349
+ *
350
+ * 会输出
351
+ * a
352
+ * b
353
+ *
354
+ * 此功能可以用于输出多行字符串时,保持代码的缩进格式,而不会影响输出结果
355
+ *
356
+ * @param str : 要输出的字符串
357
+ * @param vars : 用于替换字符串中的变量
358
+ *
359
+ */
360
+ declare function outputStr(str: string, vars?: Record<string, any> | any[]): void;
361
+ /**
362
+ * 修正多行字符串的缩进
363
+ *
364
+ * @param text
365
+ * @param indent
366
+ * @returns
367
+ */
368
+ declare function fixIndent(text: string, indent?: boolean | number): string;
369
+ /**
370
+ * 增加内置选项
371
+ * @param command
372
+ */
373
+ declare function addBuiltInOptions(command: any): void;
374
+ /**
375
+ * 是否命令行中包含了--debug-cli选项
376
+ */
377
+ declare function isDebug(): boolean;
378
+ declare function isEnablePrompts(): boolean;
379
+ /**
380
+ * 打印调试信息
381
+ * @param message
382
+ * @param args
383
+ */
384
+ declare function outputDebug(message: string, ...args: any[]): void;
385
+ declare const fileExists: flex_tools.AsyncFunction;
386
+ declare const readFile: flex_tools.AsyncFunction;
387
+ declare const writeFile: flex_tools.AsyncFunction;
388
+ declare const mkdir: flex_tools.AsyncFunction;
389
+ /**
390
+ * 基于artTemplate模板生成文件
391
+ *
392
+ * @param {*} tmplFile
393
+ * @param {*} vars
394
+ */
395
+ declare function createFileByTemplate(targetFile: string, tmplFile: string, vars?: Record<string, any>): Promise<string>;
396
+ /**
397
+ * 创建目录
398
+ *
399
+ *
400
+ *
401
+ * @param {String[]} dirs 要创建的目录列表,类型为字符串数组
402
+ * @param callback 创建目录过程中的回调函数,类型为异步函数,接收一个参数 dir,表示当前正在创建的目录
403
+ * @returns 该函数返回一个 Promise 对象,表示创建目录的操作是否完成
404
+ */
405
+ declare function mkDirs(dirs: string[], { callback, base }: {
406
+ callback?: Function;
407
+ base?: string;
408
+ }): Promise<void>;
409
+
410
+ export { ActionOptions, ActionRegistry, AfterCommandHookListener, BREAK, BeforeCommandHookListener, EnhanceAction, ICommandHookListener, MixCli, MixCliCommand, MixCliEvents, MixCliOptions, MixCommand, MixOption, MixedOptionParams, OriginalAction, addBuiltInOptions, createFileByTemplate, fileExists, fixIndent, isDebug, isEnablePrompts, mkDirs, mkdir, outputDebug, outputStr, readFile, writeFile };