mixcli 3.2.0 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LiteEvent } from 'flex-tools/events/liteEvent';
2
- import { Option, Command } from 'commander';
2
+ import { OptionValues, Option, Command } from 'commander';
3
3
  import { PromptObject } from 'prompts';
4
4
  import * as flex_tools__ from 'flex-tools/*';
5
5
 
@@ -7,6 +7,8 @@ type PromptType = "text" | "password" | "invisible" | "number" | "confirm" | "li
7
7
  type PromptParam = 'auto' | boolean | PromptType | PromptObject;
8
8
  type InputPromptParam = PromptParam | ((value: any) => PromptParam) | boolean;
9
9
  type PromptParamDefaultValue = string | boolean | string[];
10
+ declare const promptTypeMap: Record<string, string>;
11
+ declare const supportedPromptTypes: string[];
10
12
  interface PromptChoice {
11
13
  title: string;
12
14
  value?: any;
@@ -14,128 +16,112 @@ interface PromptChoice {
14
16
  selected?: boolean | undefined;
15
17
  description?: string | undefined;
16
18
  }
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
- }
19
+ type PromptParams = Omit<PromptObject, 'name'> | PromptType | boolean | 'auto' | undefined;
38
20
  /**
39
21
  * 负责生成prompt对象
40
22
  *
41
23
  */
