@breadc/core 1.0.0-beta.1
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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/index.d.mts +696 -0
- package/dist/index.mjs +1045 -0
- package/package.json +54 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,696 @@
|
|
|
1
|
+
//#region src/utils/types.d.ts
|
|
2
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
3
|
+
type IsEqual<T, U> = (<G>() => G extends T ? 1 : 0) extends (<G>() => G extends U ? 1 : 0) ? true : false;
|
|
4
|
+
type Lowercase = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
|
|
5
|
+
type Uppercase = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
|
|
6
|
+
type Letter = Lowercase | Uppercase;
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/breadc/types/infer.d.ts
|
|
9
|
+
/**
|
|
10
|
+
* Infer option raw name
|
|
11
|
+
*
|
|
12
|
+
* Examples:
|
|
13
|
+
* + const t1: InferOptionRawName<'--option' | '--hello'> = 'hello'
|
|
14
|
+
* + const t2: InferOptionRawName<'-r, --root'> = 'root'
|
|
15
|
+
* + const t3: InferOptionRawName<'--page-index'> = 'page-index'
|
|
16
|
+
*/
|
|
17
|
+
type InferOptionRawName<S extends string> = S extends `-${Letter}, --no-${infer R} <${string}>` ? R : S extends `-${Letter}, --no-${infer R} [${string}]` ? R : S extends `-${Letter}, --no-${infer R}` ? R : S extends `-${Letter}, --${infer R} <${string}>` ? R : S extends `-${Letter}, --${infer R} [${string}]` ? R : S extends `-${Letter}, --${infer R}` ? R : S extends `--no-${string} <${string}>` ? never : S extends `--no-${string} [${string}]` ? never : S extends `--no-${infer R}` ? R : S extends `--${infer R} <${string}>` ? R : S extends `--${infer R} [${string}]` ? R : S extends `--${infer R}` ? R : string;
|
|
18
|
+
/**
|
|
19
|
+
* Infer camel case option name
|
|
20
|
+
*/
|
|
21
|
+
type InferOptionName<T extends string> = InferOptionRawName<T> extends `${infer P1}-${infer P2}-${infer P3}` ? `${P1}${Capitalize<P2>}${Capitalize<P3>}` : InferOptionRawName<T> extends `${infer P1}-${infer P2}` ? `${P1}${Capitalize<P2>}` : InferOptionRawName<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Infer the raw option type: boolean or string or string[]
|
|
24
|
+
*/
|
|
25
|
+
type InferOptionRawType<S extends string> = S extends `-${Letter}, --${string} <${string}>` ? undefined | string : S extends `-${Letter}, --${string} [...${string}]` ? string[] : S extends `-${Letter}, --${string} [${string}]` ? false | true | string : S extends `-${Letter}, --${string}` ? boolean : S extends `--${string} <${string}>` ? undefined | string : S extends `--${string} [...${string}]` ? string[] : S extends `--${string} [${string}]` ? false | true | string : S extends `--${string}` ? boolean : undefined | boolean | string | string[];
|
|
26
|
+
/**
|
|
27
|
+
* Infer the raw option type: boolean or string or string[]
|
|
28
|
+
*/
|
|
29
|
+
type InferOptionInitialType<S extends string> = S extends `-${Letter}, --${string} <${string}>` ? undefined | string : S extends `-${Letter}, --${string} [...${string}]` ? string[] : S extends `-${Letter}, --${string} [${string}]` ? string : S extends `-${Letter}, --${string}` ? boolean : S extends `--${string} <${string}>` ? undefined | string : S extends `--${string} [...${string}]` ? string[] : S extends `--${string} [${string}]` ? undefined | string : S extends `--${string}` ? boolean : boolean | string | string[];
|
|
30
|
+
type NonTrueNullable<T> = IsEqual<T, false | true | string> extends true ? T & string & {} : T & {};
|
|
31
|
+
/**
|
|
32
|
+
* Infer the option type with config
|
|
33
|
+
*/
|
|
34
|
+
type InferOptionType<S extends string, I extends InferOptionInitialType<S>, C extends OptionInit<S, I>> = C['default'] extends {} ? C['cast'] extends ((...args: any[]) => infer R) ? IsEqual<R, C['default']> extends true ? R : never : C['default'] | (C['initial'] extends {} ? C['initial'] | NonTrueNullable<InferOptionRawType<S>> : NonTrueNullable<InferOptionRawType<S>>) : C['cast'] extends ((...args: any[]) => infer R) ? R : C['initial'] extends {} ? C['initial'] | NonTrueNullable<InferOptionRawType<S>> : InferOptionRawType<S>;
|
|
35
|
+
/**
|
|
36
|
+
* Infer option information
|
|
37
|
+
*/
|
|
38
|
+
type InferOption<S extends string, I extends InferOptionInitialType<S>, C extends OptionInit<S, I, unknown>> = { [k in InferOptionName<S>]: InferOptionType<S, I, C> };
|
|
39
|
+
/**
|
|
40
|
+
* Infer the raw argument type: required or optional or spread
|
|
41
|
+
*/
|
|
42
|
+
type InferArgumentRawType<S extends string> = S extends `<${string}>` ? string : S extends `[...${string}]` ? string[] : S extends `[${string}]` ? undefined | string : undefined | string | string[];
|
|
43
|
+
/**
|
|
44
|
+
* Infer the argument type with config
|
|
45
|
+
*/
|
|
46
|
+
type InferArgumentType<S extends string, I extends InferArgumentRawType<S>, C extends ArgumentInit<S, I, unknown> | NonNullableArgumentInit<S, NonNullable<I>, unknown>> = C['default'] extends {} ? C['cast'] extends ((...args: any[]) => infer R) ? IsEqual<R, C['default']> extends true ? R : never : C['default'] | (C['initial'] extends {} ? C['initial'] | NonNullable<InferArgumentRawType<S>> : NonNullable<InferArgumentRawType<S>>) : C['cast'] extends ((...args: any[]) => infer R) ? R : C['initial'] extends {} ? C['initial'] | NonNullable<InferArgumentRawType<S>> : InferArgumentRawType<S>;
|
|
47
|
+
/**
|
|
48
|
+
* Infer the arguments type
|
|
49
|
+
*/
|
|
50
|
+
type InferArgumentsType<S extends string> = S extends `<${string}> ${infer U}` ? [string, ...InferArgumentsType1<U>] : S extends `[...${string}] ${string}` ? never : S extends `[${string}] ${infer U}` ? [undefined | string, ...InferArgumentsType2<U>] : S extends `${string} ${infer U}` ? InferArgumentsType<U> : S extends `<${string}>` ? [string] : S extends `[...${string}]` ? [string[]] : S extends `[${string}]` ? [undefined | string] : [];
|
|
51
|
+
type InferArgumentsType1<S extends string> = S extends `<${string}> ${infer U}` ? [string, ...InferArgumentsType<U>] : S extends `[...${string}] ${string}` ? never : S extends `[${string}] ${infer U}` ? [undefined | string, ...InferArgumentsType2<U>] : S extends `${string} ${string}` ? never : S extends `<${string}>` ? [string] : S extends `[...${string}]` ? [string[]] : S extends `[${string}]` ? [undefined | string] : S extends `${string}` ? never : [];
|
|
52
|
+
type InferArgumentsType2<S extends string> = S extends `<${string}> ${string}` ? never : S extends `[...${string}] ${string}` ? never : S extends `[${string}] ${infer U}` ? [undefined | string, ...InferArgumentsType2<U>] : S extends `${string} ${string}` ? never : S extends `<${string}>` ? never : S extends `[...${string}]` ? [string[]] : S extends `[${string}]` ? [undefined | string] : S extends `${string}` ? never : [];
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/breadc/types/init.d.ts
|
|
55
|
+
type BreadcInit = {
|
|
56
|
+
/**
|
|
57
|
+
* CLI app version
|
|
58
|
+
*/
|
|
59
|
+
version?: string;
|
|
60
|
+
/**
|
|
61
|
+
* CLI app description
|
|
62
|
+
*/
|
|
63
|
+
description?: string;
|
|
64
|
+
/**
|
|
65
|
+
* I18n language or custom i18n function
|
|
66
|
+
*
|
|
67
|
+
* @default 'en'
|
|
68
|
+
*/
|
|
69
|
+
/**
|
|
70
|
+
* Logger
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* Builtin command configuration
|
|
74
|
+
*/
|
|
75
|
+
builtin?: {
|
|
76
|
+
version?: boolean | {
|
|
77
|
+
/**
|
|
78
|
+
* @default '-v, --version'
|
|
79
|
+
*/
|
|
80
|
+
spec?: string;
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
*/
|
|
84
|
+
description?: string;
|
|
85
|
+
};
|
|
86
|
+
help?: boolean | {
|
|
87
|
+
/**
|
|
88
|
+
* @default '-h, --help'
|
|
89
|
+
*/
|
|
90
|
+
spec?: string;
|
|
91
|
+
/**
|
|
92
|
+
*
|
|
93
|
+
*/
|
|
94
|
+
description?: string;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
type OptionInit<Spec extends string, Initial extends InferOptionInitialType<Spec>, Cast extends unknown = unknown> = {
|
|
99
|
+
/**
|
|
100
|
+
* Option description
|
|
101
|
+
*/
|
|
102
|
+
description?: string;
|
|
103
|
+
/**
|
|
104
|
+
* Generate negated option
|
|
105
|
+
*/
|
|
106
|
+
negated?: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Overwrite the initial value of the corresponding matched option.
|
|
109
|
+
* - <required>: undefined
|
|
110
|
+
* - \[optional\]: false
|
|
111
|
+
* - \[...remaining\]: \[\]
|
|
112
|
+
*/
|
|
113
|
+
initial?: Initial;
|
|
114
|
+
/**
|
|
115
|
+
* Cast initial value to the result
|
|
116
|
+
*/
|
|
117
|
+
cast?: (value: Initial extends {} ? Initial : InferOptionRawType<Spec>) => Cast;
|
|
118
|
+
/**
|
|
119
|
+
* Default option value if it is not provided
|
|
120
|
+
*/
|
|
121
|
+
default?: Cast;
|
|
122
|
+
};
|
|
123
|
+
type NonNullableOptionInit<Spec extends string, Initial extends NonTrueNullable<InferOptionInitialType<Spec>>, Cast extends unknown = unknown> = {
|
|
124
|
+
/**
|
|
125
|
+
* Option description
|
|
126
|
+
*/
|
|
127
|
+
description?: string;
|
|
128
|
+
/**
|
|
129
|
+
* Generate corresponding negated option
|
|
130
|
+
*
|
|
131
|
+
* @default false
|
|
132
|
+
*/
|
|
133
|
+
negated?: InferOptionRawType<Spec> extends boolean ? boolean : never;
|
|
134
|
+
/**
|
|
135
|
+
* Overwrite the initial value of the corresponding matched option.
|
|
136
|
+
* - `--option`: `false`
|
|
137
|
+
* - `--option [optional]`: `false`
|
|
138
|
+
* - `--option <required>`: `undefined`
|
|
139
|
+
* - `--option [...spread]`: `[]`
|
|
140
|
+
*/
|
|
141
|
+
initial: Initial;
|
|
142
|
+
/**
|
|
143
|
+
* Cast initial value to the result
|
|
144
|
+
*/
|
|
145
|
+
cast?: (value: Initial extends {} ? Initial : InferOptionRawType<Spec>) => Cast;
|
|
146
|
+
/**
|
|
147
|
+
* Default option value when its value or initial value is not provided
|
|
148
|
+
*/
|
|
149
|
+
default?: Cast;
|
|
150
|
+
};
|
|
151
|
+
type GroupInit<Spec extends string> = {};
|
|
152
|
+
type CommandInit<Spec extends string> = {};
|
|
153
|
+
type ArgumentInit<Spec extends string, Initial extends InferArgumentRawType<Spec>, Cast extends unknown = unknown> = {
|
|
154
|
+
/**
|
|
155
|
+
* Overwrite the initial value of the corresponding matched option.
|
|
156
|
+
* - <required> : undefined
|
|
157
|
+
* - \[optional\] : undefined
|
|
158
|
+
* - \[...remaining\] : \[\]
|
|
159
|
+
*/
|
|
160
|
+
initial?: Initial;
|
|
161
|
+
/**
|
|
162
|
+
* Cast initial value to the result
|
|
163
|
+
*/
|
|
164
|
+
cast?: (value: Initial extends {} ? Initial : InferArgumentRawType<Spec>) => Cast;
|
|
165
|
+
/**
|
|
166
|
+
* Default argument value if it is not provided
|
|
167
|
+
*/
|
|
168
|
+
default?: Cast;
|
|
169
|
+
};
|
|
170
|
+
type NonNullableArgumentInit<Spec extends string, Initial extends NonNullable<InferArgumentRawType<Spec>>, Cast extends unknown = unknown> = {
|
|
171
|
+
/**
|
|
172
|
+
* Overwrite the initial value of the corresponding matched option.
|
|
173
|
+
* - <required> : undefined
|
|
174
|
+
* - \[optional\] : undefined
|
|
175
|
+
* - \[...remaining\] : \[\]
|
|
176
|
+
*/
|
|
177
|
+
initial: Initial;
|
|
178
|
+
/**
|
|
179
|
+
* Cast initial value to the result
|
|
180
|
+
*/
|
|
181
|
+
cast?: (value: Initial extends {} ? Initial : InferArgumentRawType<Spec>) => Cast;
|
|
182
|
+
/**
|
|
183
|
+
* Default argument value if it is not provided
|
|
184
|
+
*/
|
|
185
|
+
default?: Cast;
|
|
186
|
+
};
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/breadc/types/middleware.d.ts
|
|
189
|
+
/**
|
|
190
|
+
* Unknown command middleware
|
|
191
|
+
*
|
|
192
|
+
* @public
|
|
193
|
+
*/
|
|
194
|
+
type UnknownCommandMiddleware<Data extends {} = {}> = (context: Context<Data>) => Promise<any> | any;
|
|
195
|
+
/**
|
|
196
|
+
* Unknown option middleware
|
|
197
|
+
*
|
|
198
|
+
* @public
|
|
199
|
+
*/
|
|
200
|
+
type UnknownOptionMiddleware<Data extends {} = {}> = (context: Context<Data>, key: string, value: string | undefined) => MatchedUnknownOption | null | undefined;
|
|
201
|
+
interface ActionMiddlewareNextFn {
|
|
202
|
+
<NextData extends {} = {}>(nextContextData?: {
|
|
203
|
+
data?: NextData;
|
|
204
|
+
}): Promise<Context<NextData>>;
|
|
205
|
+
}
|
|
206
|
+
type InferMiddlewareNextFn<Fn extends ActionMiddlewareNextFn> = Awaited<ReturnType<Fn>>['data'];
|
|
207
|
+
/**
|
|
208
|
+
* Command action middleware
|
|
209
|
+
*
|
|
210
|
+
* @public
|
|
211
|
+
*/
|
|
212
|
+
type ActionMiddleware<Data extends {} = {}, NextFn extends ActionMiddlewareNextFn = ActionMiddlewareNextFn> = (context: Context<Data>, next: NextFn) => Promise<Context<InferMiddlewareNextFn<NextFn>>>;
|
|
213
|
+
type InferMiddlewareData<Middleware extends ActionMiddleware<any, ActionMiddlewareNextFn>> = Awaited<ReturnType<Middleware>>['data'];
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/breadc/types/internal.d.ts
|
|
216
|
+
/**
|
|
217
|
+
* @internal
|
|
218
|
+
*/
|
|
219
|
+
type InternalBreadc = Breadc<any, any> & {
|
|
220
|
+
/**
|
|
221
|
+
* @internal
|
|
222
|
+
*/
|
|
223
|
+
_init: BreadcInit;
|
|
224
|
+
/**
|
|
225
|
+
* @internal
|
|
226
|
+
*/
|
|
227
|
+
_commands: (InternalCommand | InternalGroup)[];
|
|
228
|
+
/**
|
|
229
|
+
* @internal
|
|
230
|
+
*/
|
|
231
|
+
_version?: InternalOption;
|
|
232
|
+
/**
|
|
233
|
+
* @internal
|
|
234
|
+
*/
|
|
235
|
+
_help?: InternalOption;
|
|
236
|
+
/**
|
|
237
|
+
* @internal
|
|
238
|
+
*/
|
|
239
|
+
_options: InternalOption[];
|
|
240
|
+
/**
|
|
241
|
+
* @internal
|
|
242
|
+
*/
|
|
243
|
+
_actionMiddlewares: ActionMiddleware<any, any>[];
|
|
244
|
+
/**
|
|
245
|
+
* @internal
|
|
246
|
+
*/
|
|
247
|
+
_unknownCommandMiddlewares: UnknownCommandMiddleware<any>[];
|
|
248
|
+
/**
|
|
249
|
+
* @internal
|
|
250
|
+
*/
|
|
251
|
+
_unknownOptionMiddlewares: UnknownOptionMiddleware<any>[];
|
|
252
|
+
};
|
|
253
|
+
type InternalGroup = Group<string, GroupInit<string>, any, any> & {
|
|
254
|
+
/**
|
|
255
|
+
* Mark whether it is a default command
|
|
256
|
+
*
|
|
257
|
+
* @internal
|
|
258
|
+
*/
|
|
259
|
+
_default?: false;
|
|
260
|
+
/**
|
|
261
|
+
* It is lazy built when running
|
|
262
|
+
*
|
|
263
|
+
* @internal
|
|
264
|
+
*/
|
|
265
|
+
_pieces: [string[]];
|
|
266
|
+
/**
|
|
267
|
+
* @internal
|
|
268
|
+
*/
|
|
269
|
+
_commands: InternalCommand[];
|
|
270
|
+
/**
|
|
271
|
+
* @internal
|
|
272
|
+
*/
|
|
273
|
+
_options: InternalOption[];
|
|
274
|
+
/**
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
_actionMiddlewares?: ActionMiddleware[];
|
|
278
|
+
/**
|
|
279
|
+
* @internal
|
|
280
|
+
*/
|
|
281
|
+
_unknownOptionMiddlewares?: UnknownOptionMiddleware<any>[];
|
|
282
|
+
};
|
|
283
|
+
type InternalCommand = Command & {
|
|
284
|
+
/**
|
|
285
|
+
* @internal
|
|
286
|
+
*/
|
|
287
|
+
_group?: InternalGroup;
|
|
288
|
+
/**
|
|
289
|
+
* @internal
|
|
290
|
+
*/
|
|
291
|
+
_aliases: string[];
|
|
292
|
+
/**
|
|
293
|
+
* Mark whether it is a default command
|
|
294
|
+
*
|
|
295
|
+
* @internal
|
|
296
|
+
*/
|
|
297
|
+
_default?: boolean;
|
|
298
|
+
/**
|
|
299
|
+
* @internal
|
|
300
|
+
*/
|
|
301
|
+
_pieces: string[][];
|
|
302
|
+
/**
|
|
303
|
+
* It is lazy built when running
|
|
304
|
+
*
|
|
305
|
+
* @internal
|
|
306
|
+
*/
|
|
307
|
+
_arguments: InternalArgument[];
|
|
308
|
+
/**
|
|
309
|
+
* @internal
|
|
310
|
+
*/
|
|
311
|
+
_options: InternalOption[];
|
|
312
|
+
/**
|
|
313
|
+
* @internal
|
|
314
|
+
*/
|
|
315
|
+
_actionMiddlewares?: ActionMiddleware<any, any>[];
|
|
316
|
+
/**
|
|
317
|
+
* @internal
|
|
318
|
+
*/
|
|
319
|
+
_unknownOptionMiddlewares?: UnknownOptionMiddleware<any>[];
|
|
320
|
+
/**
|
|
321
|
+
* @internal
|
|
322
|
+
*/
|
|
323
|
+
_actionFn?: Function;
|
|
324
|
+
};
|
|
325
|
+
type OptionType = 'boolean' | 'required' | 'optional' | 'spread';
|
|
326
|
+
type InternalOption = Option & {
|
|
327
|
+
type: OptionType;
|
|
328
|
+
long: string;
|
|
329
|
+
short?: string | undefined;
|
|
330
|
+
argument?: string | undefined;
|
|
331
|
+
};
|
|
332
|
+
type ArgumentType = 'required' | 'optional' | 'spread';
|
|
333
|
+
type InternalArgument = Argument & {
|
|
334
|
+
type: ArgumentType;
|
|
335
|
+
};
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/runtime/lexer.d.ts
|
|
338
|
+
declare class TokenStream {
|
|
339
|
+
private readonly args;
|
|
340
|
+
private readonly tokens;
|
|
341
|
+
private cursor;
|
|
342
|
+
constructor(args: string[]);
|
|
343
|
+
reset(): this;
|
|
344
|
+
/**
|
|
345
|
+
* Get the current arg token and advance the cursor
|
|
346
|
+
*
|
|
347
|
+
* @returns next arg
|
|
348
|
+
*/
|
|
349
|
+
next(): Token | undefined;
|
|
350
|
+
/**
|
|
351
|
+
* Move the cursor to preivous position
|
|
352
|
+
*/
|
|
353
|
+
prev(): void;
|
|
354
|
+
/**
|
|
355
|
+
* Peek the next arg and does not advance the cursor
|
|
356
|
+
*
|
|
357
|
+
* @returns next arg
|
|
358
|
+
*/
|
|
359
|
+
peek(): Token | undefined;
|
|
360
|
+
/**
|
|
361
|
+
* Return all remaining raw arguments, advancing the cursor to the end
|
|
362
|
+
*
|
|
363
|
+
* @returns all remaining raw arguments
|
|
364
|
+
*/
|
|
365
|
+
remaining(): Token[];
|
|
366
|
+
/**
|
|
367
|
+
* Return whether has consumed all the args
|
|
368
|
+
*
|
|
369
|
+
* @returns whether has consumed all the args
|
|
370
|
+
*/
|
|
371
|
+
get isEnd(): boolean;
|
|
372
|
+
[Symbol.iterator](): Generator<Token, void, unknown>;
|
|
373
|
+
}
|
|
374
|
+
declare class Token {
|
|
375
|
+
readonly text: string;
|
|
376
|
+
constructor(arg: string);
|
|
377
|
+
/**
|
|
378
|
+
* @returns raw arg string
|
|
379
|
+
*/
|
|
380
|
+
toRaw(): string;
|
|
381
|
+
/**
|
|
382
|
+
* @returns whether arg is empty
|
|
383
|
+
*/
|
|
384
|
+
get isEmpty(): boolean;
|
|
385
|
+
/**
|
|
386
|
+
* @returns whether arg looks like a stdio argument (`-`)
|
|
387
|
+
*/
|
|
388
|
+
get isStdio(): boolean;
|
|
389
|
+
/**
|
|
390
|
+
* @returns whether arg looks like an argument escape (`--`)
|
|
391
|
+
*/
|
|
392
|
+
get isEscape(): boolean;
|
|
393
|
+
/**
|
|
394
|
+
* @returns whether arg looks like a negative number (`-123`)
|
|
395
|
+
*/
|
|
396
|
+
get isNegativeNumber(): boolean;
|
|
397
|
+
/**
|
|
398
|
+
* @returns whether arg looks like a number (`123`, `-123`)
|
|
399
|
+
*/
|
|
400
|
+
get isNumber(): boolean;
|
|
401
|
+
/**
|
|
402
|
+
* @returns whether arg can treat as a long-flag
|
|
403
|
+
*/
|
|
404
|
+
get isLong(): boolean;
|
|
405
|
+
/**
|
|
406
|
+
* Treat as a long-flag, un-checked version
|
|
407
|
+
*
|
|
408
|
+
* @returns a long flag and its argument
|
|
409
|
+
*/
|
|
410
|
+
toLong(): [key: string, value: string | undefined];
|
|
411
|
+
/**
|
|
412
|
+
* Treat as a long-flag, checked version
|
|
413
|
+
*
|
|
414
|
+
* @returns a long flag and its argument
|
|
415
|
+
*/
|
|
416
|
+
checkToLong(): [key: string, value: string | undefined] | undefined;
|
|
417
|
+
/**
|
|
418
|
+
* @returns whether arg can treat as a long-flag
|
|
419
|
+
*/
|
|
420
|
+
get isShort(): boolean;
|
|
421
|
+
/**
|
|
422
|
+
* Treat as a short-flag, un-checked version
|
|
423
|
+
*
|
|
424
|
+
* @returns a short flag and its argument
|
|
425
|
+
*/
|
|
426
|
+
toShort(): [key: string, value: string | undefined];
|
|
427
|
+
/**
|
|
428
|
+
* Treat as a short-flag, checked version
|
|
429
|
+
*
|
|
430
|
+
* @returns a short flag and its argument
|
|
431
|
+
*/
|
|
432
|
+
checkToShort(): [key: string, value: string | undefined] | undefined;
|
|
433
|
+
get length(): number;
|
|
434
|
+
toString(): string;
|
|
435
|
+
[Symbol.iterator](): StringIterator<string>;
|
|
436
|
+
}
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region src/runtime/matched.d.ts
|
|
439
|
+
declare class MatchedArgument {
|
|
440
|
+
readonly argument: InternalArgument;
|
|
441
|
+
readonly token: Token | undefined;
|
|
442
|
+
dirty: boolean;
|
|
443
|
+
raw: string | string[] | undefined;
|
|
444
|
+
constructor(argument: Argument<string, any, any> | InternalArgument);
|
|
445
|
+
value<T = any>(): T;
|
|
446
|
+
accept(_context: Context, value: string | string[] | undefined): this;
|
|
447
|
+
}
|
|
448
|
+
declare class MatchedOption {
|
|
449
|
+
readonly option: InternalOption;
|
|
450
|
+
dirty: boolean;
|
|
451
|
+
raw: any;
|
|
452
|
+
constructor(option: Option<string, any, any> | InternalOption);
|
|
453
|
+
value<T = any>(): T;
|
|
454
|
+
accept(context: Context, long: string, text: string | undefined): this;
|
|
455
|
+
}
|
|
456
|
+
interface MatchedUnknownOption<T = any> {
|
|
457
|
+
name: string;
|
|
458
|
+
type?: OptionType;
|
|
459
|
+
value: T;
|
|
460
|
+
}
|
|
461
|
+
//#endregion
|
|
462
|
+
//#region src/runtime/context.d.ts
|
|
463
|
+
type Context<Data extends {} = {}> = {
|
|
464
|
+
/**
|
|
465
|
+
* Data used in the command action
|
|
466
|
+
*/
|
|
467
|
+
data: Data;
|
|
468
|
+
/**
|
|
469
|
+
* Output of the command action
|
|
470
|
+
*/
|
|
471
|
+
output?: any;
|
|
472
|
+
/**
|
|
473
|
+
* Matched commands or groups, sorted by appearance order
|
|
474
|
+
*/
|
|
475
|
+
group?: InternalGroup;
|
|
476
|
+
/**
|
|
477
|
+
* Matched command
|
|
478
|
+
*/
|
|
479
|
+
command?: InternalCommand;
|
|
480
|
+
/**
|
|
481
|
+
* Match command pieces
|
|
482
|
+
*/
|
|
483
|
+
readonly pieces: string[];
|
|
484
|
+
/**
|
|
485
|
+
* Matched options
|
|
486
|
+
*/
|
|
487
|
+
readonly options: Map<string, MatchedOption>;
|
|
488
|
+
/**
|
|
489
|
+
* Matched arguments
|
|
490
|
+
*/
|
|
491
|
+
readonly arguments: MatchedArgument[];
|
|
492
|
+
/**
|
|
493
|
+
* Remaing arguments
|
|
494
|
+
*/
|
|
495
|
+
readonly remaining: string[];
|
|
496
|
+
/**
|
|
497
|
+
* Breadc app instance
|
|
498
|
+
*/
|
|
499
|
+
readonly breadc: InternalBreadc;
|
|
500
|
+
/**
|
|
501
|
+
* Input token stream
|
|
502
|
+
*/
|
|
503
|
+
readonly tokens: TokenStream;
|
|
504
|
+
};
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/breadc/types/app.d.ts
|
|
507
|
+
/**
|
|
508
|
+
* @public
|
|
509
|
+
*/
|
|
510
|
+
type Breadc<Data extends {} = {}, Options extends Record<never, never> = {}> = {
|
|
511
|
+
name: string;
|
|
512
|
+
version: string | undefined;
|
|
513
|
+
group<GS extends string, G extends Group<GS>>(group: G): G;
|
|
514
|
+
group<GS extends string, GI extends GroupInit<GS>>(spec: GS, description?: string, init?: GI): Group<GS, GI, Data, Options>;
|
|
515
|
+
/**
|
|
516
|
+
* Add option
|
|
517
|
+
*
|
|
518
|
+
* @param spec
|
|
519
|
+
* @param init
|
|
520
|
+
*/
|
|
521
|
+
option<Opt extends Option<any, any, any>>(option: Opt): Breadc<Data, Options & InferOptionFromInstance<Opt>>;
|
|
522
|
+
option<OS extends string, Initial extends NonTrueNullable<InferOptionInitialType<OS>>, OI extends NonNullableOptionInit<OS, Initial>>(spec: OS, description: string, init: OI): Breadc<Data, Options & InferOption<OS, Initial, OI>>;
|
|
523
|
+
option<OS extends string, Initial extends InferOptionInitialType<OS>, OI extends OptionInit<OS, Initial>>(spec: OS, description?: string, init?: OI): Breadc<Data, Options & InferOption<OS, Initial, OI>>;
|
|
524
|
+
command<S extends string, I extends CommandInit<S>>(spec: S, description?: string, init?: I): Command<S, I, Data, Options, InferArgumentsType<S>, unknown>;
|
|
525
|
+
command<S extends string, I extends CommandInit<S>>(command: Command<S, I, Data, Options>): Command<S, I, Data, Options, InferArgumentsType<S>, unknown>;
|
|
526
|
+
/**
|
|
527
|
+
* Action middleware
|
|
528
|
+
*/
|
|
529
|
+
use<Middleware extends ActionMiddleware<Data, ActionMiddlewareNextFn>>(middleware: Middleware): Breadc<InferMiddlewareData<Middleware>, Options>;
|
|
530
|
+
/**
|
|
531
|
+
* Allow unknown option middleware
|
|
532
|
+
*/
|
|
533
|
+
onUnknownCommand(middleware?: boolean | UnknownCommandMiddleware<Data>): Breadc<Data, Options>;
|
|
534
|
+
/**
|
|
535
|
+
* Allow unknown option middleware
|
|
536
|
+
*/
|
|
537
|
+
allowUnknownOption(middleware?: UnknownOptionMiddleware<Data>): Breadc<Data, Options>;
|
|
538
|
+
/**
|
|
539
|
+
* Parse CLI options
|
|
540
|
+
*
|
|
541
|
+
* @param argv CLI arguments
|
|
542
|
+
*/
|
|
543
|
+
parse<PArgs extends any[] = any[], POpts extends Record<string, any> = {}>(argv: string[]): {
|
|
544
|
+
args: PArgs;
|
|
545
|
+
options: Prettify<Options & POpts>;
|
|
546
|
+
'--': string[];
|
|
547
|
+
context: Context<Data>;
|
|
548
|
+
};
|
|
549
|
+
/**
|
|
550
|
+
* Parse and run corresponding command actions
|
|
551
|
+
*
|
|
552
|
+
* @param argv CLI arguments
|
|
553
|
+
*/
|
|
554
|
+
run<T>(argv: string[]): Promise<T>;
|
|
555
|
+
};
|
|
556
|
+
type Group<Spec extends string = string, Init extends GroupInit<Spec> = GroupInit<Spec>, Data extends {} = {}, Options extends Record<never, never> = Record<never, never>> = {
|
|
557
|
+
spec: Spec;
|
|
558
|
+
init: Init | undefined;
|
|
559
|
+
/**
|
|
560
|
+
* Add option
|
|
561
|
+
*
|
|
562
|
+
* @param spec
|
|
563
|
+
* @param init
|
|
564
|
+
*/
|
|
565
|
+
option<Opt extends Option<any, any, any>>(option: Opt): Group<Spec, Init, Data, Options & InferOptionFromInstance<Opt>>;
|
|
566
|
+
option<OS extends string, Initial extends NonTrueNullable<InferOptionInitialType<OS>>, OI extends NonNullableOptionInit<OS, Initial>>(spec: OS, description: string, init: OI): Group<Spec, Init, Data, Options & InferOption<OS, Initial, OI>>;
|
|
567
|
+
option<OS extends string, Initial extends InferOptionInitialType<OS>, OI extends OptionInit<OS, Initial>>(spec: OS, description?: string, init?: OI): Group<Spec, Init, Data, Options & InferOption<OS, Initial, OI>>;
|
|
568
|
+
command<S extends string, I extends CommandInit<S>>(spec: S, description?: string, init?: I): Command<S, I, Data, Options, InferArgumentsType<S>, unknown>;
|
|
569
|
+
command<S extends string, I extends CommandInit<S>>(command: Command<S, I, Options>): Command<S, I, Data, Options, InferArgumentsType<S>, unknown>;
|
|
570
|
+
/**
|
|
571
|
+
* Action middleware
|
|
572
|
+
*/
|
|
573
|
+
use<Middleware extends ActionMiddleware<Data>>(middleware: Middleware): Group<Spec, Init, InferMiddlewareData<Middleware>, Options>;
|
|
574
|
+
/**
|
|
575
|
+
* Allow unknown options middleware
|
|
576
|
+
*/
|
|
577
|
+
allowUnknownOption(middleware?: UnknownOptionMiddleware<Data>): Group<Spec, Init, Data, Options>;
|
|
578
|
+
};
|
|
579
|
+
type Command<Spec extends string = string, Init extends CommandInit<Spec> = CommandInit<Spec>, Data extends {} = {}, Options extends Record<never, never> = {}, Arguments extends unknown[] = InferArgumentsType<Spec>, Return extends unknown = unknown> = {
|
|
580
|
+
spec: Spec;
|
|
581
|
+
init: Init | undefined;
|
|
582
|
+
/**
|
|
583
|
+
* Alias command
|
|
584
|
+
*
|
|
585
|
+
* @param spec
|
|
586
|
+
*/
|
|
587
|
+
alias(spec: string): Command<Spec, Init, Data, Options, Arguments, Return>;
|
|
588
|
+
/**
|
|
589
|
+
* Add option
|
|
590
|
+
*
|
|
591
|
+
* @param spec
|
|
592
|
+
* @param init
|
|
593
|
+
*/
|
|
594
|
+
option<Opt extends Option<any, any, any>>(option: Opt): Command<Spec, Init, Data, Options & InferOptionFromInstance<Opt>, Arguments, Return>;
|
|
595
|
+
option<OS extends string, Initial extends NonTrueNullable<InferOptionInitialType<OS>>, OI extends NonNullableOptionInit<OS, Initial>>(spec: OS, description: string, init: OI): Command<Spec, Init, Data, Options & InferOption<OS, Initial, OI>, Arguments, Return>;
|
|
596
|
+
option<OS extends string, Initial extends InferOptionInitialType<OS>, OI extends OptionInit<OS, Initial>>(spec: OS, description?: string, init?: OI): Command<Spec, Init, Data, Options & InferOption<OS, Initial, OI>, Arguments, Return>;
|
|
597
|
+
/**
|
|
598
|
+
* Add argument
|
|
599
|
+
*/
|
|
600
|
+
argument<AS extends string, Initial extends InferArgumentRawType<AS>, Cast extends unknown, AI extends ArgumentInit<AS, Initial, Cast>>(argument: Argument<AS, Initial, Cast, AI>): Command<Spec, Init, Data, Options, [...Arguments, InferArgumentType<AS, Initial, AI>], Return>;
|
|
601
|
+
argument<AS extends string, Initial extends NonNullable<InferArgumentRawType<AS>>, Cast extends unknown, AI extends NonNullableArgumentInit<AS, Initial, Cast>>(spec: AS, init: AI): Command<Spec, Init, Data, Options, [...Arguments, InferArgumentType<AS, Initial, AI>], Return>;
|
|
602
|
+
argument<AS extends string, Initial extends InferArgumentRawType<AS>, Cast extends unknown, AI extends ArgumentInit<AS, Initial, Cast>>(spec: AS, init?: AI): Command<Spec, Init, Data, Options, [...Arguments, InferArgumentType<AS, Initial, AI>], Return>;
|
|
603
|
+
/**
|
|
604
|
+
* Action middleware
|
|
605
|
+
*/
|
|
606
|
+
use<Middleware extends ActionMiddleware<Data>>(middleware: Middleware): Command<Spec, Init, InferMiddlewareData<Middleware>, Options, Arguments, Return>;
|
|
607
|
+
/**
|
|
608
|
+
* Allow unknown options middleware
|
|
609
|
+
*/
|
|
610
|
+
allowUnknownOption(middleware?: UnknownOptionMiddleware<Data>): Command<Spec, Init, Data, Options, Arguments, Return>;
|
|
611
|
+
/**
|
|
612
|
+
* Bind action function
|
|
613
|
+
*/
|
|
614
|
+
action<R extends unknown>(fn: (...args: [...Arguments, Prettify<Options & {
|
|
615
|
+
'--': string[];
|
|
616
|
+
}>]) => Promise<R> | R): Command<Spec, Init, Data, Options, Arguments, R>;
|
|
617
|
+
/**
|
|
618
|
+
* Run command directly
|
|
619
|
+
*/
|
|
620
|
+
(...args: [...Arguments, Prettify<Options & {
|
|
621
|
+
'--': string[];
|
|
622
|
+
}>]): Promise<Return>;
|
|
623
|
+
};
|
|
624
|
+
type Option<Spec extends string = string, Initial extends InferOptionInitialType<Spec> = InferOptionInitialType<Spec>, Init extends OptionInit<Spec, Initial> = OptionInit<Spec, Initial>> = {
|
|
625
|
+
spec: Spec;
|
|
626
|
+
init: Init;
|
|
627
|
+
};
|
|
628
|
+
type InferOptionFromInstance<Opt extends Option<any, any, any>> = Opt extends Option<infer OS, infer Initial, infer OI> ? InferOption<OS, Initial, OI & {}> : never;
|
|
629
|
+
type Argument<Spec extends string = string, Initial extends InferArgumentRawType<Spec> = InferArgumentRawType<Spec>, Cast extends unknown = unknown, Init extends ArgumentInit<Spec, Initial, Cast> = ArgumentInit<Spec, Initial, Cast>> = {
|
|
630
|
+
spec: Spec;
|
|
631
|
+
type: ArgumentType;
|
|
632
|
+
name: string;
|
|
633
|
+
init: Init | undefined;
|
|
634
|
+
};
|
|
635
|
+
//#endregion
|
|
636
|
+
//#region src/breadc/app.d.ts
|
|
637
|
+
declare function breadc(name: string, init?: BreadcInit): Breadc;
|
|
638
|
+
//#endregion
|
|
639
|
+
//#region src/breadc/group.d.ts
|
|
640
|
+
declare function group<S extends string, I extends GroupInit<S>>(spec: S, init?: I): Group<S, I, {}, {}>;
|
|
641
|
+
//#endregion
|
|
642
|
+
//#region src/breadc/option.d.ts
|
|
643
|
+
declare function option<Spec extends string, Initial extends InferOptionInitialType<Spec>, Init extends OptionInit<Spec, Initial, unknown>>(spec: Spec, description?: string, init?: Init): Option<Spec, Initial, Init>;
|
|
644
|
+
//#endregion
|
|
645
|
+
//#region src/breadc/command.d.ts
|
|
646
|
+
declare function command<S extends string, I extends CommandInit<S>>(spec: S, init?: I): Command<S, I, {}, {}, InferArgumentsType<S>, unknown>;
|
|
647
|
+
declare function argument<Spec extends string, Initial extends NonNullable<InferArgumentRawType<Spec>>, Cast extends unknown, Init extends NonNullableArgumentInit<Spec, Initial, Cast>>(spec: Spec, init: Init): Argument<Spec, Initial, Cast, Init>;
|
|
648
|
+
declare function argument<Spec extends string, Initial extends InferArgumentRawType<Spec>, Cast extends unknown, Init extends ArgumentInit<Spec, Initial, Cast>>(spec: Spec, init?: Init): Argument<Spec, Initial, Cast, Init>;
|
|
649
|
+
//#endregion
|
|
650
|
+
//#region src/error.d.ts
|
|
651
|
+
declare abstract class BreadcError extends Error {}
|
|
652
|
+
declare class BreadcAppError extends BreadcError {
|
|
653
|
+
static DUPLICATED_DEFAULT_COMMAND: string;
|
|
654
|
+
static DUPLICATED_COMMAND: string;
|
|
655
|
+
static NO_ACTION_BOUND: string;
|
|
656
|
+
cause: {
|
|
657
|
+
command?: InternalCommand;
|
|
658
|
+
commands?: (InternalCommand | InternalGroup)[];
|
|
659
|
+
};
|
|
660
|
+
constructor(message: string, cause: BreadcAppError['cause']);
|
|
661
|
+
}
|
|
662
|
+
declare class ResolveGroupError extends BreadcError {
|
|
663
|
+
static EMPTY: string;
|
|
664
|
+
static INVALID_ARG_IN_GROUP: string;
|
|
665
|
+
cause: {
|
|
666
|
+
spec: string;
|
|
667
|
+
position: number;
|
|
668
|
+
};
|
|
669
|
+
constructor(message: string, cause: ResolveGroupError['cause']);
|
|
670
|
+
}
|
|
671
|
+
declare class ResolveCommandError extends BreadcError {
|
|
672
|
+
static INVALID_ARG: string;
|
|
673
|
+
static INVALID_EMPTY_ARG: string;
|
|
674
|
+
static INVALID_REQUIRED_ARG: string;
|
|
675
|
+
static INVALID_OPTIONAL_ARG: string;
|
|
676
|
+
static INVALID_SPREAD_ARG: string;
|
|
677
|
+
static INVALID_ALIAS_FORMAT: string;
|
|
678
|
+
static PIECE_BEFORE_REQUIRED: string;
|
|
679
|
+
static REQUIRED_BEFORE_OPTIONAL: string;
|
|
680
|
+
static OPTIONAL_BEFORE_SPREAD: string;
|
|
681
|
+
static SPREAD_ONLY_ONCE: string;
|
|
682
|
+
cause: {
|
|
683
|
+
spec: string;
|
|
684
|
+
position: number;
|
|
685
|
+
};
|
|
686
|
+
constructor(message: string, cause: ResolveCommandError['cause']);
|
|
687
|
+
}
|
|
688
|
+
declare class ResolveOptionError extends BreadcError {
|
|
689
|
+
static INVALID_OPTION: string;
|
|
690
|
+
cause: {
|
|
691
|
+
spec: string;
|
|
692
|
+
};
|
|
693
|
+
constructor(message: string, cause: ResolveOptionError['cause']);
|
|
694
|
+
}
|
|
695
|
+
//#endregion
|
|
696
|
+
export { type ActionMiddleware, type Argument, type ArgumentInit, type Breadc, BreadcAppError, BreadcError, type BreadcInit, type Command, type CommandInit, type Context, type Group, type GroupInit, MatchedArgument, MatchedOption, type MatchedUnknownOption, type Option, type OptionInit, ResolveCommandError, ResolveGroupError, ResolveOptionError, type UnknownCommandMiddleware, type UnknownOptionMiddleware, argument, breadc, command, group, option };
|