42
- declare class PromptManager {
43
- args: InputPromptParam;
44
- private _promptable;
45
- constructor(promptable: IPromptable, promptArgs?: InputPromptParam);
24
+ declare class MixOptionPrompt {
25
+ cliOption: MixOption;
26
+ params?: PromptParams;
27
+ constructor(cliOption: MixOption, promptParams?: PromptParams);
46
28
  /**
47
29
  * 返回输入的是否是有效的prompt类型
48
30
  * @param type
49
31
  * @returns
50
32
  */
51
- isValid(type: any): boolean;
33
+ isValidPromptType(type: any): boolean;
52
34
  /**
53
35
  * 推断是否需要提示
54
36
  *
37
+ * 1. 显式指定prompt=true或者提示类型,或者提示对象,则需要提示
38
+ *
39
+ *
55
40
  */
56
- isNeed(input: any, defaultValue?: any): boolean;
41
+ isNeedPrompt(input: any, defaultValue?: any): boolean;
42
+ private _getChoices;
57
43
  /**
58
- * 返回生成prompt对象
44
+ * 自动推断prompt类型
45
+ *
46
+ *
59
47
  *
60
48
  * @param inputValue 从命令行输入的值
61
49
  */
62
- get(inputValue?: any): PromptObject<string> | undefined;
50
+ infer(inputValue?: any): PromptType;
63
51
  /**
64
- * 推断prompt类型
52
+ * 返回生成prompt对象
65
53
  *
66
54
  * @param inputValue 从命令行输入的值
67
55
  */
68
- infer(inputValue?: any): string;
56
+ get(inputValue?: any): PromptObject<string> | undefined;
69
57
  }
70
58
 
71
- interface MixedOptionParams extends IPromptableOptions {
72
- hidden?: boolean;
59
+ interface MixedOptionParams {
60
+ required?: boolean;
61
+ optional?: boolean;
62
+ variadic?: boolean;
63
+ mandatory?: boolean;
64
+ negate?: boolean;
65
+ default?: any;
73
66
  defaultDescription?: string;
74
67
  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
- };
68
+ argParser?: unknown;
69
+ implies?: OptionValues;
70
+ envVar?: string;
71
+ parseArg?: <T>(value: string, previous: T) => T;
72
+ hidden?: boolean;
73
+ choices?: (string | PromptChoice)[] | ((pre: any, answers: any) => (string | PromptChoice)[]);
74
+ validate?: (value: any) => boolean;
75
+ preset?: unknown;
76
+ prompt?: PromptParams;
82
77
  }
83
- declare class MixOption extends Option implements IPromptable {
78
+ declare class MixOption extends Option {
79
+ params?: MixedOptionParams | undefined;
84
80
  __MIX_OPTION__: boolean;
85
- prompt?: PromptManager;
86
- promptChoices?: PromptChoice[];
87
- private _validate?;
88
- constructor(flags: string, description?: string | undefined, optsOrDefault?: any);
89
- validate(value: any): boolean;
90
- choices(values: (PromptChoice | string)[]): void;
91
- private resetChoices;
92
- addChoice(value: PromptChoice | string): void;
93
- removeChoice(value: any): void;
94
- clearChoice(): void;
81
+ prompt?: MixOptionPrompt;
82
+ constructor(flags: string, description: string, params?: MixedOptionParams | undefined);
83
+ private _setOption;
95
84
  /**
96
85
  * 返回选项的提示对象
97
86
  *
98
87
  * @remarks
99
- *
100
- *
101
- *
102
88
  * @param inputValue
103
89
  * @returns
104
90
  */
105
91
  getPrompt(inputValue?: any): PromptObject | undefined;
106
92
  }
107
93
 
108
- type ICommandHookListener = ({ args, options, command, }: {
94
+ type IMixCommandHookListener = ({ args, options, command, }: {
109
95
  args: any[];
110
96
  options: Record<string, any>;
111
97
  command: MixCommand;
112
98
  }) => void | Promise<void>;
113
- type BeforeCommandHookListener = ({ args, options, command, }: {
99
+ type BeforeMixCommandHookListener = ({ args, options, command, }: {
114
100
  args: any[];
115
101
  options: Record<string, any>;
116
102
  command: MixCommand;
117
103
  }) => void | Promise<void>;
118
- type AfterCommandHookListener = ({ value, args, options, command, }: {
104
+ type AfterMixCommandHookListener = ({ value, args, options, command, }: {
119
105
  value: any;
120
106
  args: any[];
121
107
  options: Record<string, any>;
122
108
  command: MixCommand;
123
109
  }) => void | Promise<void>;
124
- interface ActionOptions {
110
+ interface MixActionOptions {
125
111
  id: string;
126
112
  at: "replace" | "before" | "after" | "preappend" | "append" | number;
127
113
  enhance: boolean;
128
114
  }
129
- interface ActionRegistry extends Omit<ActionOptions, "at"> {
115
+ interface MixActionRegistry extends Omit<MixActionOptions, "at"> {
130
116
  fn: Function;
131
117
  }
132
- type OriginalAction = (...args: any[]) => void | Promise<void>;
133
- type EnhanceAction = ({ args, options, value, command, }: {
118
+ type MixOriginalAction = (...args: any[]) => any | Promise<void>;
119
+ type MixEnhanceAction = ({ args, options, value, command, }: {
134
120
  args: any[];
135
121
  options: Record<string, any>;
136
122
  value: any;
137
123
  command: MixCommand;
138
- }) => void | Promise<any>;
124
+ }) => any | Promise<any>;
139
125
  declare const BREAK: unique symbol;
140
126
  declare class MixCommand extends Command {
141
127
  __MIX_COMMAND__: boolean;
@@ -146,20 +132,18 @@ declare class MixCommand extends Command {
146
132
  private _actions;
147
133
  private _enable_prompts;
148
134
  constructor(name?: string);
149
- /**
150
- * 是否是根命令
151
- */
152
135
  get isRoot(): boolean;
153
- get actions(): ActionRegistry[];
154
- get beforeHooks(): [BeforeCommandHookListener, boolean][];
155
- get afterHooks(): [AfterCommandHookListener, boolean][];
136
+ get optionValues(): Record<string, any>;
137
+ get actions(): MixActionRegistry[];
138
+ get beforeHooks(): [BeforeMixCommandHookListener, boolean][];
139
+ get afterHooks(): [AfterMixCommandHookListener, boolean][];
156
140
  get fullname(): string;
157
141
  /**
158
142
  * 返回根命令
159
143
  */
160
- root(): MixCommand;
161
- action(fn: EnhanceAction, options: ActionOptions): this;
162
- action(fn: OriginalAction): this;
144
+ root(): any;
145
+ action(fn: MixEnhanceAction, options: MixActionOptions): this;
146
+ action(fn: MixOriginalAction): this;
163
147
  /**
164
148
  * 读取命令配置值,包括父命令提供的配置选项
165
149
  * @param command
@@ -188,7 +172,7 @@ declare class MixCommand extends Command {
188
172
  * @param scope =false时代表只在本命令执行,=true时代表在本命令及其子命令执行
189
173
  * @returns
190
174
  */
191
- before(listener: BeforeCommandHookListener, scope?: boolean): this;
175
+ before(listener: BeforeMixCommandHookListener, scope?: boolean): this;
192
176
  private executeBeforeHooks;
193
177
  /**
194
178
  * 添加一个After钩子
@@ -196,7 +180,7 @@ declare class MixCommand extends Command {
196
180
  * @param scope =false时代表只在本命令执行,=true时代表在本命令及其子命令执行
197
181
  * @returns
198
182
  */
199
- after(listener: AfterCommandHookListener, scope?: boolean): this;
183
+ after(listener: AfterMixCommandHookListener, scope?: boolean): this;
200
184
  private executeAfterHooks;
201
185
  private preActionHook;
202
186
  private isEnablePrompts;
@@ -204,14 +188,14 @@ declare class MixCommand extends Command {
204
188
  * 生成选项自动提示
205
189
  *
206
190
  * @remarks
207
- * FlexCli要求所有未提供默认值的Option自动生成提示
191
+ * 要求所有未提供默认值的Option自动生成提示
208
192
  *
209
193
  * - 未提供默认值,并且是必选的参数Option
210
194
  * - 指定了choices但未提供有效值的Option
211
195
  *
212
196
  */
213
197
  private generateAutoPrompts;
214
- option(flags: string, description?: string | undefined, defaultValue?: any): this;
198
+ option(flags: string, description: string, options?: MixedOptionParams): this;
215
199
  /**
216
200
  * 添加提示
217
201
  *
@@ -219,9 +203,7 @@ declare class MixCommand extends Command {
219
203
  *
220
204
  * 添加一些自定义提示
221
205
  *
222
- *
223
206
  * @param questions
224
- * @param show 是否显示提示信息,auto表示只有在用户没有提供option的值时才显示提示信息,always表示总是显示提示信息,never表示不显示提示信息
225
207
  * @returns
226
208
  */
227
209
  prompt(questions: PromptObject | PromptObject[]): this;
@@ -239,6 +221,9 @@ declare class MixCommand extends Command {
239
221
  * 禁用/启用所有提示
240
222
  */
241
223
  disablePrompts(): this;
224
+ /**
225
+ * 启用所有提示
226
+ */
242
227
  enablePrompts(): this;
243
228
  }
244
229
 
@@ -261,7 +246,7 @@ type MixCliCommand = (cli: MixCli) => MixCommand | MixCommand[] | void;
261
246
  type MixCliEvents = "register";
262
247
  declare class MixCli extends LiteEvent<any, MixCliEvents> {
263
248
  options: Required<MixCliOptions>;
264
- root: Command;
249
+ root: MixCommand;
265
250
  private findSignals;
266
251
  constructor(options?: MixCliOptions);
267
252
  get context(): Record<string, any>;
@@ -331,12 +316,6 @@ declare class MixCli extends LiteEvent<any, MixCliEvents> {
331
316
  * 运行命令行程序
332
317
  */
333
318
  run(): void;
334
- /**
335
- * 创建一个命令
336
- *
337
- *
338
- */
339
- create(): void;
340
319
  }
341
320
 
342
321
  /**
@@ -378,7 +357,7 @@ declare function addBuiltInOptions(command: any): void;
378
357
  * 是否命令行中包含了--debug-cli选项
379
358
  */
380
359
  declare function isDebug(): boolean;
381
- declare function isEnablePrompts(): boolean;
360
+ declare function isDisablePrompts(): boolean;
382
361
  /**
383
362
  * 打印调试信息
384
363
  * @param message
@@ -389,13 +368,6 @@ declare const fileExists: flex_tools__.AsyncFunction;
389
368
  declare const readFile: flex_tools__.AsyncFunction;
390
369
  declare const writeFile: flex_tools__.AsyncFunction;
391
370
  declare const mkdir: flex_tools__.AsyncFunction;
392
- /**
393
- * 基于artTemplate模板生成文件
394
- *
395
- * @param {*} tmplFile
396
- * @param {*} vars
397
- */
398
- declare function createFileByTemplate(targetFile: string, tmplFile: string, vars?: Record<string, any>): Promise<string>;
399
371
  /**
400
372
  * 创建目录
401
373
  *
@@ -410,7 +382,8 @@ declare function mkDirs(dirs: string[], { callback, base }: {
410
382
  base?: string;
411
383
  }): Promise<void>;
412
384
  declare function showError(e: any): void;
385
+ declare function hyphenToCamelCase(str: string): string;
413
386
  declare function getId(): string;
414
387
  declare function importModule(file: string): Promise<any>;
415
388
 
416
- export { type ActionOptions, type ActionRegistry, type AfterCommandHookListener, BREAK, type BeforeCommandHookListener, type EnhanceAction, type ICommandHookListener, MixCli, type MixCliCommand, type MixCliEvents, type MixCliOptions, MixCommand, MixOption, type MixedOptionParams, type OriginalAction, addBuiltInOptions, createFileByTemplate, fileExists, fixIndent, getId, importModule, isDebug, isEnablePrompts, mkDirs, mkdir, outputDebug, outputStr, readFile, showError, writeFile };
389
+ export { type AfterMixCommandHookListener, BREAK, type BeforeMixCommandHookListener, type IMixCommandHookListener, type InputPromptParam, type MixActionOptions, type MixActionRegistry, MixCli, type MixCliCommand, type MixCliEvents, type MixCliOptions, MixCommand, type MixEnhanceAction, MixOption, MixOptionPrompt, type MixOriginalAction, type MixedOptionParams, type PromptChoice, type PromptParam, type PromptParamDefaultValue, type PromptParams, type PromptType, addBuiltInOptions, fileExists, fixIndent, getId, hyphenToCamelCase, importModule, isDebug, isDisablePrompts, mkDirs, mkdir, outputDebug, outputStr, promptTypeMap, readFile, showError, supportedPromptTypes, writeFile };