@gunshi/shared 0.26.3 → 0.27.0-alpha.10
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/README.md +8 -2
- package/lib/index.d.ts +283 -123
- package/lib/index.js +75 -5
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# `@gunshi/shared`
|
|
2
2
|
|
|
3
|
-
shared utils for gunshi
|
|
3
|
+
> shared utils for gunshi
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<!-- eslint-disable markdown/no-missing-label-refs -->
|
|
6
|
+
|
|
7
|
+
> [!WARNING]
|
|
8
|
+
> Please, don't use this package for your project.
|
|
9
|
+
> This package was made for use with the gunshi plugin.
|
|
10
|
+
|
|
11
|
+
<!-- eslint-enable markdown/no-missing-label-refs -->
|
|
6
12
|
|
|
7
13
|
## ©️ License
|
|
8
14
|
|
package/lib/index.d.ts
CHANGED
|
@@ -3,164 +3,201 @@ import en_US_default from "@gunshi/resources/en-US";
|
|
|
3
3
|
//#region rolldown:runtime
|
|
4
4
|
|
|
5
5
|
//#endregion
|
|
6
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
6
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/parser-Cbxholql.d.ts
|
|
7
7
|
//#region src/parser.d.ts
|
|
8
8
|
/**
|
|
9
|
-
* Entry point of argument parser.
|
|
10
|
-
* @module
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* forked from `nodejs/node` (`pkgjs/parseargs`)
|
|
14
|
-
* repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
|
|
15
|
-
* code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
|
|
16
|
-
*
|
|
17
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
18
|
-
* @license MIT
|
|
19
|
-
*/
|
|
20
|
-
/**
|
|
21
|
-
* Argument token Kind.
|
|
22
|
-
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
23
|
-
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
24
|
-
* - `positional`: positional token
|
|
25
|
-
*/
|
|
26
|
-
type ArgTokenKind =
|
|
27
|
-
/**
|
|
28
|
-
* Argument token.
|
|
29
|
-
*/
|
|
9
|
+
* Entry point of argument parser.
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* forked from `nodejs/node` (`pkgjs/parseargs`)
|
|
14
|
+
* repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
|
|
15
|
+
* code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
|
|
16
|
+
*
|
|
17
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
18
|
+
* @license MIT
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Argument token Kind.
|
|
22
|
+
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
23
|
+
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
24
|
+
* - `positional`: positional token
|
|
25
|
+
*/
|
|
26
|
+
type ArgTokenKind = 'option' | 'option-terminator' | 'positional';
|
|
27
|
+
/**
|
|
28
|
+
* Argument token.
|
|
29
|
+
*/
|
|
30
30
|
interface ArgToken {
|
|
31
31
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
* Argument token kind.
|
|
33
|
+
*/
|
|
34
34
|
kind: ArgTokenKind;
|
|
35
35
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
* Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1.
|
|
37
|
+
*/
|
|
38
38
|
index: number;
|
|
39
39
|
/**
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
* Option name, e.g. `--foo` => `foo`, `-x` => `x`.
|
|
41
|
+
*/
|
|
42
42
|
name?: string;
|
|
43
43
|
/**
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
* Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`.
|
|
45
|
+
*/
|
|
46
46
|
rawName?: string;
|
|
47
47
|
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
* Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
|
|
49
|
+
* If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
|
|
50
|
+
*/
|
|
51
51
|
value?: string;
|
|
52
52
|
/**
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
* Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
|
|
54
|
+
*/
|
|
55
55
|
inlineValue?: boolean;
|
|
56
|
-
}
|
|
57
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.20.1/node_modules/args-tokens/lib/resolver-U72Jg6Ll.d.ts
|
|
58
|
-
|
|
56
|
+
}
|
|
59
57
|
/**
|
|
60
|
-
* Parser Options.
|
|
61
|
-
*/
|
|
58
|
+
* Parser Options.
|
|
59
|
+
*/
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/resolver-BoS-UnqX.d.ts
|
|
62
62
|
//#region src/resolver.d.ts
|
|
63
|
+
|
|
63
64
|
/**
|
|
64
|
-
* An argument schema
|
|
65
|
-
* This schema is similar to the schema of the `node:utils`.
|
|
66
|
-
* difference is that:
|
|
67
|
-
* - `required` property and `description` property are added
|
|
68
|
-
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
69
|
-
* - `default` property type, not support multiple types
|
|
70
|
-
*/
|
|
65
|
+
* An argument schema
|
|
66
|
+
* This schema is similar to the schema of the `node:utils`.
|
|
67
|
+
* difference is that:
|
|
68
|
+
* - `required` property and `description` property are added
|
|
69
|
+
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
70
|
+
* - `default` property type, not support multiple types
|
|
71
|
+
*/
|
|
71
72
|
interface ArgSchema {
|
|
72
73
|
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
type:
|
|
74
|
+
* Type of argument.
|
|
75
|
+
*/
|
|
76
|
+
type: 'string' | 'boolean' | 'number' | 'enum' | 'positional' | 'custom';
|
|
76
77
|
/**
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
* A single character alias for the argument.
|
|
79
|
+
*/
|
|
79
80
|
short?: string;
|
|
80
81
|
/**
|
|
81
|
-
|
|
82
|
-
|
|
82
|
+
* A description of the argument.
|
|
83
|
+
*/
|
|
83
84
|
description?: string;
|
|
84
85
|
/**
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
* Whether the argument is required or not.
|
|
87
|
+
*/
|
|
87
88
|
required?: true;
|
|
88
89
|
/**
|
|
89
|
-
|
|
90
|
-
|
|
90
|
+
* Whether the argument allow multiple values or not.
|
|
91
|
+
*/
|
|
91
92
|
multiple?: true;
|
|
92
93
|
/**
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
* Whether the negatable option for `boolean` type
|
|
95
|
+
*/
|
|
95
96
|
negatable?: boolean;
|
|
96
97
|
/**
|
|
97
|
-
|
|
98
|
-
|
|
98
|
+
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
99
|
+
*/
|
|
99
100
|
choices?: string[] | readonly string[];
|
|
100
101
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
* The default value of the argument.
|
|
103
|
+
* if the type is 'enum', the default value must be one of the allowed values.
|
|
104
|
+
*/
|
|
104
105
|
default?: string | boolean | number;
|
|
105
106
|
/**
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
* Whether to convert the argument name to kebab-case.
|
|
108
|
+
*/
|
|
108
109
|
toKebab?: true;
|
|
109
110
|
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
111
|
+
* A function to parse the value of the argument. if the type is 'custom', this function is required.
|
|
112
|
+
* If argument value will be invalid, this function have to throw an error.
|
|
113
|
+
* @param value
|
|
114
|
+
* @returns Parsed value
|
|
115
|
+
* @throws An Error, If the value is invalid. Error type should be `Error` or extends it
|
|
116
|
+
*/
|
|
117
117
|
parse?: (value: string) => any;
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
120
|
-
* An object that contains {@link ArgSchema | argument schema}.
|
|
121
|
-
*/
|
|
120
|
+
* An object that contains {@link ArgSchema | argument schema}.
|
|
121
|
+
*/
|
|
122
122
|
interface Args {
|
|
123
123
|
[option: string]: ArgSchema;
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
126
|
-
* An object that contains the values of the arguments.
|
|
127
|
-
*/
|
|
126
|
+
* An object that contains the values of the arguments.
|
|
127
|
+
*/
|
|
128
128
|
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
129
129
|
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
130
130
|
};
|
|
131
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
132
131
|
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
133
132
|
/**
|
|
134
|
-
* @internal
|
|
135
|
-
*/
|
|
136
|
-
type ExtractOptionValue<A extends ArgSchema> = A[
|
|
137
|
-
type ResolveOptionValue<A extends ArgSchema, T> = A[
|
|
133
|
+
* @internal
|
|
134
|
+
*/
|
|
135
|
+
type ExtractOptionValue<A extends ArgSchema> = A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ? IsFunction<A['parse']> extends true ? ResolveOptionValue<A, ReturnType<NonNullable<A['parse']>>> : never : ResolveOptionValue<A, string | boolean | number>;
|
|
136
|
+
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
138
137
|
/**
|
|
139
|
-
* @internal
|
|
140
|
-
*/
|
|
141
|
-
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } & FilterArgs<A, V,
|
|
138
|
+
* @internal
|
|
139
|
+
*/
|
|
140
|
+
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } & FilterArgs<A, V, 'default'> & FilterArgs<A, V, 'required'> & FilterPositionalArgs<A, V> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
142
141
|
/**
|
|
143
|
-
* @internal
|
|
144
|
-
*/
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
145
144
|
type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K extends keyof ArgSchema> = { [Arg in keyof A as A[Arg][K] extends {} ? Arg : never]: V[Arg] };
|
|
146
145
|
/**
|
|
147
|
-
* @internal
|
|
148
|
-
*/
|
|
149
|
-
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg][
|
|
146
|
+
* @internal
|
|
147
|
+
*/
|
|
148
|
+
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg]['type'] extends 'positional' ? Arg : never]: V[Arg] };
|
|
149
|
+
/**
|
|
150
|
+
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
151
|
+
*/
|
|
150
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Tracks which arguments were explicitly provided by the user.
|
|
155
|
+
*
|
|
156
|
+
* Each property indicates whether the corresponding argument was explicitly
|
|
157
|
+
* provided (true) or is using a default value or not provided (false).
|
|
158
|
+
*/
|
|
159
|
+
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
160
|
+
/**
|
|
161
|
+
* Resolve command line arguments.
|
|
162
|
+
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
163
|
+
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
164
|
+
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
165
|
+
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // passed tokens: --port 3000
|
|
170
|
+
*
|
|
171
|
+
* const { values, explicit } = resolveArgs({
|
|
172
|
+
* port: {
|
|
173
|
+
* type: 'number',
|
|
174
|
+
* default: 8080
|
|
175
|
+
* },
|
|
176
|
+
* host: {
|
|
177
|
+
* type: 'string',
|
|
178
|
+
* default: 'localhost'
|
|
179
|
+
* }
|
|
180
|
+
* }, parsedTokens)
|
|
181
|
+
*
|
|
182
|
+
* values.port // 3000
|
|
183
|
+
* values.host // 'localhost'
|
|
184
|
+
*
|
|
185
|
+
* explicit.port // true (explicitly provided)
|
|
186
|
+
* explicit.host // false (not provided, fallback to default)
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
151
189
|
//#endregion
|
|
152
190
|
//#region ../gunshi/src/types.d.ts
|
|
153
|
-
/**
|
|
154
|
-
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
155
|
-
*/
|
|
156
191
|
type Awaitable<T> = T | Promise<T>;
|
|
157
192
|
/**
|
|
158
193
|
* Extend command context type. This type is used to extend the command context with additional properties at {@link CommandContext.extensions}.
|
|
194
|
+
* @since v0.27.0
|
|
159
195
|
*/
|
|
160
196
|
type ExtendContext = Record<string, unknown>;
|
|
161
197
|
/**
|
|
162
198
|
* Gunshi unified parameter type.
|
|
163
199
|
* This type combines both argument definitions and command context extensions.
|
|
200
|
+
* @since v0.27.0
|
|
164
201
|
*/
|
|
165
202
|
interface GunshiParams<P extends {
|
|
166
203
|
args?: Args;
|
|
@@ -184,11 +221,13 @@ interface GunshiParams<P extends {
|
|
|
184
221
|
}
|
|
185
222
|
/**
|
|
186
223
|
* Default Gunshi parameters
|
|
224
|
+
* @since v0.27.0
|
|
187
225
|
*/
|
|
188
226
|
type DefaultGunshiParams = GunshiParams;
|
|
189
227
|
/**
|
|
190
228
|
* Generic constraint for command-related types.
|
|
191
229
|
* This type constraint allows both GunshiParams and objects with extensions.
|
|
230
|
+
* @since v0.27.0
|
|
192
231
|
*/
|
|
193
232
|
type GunshiParamsConstraint = GunshiParams<any> | {
|
|
194
233
|
extensions: ExtendContext;
|
|
@@ -198,6 +237,11 @@ type GunshiParamsConstraint = GunshiParams<any> | {
|
|
|
198
237
|
* @internal
|
|
199
238
|
*/
|
|
200
239
|
type ExtractArgs<G> = G extends GunshiParams<any> ? G['args'] : Args;
|
|
240
|
+
/**
|
|
241
|
+
* Type helper to extract explicitly provided argument flags from G
|
|
242
|
+
* @internal
|
|
243
|
+
*/
|
|
244
|
+
type ExtractArgExplicitlyProvided<G> = ArgExplicitlyProvided<ExtractArgs<G>>;
|
|
201
245
|
/**
|
|
202
246
|
* Type helper to extract extensions from G
|
|
203
247
|
* @internal
|
|
@@ -285,16 +329,19 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
285
329
|
/**
|
|
286
330
|
* Hook that runs before any command execution
|
|
287
331
|
* @see {@link CliOptions.onBeforeCommand}
|
|
332
|
+
* @since v0.27.0
|
|
288
333
|
*/
|
|
289
334
|
onBeforeCommand: ((ctx: Readonly<CommandContext<G>>) => Awaitable<void>) | undefined;
|
|
290
335
|
/**
|
|
291
336
|
* Hook that runs after successful command execution
|
|
292
337
|
* @see {@link CliOptions.onAfterCommand}
|
|
338
|
+
* @since v0.27.0
|
|
293
339
|
*/
|
|
294
|
-
onAfterCommand: ((ctx: Readonly<CommandContext<G>>, result: string |
|
|
340
|
+
onAfterCommand: ((ctx: Readonly<CommandContext<G>>, result: string | undefined) => Awaitable<void>) | undefined;
|
|
295
341
|
/**
|
|
296
342
|
* Hook that runs when a command throws an error
|
|
297
343
|
* @see {@link CliOptions.onErrorCommand}
|
|
344
|
+
* @since v0.27.0
|
|
298
345
|
*/
|
|
299
346
|
onErrorCommand: ((ctx: Readonly<CommandContext<G>>, error: Error) => Awaitable<void>) | undefined;
|
|
300
347
|
}
|
|
@@ -331,6 +378,15 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
331
378
|
* The command arguments is same {@link Command.args}.
|
|
332
379
|
*/
|
|
333
380
|
args: ExtractArgs<G>;
|
|
381
|
+
/**
|
|
382
|
+
* Whether arguments were explicitly provided by the user.
|
|
383
|
+
*
|
|
384
|
+
* - `true`: The argument was explicitly provided via command line
|
|
385
|
+
* - `false`: The argument was not explicitly provided. This means either:
|
|
386
|
+
* - The value comes from a default value defined in the argument schema
|
|
387
|
+
* - The value is `undefined` (no explicit input and no default value)
|
|
388
|
+
*/
|
|
389
|
+
explicit: ExtractArgExplicitlyProvided<G>;
|
|
334
390
|
/**
|
|
335
391
|
* Command values, that is the values of the command that is executed.
|
|
336
392
|
* Resolve values with `resolveArgs` from command arguments and {@link Command.args}.
|
|
@@ -377,7 +433,8 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
377
433
|
*/
|
|
378
434
|
log: (message?: any, ...optionalParams: any[]) => void;
|
|
379
435
|
/**
|
|
380
|
-
*
|
|
436
|
+
* Command context extensions.
|
|
437
|
+
* @since v0.27.0
|
|
381
438
|
*/
|
|
382
439
|
extensions: keyof ExtractExtensions<G> extends never ? undefined : ExtractExtensions<G>;
|
|
383
440
|
/**
|
|
@@ -388,8 +445,36 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
388
445
|
}
|
|
389
446
|
/**
|
|
390
447
|
* CommandContextCore type (base type without extensions)
|
|
448
|
+
* @since v0.27.0
|
|
391
449
|
*/
|
|
392
450
|
|
|
451
|
+
/**
|
|
452
|
+
* Rendering control options
|
|
453
|
+
* @since v0.27.0
|
|
454
|
+
*/
|
|
455
|
+
interface RenderingOptions<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
456
|
+
/**
|
|
457
|
+
* Header rendering configuration
|
|
458
|
+
* - `null`: Disable rendering
|
|
459
|
+
* - `function`: Use custom renderer
|
|
460
|
+
* - `undefined` (when omitted): Use default renderer
|
|
461
|
+
*/
|
|
462
|
+
header?: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null;
|
|
463
|
+
/**
|
|
464
|
+
* Usage rendering configuration
|
|
465
|
+
* - `null`: Disable rendering
|
|
466
|
+
* - `function`: Use custom renderer
|
|
467
|
+
* - `undefined` (when omitted): Use default renderer
|
|
468
|
+
*/
|
|
469
|
+
usage?: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null;
|
|
470
|
+
/**
|
|
471
|
+
* Validation errors rendering configuration
|
|
472
|
+
* - `null`: Disable rendering
|
|
473
|
+
* - `function`: Use custom renderer
|
|
474
|
+
* - `undefined` (when omitted): Use default renderer
|
|
475
|
+
*/
|
|
476
|
+
validationErrors?: ((ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>) | null;
|
|
477
|
+
}
|
|
393
478
|
/**
|
|
394
479
|
* Command interface.
|
|
395
480
|
*/
|
|
@@ -423,6 +508,24 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
423
508
|
* If you will set to `true`, All {@link Command.args} names will be converted to kebab-case.
|
|
424
509
|
*/
|
|
425
510
|
toKebab?: boolean;
|
|
511
|
+
/**
|
|
512
|
+
* Whether this is an internal command.
|
|
513
|
+
* Internal commands are not shown in help usage.
|
|
514
|
+
* @default false
|
|
515
|
+
* @since v0.27.0
|
|
516
|
+
*/
|
|
517
|
+
internal?: boolean;
|
|
518
|
+
/**
|
|
519
|
+
* Whether this command is an entry command.
|
|
520
|
+
* @default undefined
|
|
521
|
+
* @since v0.27.0
|
|
522
|
+
*/
|
|
523
|
+
entry?: boolean;
|
|
524
|
+
/**
|
|
525
|
+
* Rendering control options
|
|
526
|
+
* @since v0.27.0
|
|
527
|
+
*/
|
|
528
|
+
rendering?: RenderingOptions<G>;
|
|
426
529
|
}
|
|
427
530
|
/**
|
|
428
531
|
* Lazy command interface.
|
|
@@ -453,40 +556,36 @@ type CommandExamplesFetcher<G extends GunshiParamsConstraint = DefaultGunshiPara
|
|
|
453
556
|
* @param ctx A {@link CommandContext | command context}
|
|
454
557
|
* @returns void or string (for CLI output)
|
|
455
558
|
*/
|
|
456
|
-
type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<
|
|
457
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.20.1/node_modules/args-tokens/lib/utils.d.ts
|
|
458
|
-
|
|
559
|
+
type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<string | void>;
|
|
459
560
|
/**
|
|
460
561
|
* Command loader.
|
|
461
562
|
* A function that returns a command or command runner.
|
|
462
563
|
* This is used to lazily load commands.
|
|
463
564
|
* @returns A command or command runner
|
|
464
565
|
*/
|
|
566
|
+
//#endregion
|
|
567
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/utils.d.ts
|
|
465
568
|
//#region src/utils.d.ts
|
|
466
569
|
/**
|
|
467
|
-
* Entry point of utils.
|
|
468
|
-
*
|
|
469
|
-
* Note that this entry point is used by gunshi to import utility functions.
|
|
470
|
-
*
|
|
471
|
-
* @module
|
|
472
|
-
*/
|
|
570
|
+
* Entry point of utils.
|
|
571
|
+
*
|
|
572
|
+
* Note that this entry point is used by gunshi to import utility functions.
|
|
573
|
+
*
|
|
574
|
+
* @module
|
|
575
|
+
*/
|
|
473
576
|
/**
|
|
474
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
475
|
-
* @license MIT
|
|
476
|
-
*/
|
|
577
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
578
|
+
* @license MIT
|
|
579
|
+
*/
|
|
477
580
|
declare function kebabnize(str: string): string;
|
|
478
|
-
|
|
479
581
|
//#endregion
|
|
480
|
-
//#region ../gunshi/src/utils.d.ts
|
|
481
582
|
//#endregion
|
|
583
|
+
//#region ../gunshi/src/utils.d.ts
|
|
482
584
|
declare function isLazyCommand<G extends GunshiParamsConstraint = DefaultGunshiParams>(cmd: unknown): cmd is LazyCommand<G>;
|
|
483
585
|
declare function resolveLazyCommand<G extends GunshiParamsConstraint = DefaultGunshiParams>(cmd: Commandable<G>, name?: string | undefined, needRunResolving?: boolean): Promise<Command<G>>;
|
|
484
586
|
declare function create<T>(obj?: object | null): T;
|
|
485
587
|
declare function log(...args: unknown[]): void;
|
|
486
588
|
declare function deepFreeze<T extends Record<string, any>>(obj: T, ignores?: string[]): Readonly<T>;
|
|
487
|
-
|
|
488
|
-
//#endregion
|
|
489
|
-
//#region src/constants.d.ts
|
|
490
589
|
declare namespace constants_d_exports {
|
|
491
590
|
export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, PLUGIN_PREFIX };
|
|
492
591
|
}
|
|
@@ -515,7 +614,6 @@ type CommonArgType = {
|
|
|
515
614
|
};
|
|
516
615
|
declare const COMMON_ARGS: CommonArgType;
|
|
517
616
|
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
518
|
-
|
|
519
617
|
//#endregion
|
|
520
618
|
//#region src/types.d.ts
|
|
521
619
|
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K] };
|
|
@@ -537,26 +635,88 @@ type CommandBuiltinArgsKeys = keyof (typeof constants_d_exports)['COMMON_ARGS'];
|
|
|
537
635
|
*/
|
|
538
636
|
type CommandBuiltinResourceKeys = (typeof constants_d_exports)['COMMAND_BUILTIN_RESOURCE_KEYS'][number];
|
|
539
637
|
/**
|
|
540
|
-
*
|
|
638
|
+
* Built-in resource keys.
|
|
541
639
|
*/
|
|
542
640
|
type BuiltinResourceKeys = CommandBuiltinArgsKeys | CommandBuiltinResourceKeys;
|
|
543
641
|
/**
|
|
544
|
-
* Command
|
|
545
|
-
* The command i18n built-in keys are used by the i18n plugin for translation.
|
|
642
|
+
* Command built-in keys.
|
|
546
643
|
*/
|
|
547
|
-
type CommandBuiltinKeys = GenerateNamespacedKey<BuiltinResourceKeys
|
|
644
|
+
type CommandBuiltinKeys = GenerateNamespacedKey<BuiltinResourceKeys>;
|
|
548
645
|
/**
|
|
549
646
|
* Command i18n option keys.
|
|
550
647
|
* The command i18n option keys are used by the i18n plugin for translation.
|
|
551
648
|
*/
|
|
552
|
-
type CommandArgKeys<A extends Args
|
|
553
|
-
|
|
649
|
+
type CommandArgKeys<A extends Args, C = {}, K extends string = GenerateNamespacedKey<Extract<KeyOfArgs<RemovedIndex<A>>, string>, typeof ARG_PREFIX>> = C extends {
|
|
650
|
+
name: infer N;
|
|
651
|
+
} ? (N extends string ? GenerateNamespacedKey<K, N> : K) : K;
|
|
652
|
+
/**
|
|
653
|
+
* Resolve translation keys for command context.
|
|
654
|
+
*/
|
|
655
|
+
type ResolveTranslationKeys<A extends Args, C = {},
|
|
656
|
+
// for CommandContext
|
|
657
|
+
E extends Record<string, string> = {},
|
|
658
|
+
// for extended resources
|
|
659
|
+
R extends string = keyof RemovedIndex<E>, T extends string = (C extends {
|
|
660
|
+
name: infer N;
|
|
661
|
+
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys), O = CommandArgKeys<A, C>> = CommandBuiltinKeys | O | T;
|
|
662
|
+
/**
|
|
663
|
+
* Translation function interface
|
|
664
|
+
*/
|
|
665
|
+
interface Translation<A extends Args, C = {},
|
|
666
|
+
// for CommandContext
|
|
667
|
+
E extends Record<string, string> = {},
|
|
668
|
+
// for extended resources
|
|
669
|
+
K = ResolveTranslationKeys<A, C, E>> {
|
|
670
|
+
(key: K, values?: Record<string, unknown>): string;
|
|
671
|
+
}
|
|
672
|
+
//#endregion
|
|
673
|
+
//#region src/localization.d.ts
|
|
674
|
+
interface Localization<A extends Args, C = {},
|
|
675
|
+
// for CommandContext
|
|
676
|
+
E extends Record<string, string> = {}> {
|
|
677
|
+
<K = ResolveTranslationKeys<A, C, E>>(key: K, values?: Record<string, unknown>): string;
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Create a localizable function for a command.
|
|
681
|
+
* This function will resolve the translation key based on the command context and the provided translation function.
|
|
682
|
+
* @param ctx Command context
|
|
683
|
+
* @param cmd Command
|
|
684
|
+
* @param translate Translation function
|
|
685
|
+
* @returns Localizable function
|
|
686
|
+
*/
|
|
687
|
+
declare function localizable<A extends Args, C = {},
|
|
688
|
+
// for CommandContext
|
|
689
|
+
E extends Record<string, string> = {},
|
|
690
|
+
// for extended resources
|
|
691
|
+
K = ResolveTranslationKeys<A, C, E>>(ctx: CommandContext, cmd: Command, translate?: Translation<A, C, E, K>): Localization<A, C, E>;
|
|
554
692
|
//#endregion
|
|
555
693
|
//#region src/utils.d.ts
|
|
694
|
+
/**
|
|
695
|
+
* Resolve a namespaced key for built-in resources.
|
|
696
|
+
* Built-in keys are prefixed with "_:".
|
|
697
|
+
* @param key The built-in key to resolve.
|
|
698
|
+
* @returns Prefixed built-in key.
|
|
699
|
+
*/
|
|
556
700
|
declare function resolveBuiltInKey<K extends string = CommandBuiltinArgsKeys | CommandBuiltinResourceKeys>(key: K): GenerateNamespacedKey<K>;
|
|
557
|
-
|
|
701
|
+
/**
|
|
702
|
+
* Resolve a namespaced key for argument resources.
|
|
703
|
+
* Argument keys are prefixed with "arg:".
|
|
704
|
+
* If the command name is provided, it will be prefixed with the command name (e.g. "cmd1:arg:foo").
|
|
705
|
+
* @param key The argument key to resolve.
|
|
706
|
+
* @param ctx The command context.
|
|
707
|
+
* @returns Prefixed argument key.
|
|
708
|
+
*/
|
|
709
|
+
declare function resolveArgKey<A extends Args = DefaultGunshiParams['args'], K extends string = KeyOfArgs<RemovedIndex<A>>>(key: K, ctx?: Readonly<CommandContext>): string;
|
|
710
|
+
/**
|
|
711
|
+
* Resolve a namespaced key for non-built-in resources.
|
|
712
|
+
* Non-built-in keys are not prefixed with any special characters. If the command name is provided, it will be prefixed with the command name (e.g. "cmd1:foo").
|
|
713
|
+
* @param key The non-built-in key to resolve.
|
|
714
|
+
* @param ctx The command context.
|
|
715
|
+
* @returns Prefixed non-built-in key.
|
|
716
|
+
*/
|
|
717
|
+
declare function resolveKey<T extends Record<string, string> = {}, K = (keyof T extends string ? keyof T : string)>(key: K, ctx?: Readonly<CommandContext>): string;
|
|
558
718
|
declare function resolveExamples<G extends GunshiParamsConstraint = DefaultGunshiParams>(ctx: Readonly<CommandContext<G>>, examples?: string | CommandExamplesFetcher<G>): Promise<string>;
|
|
559
719
|
declare function namespacedId<K extends string>(id: K): GenerateNamespacedKey<K, typeof PLUGIN_PREFIX>;
|
|
560
|
-
|
|
720
|
+
declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKebab?: boolean): string;
|
|
561
721
|
//#endregion
|
|
562
|
-
export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, en_US_default as DefaultResource, GenerateNamespacedKey, KeyOfArgs, PLUGIN_PREFIX, RemovedIndex, create, deepFreeze, isLazyCommand, kebabnize, log, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveLazyCommand };
|
|
722
|
+
export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, en_US_default as DefaultResource, GenerateNamespacedKey, KeyOfArgs, Localization, PLUGIN_PREFIX, RemovedIndex, ResolveTranslationKeys, Translation, create, deepFreeze, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/utils-N7UlhLbz.js
|
|
2
2
|
/**
|
|
3
3
|
* Entry point of utils.
|
|
4
4
|
*
|
|
@@ -26,7 +26,9 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
26
26
|
name: cmd.commandName,
|
|
27
27
|
description: cmd.description,
|
|
28
28
|
args: cmd.args,
|
|
29
|
-
examples: cmd.examples
|
|
29
|
+
examples: cmd.examples,
|
|
30
|
+
internal: cmd.internal,
|
|
31
|
+
entry: cmd.entry
|
|
30
32
|
};
|
|
31
33
|
if ("resource" in cmd && cmd.resource) baseCommand.resource = cmd.resource;
|
|
32
34
|
command = Object.assign(create(), baseCommand);
|
|
@@ -40,6 +42,8 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
40
42
|
command.description = loaded.description;
|
|
41
43
|
command.args = loaded.args;
|
|
42
44
|
command.examples = loaded.examples;
|
|
45
|
+
command.internal = loaded.internal;
|
|
46
|
+
command.entry = loaded.entry;
|
|
43
47
|
if ("resource" in loaded && loaded.resource) command.resource = loaded.resource;
|
|
44
48
|
} else throw new TypeError(`Cannot resolve command: ${cmd.name || name}`);
|
|
45
49
|
}
|
|
@@ -135,11 +139,35 @@ var en_US_default = {
|
|
|
135
139
|
|
|
136
140
|
//#endregion
|
|
137
141
|
//#region src/utils.ts
|
|
142
|
+
/**
|
|
143
|
+
* Resolve a namespaced key for built-in resources.
|
|
144
|
+
* Built-in keys are prefixed with "_:".
|
|
145
|
+
* @param key The built-in key to resolve.
|
|
146
|
+
* @returns Prefixed built-in key.
|
|
147
|
+
*/
|
|
138
148
|
function resolveBuiltInKey(key) {
|
|
139
149
|
return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
140
150
|
}
|
|
141
|
-
|
|
142
|
-
|
|
151
|
+
/**
|
|
152
|
+
* Resolve a namespaced key for argument resources.
|
|
153
|
+
* Argument keys are prefixed with "arg:".
|
|
154
|
+
* If the command name is provided, it will be prefixed with the command name (e.g. "cmd1:arg:foo").
|
|
155
|
+
* @param key The argument key to resolve.
|
|
156
|
+
* @param ctx The command context.
|
|
157
|
+
* @returns Prefixed argument key.
|
|
158
|
+
*/
|
|
159
|
+
function resolveArgKey(key, ctx) {
|
|
160
|
+
return `${ctx?.name ? `${ctx.name}${BUILT_IN_KEY_SEPARATOR}` : ""}${ARG_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Resolve a namespaced key for non-built-in resources.
|
|
164
|
+
* Non-built-in keys are not prefixed with any special characters. If the command name is provided, it will be prefixed with the command name (e.g. "cmd1:foo").
|
|
165
|
+
* @param key The non-built-in key to resolve.
|
|
166
|
+
* @param ctx The command context.
|
|
167
|
+
* @returns Prefixed non-built-in key.
|
|
168
|
+
*/
|
|
169
|
+
function resolveKey(key, ctx) {
|
|
170
|
+
return `${ctx?.name ? `${ctx.name}${BUILT_IN_KEY_SEPARATOR}` : ""}${key}`;
|
|
143
171
|
}
|
|
144
172
|
async function resolveExamples(ctx, examples) {
|
|
145
173
|
return typeof examples === "string" ? examples : typeof examples === "function" ? await examples(ctx) : "";
|
|
@@ -147,6 +175,48 @@ async function resolveExamples(ctx, examples) {
|
|
|
147
175
|
function namespacedId(id) {
|
|
148
176
|
return `${PLUGIN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${id}`;
|
|
149
177
|
}
|
|
178
|
+
function makeShortLongOptionPair(schema, name, toKebab) {
|
|
179
|
+
const displayName = toKebab || schema.toKebab ? kebabnize(name) : name;
|
|
180
|
+
let key = `--${displayName}`;
|
|
181
|
+
if (schema.short) key = `-${schema.short}, ${key}`;
|
|
182
|
+
return key;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/localization.ts
|
|
187
|
+
/**
|
|
188
|
+
* Create a localizable function for a command.
|
|
189
|
+
* This function will resolve the translation key based on the command context and the provided translation function.
|
|
190
|
+
* @param ctx Command context
|
|
191
|
+
* @param cmd Command
|
|
192
|
+
* @param translate Translation function
|
|
193
|
+
* @returns Localizable function
|
|
194
|
+
*/
|
|
195
|
+
function localizable(ctx, cmd, translate) {
|
|
196
|
+
async function localize(key, values) {
|
|
197
|
+
if (translate) return translate(key, values);
|
|
198
|
+
if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) {
|
|
199
|
+
const resKey = key.slice(BUILD_IN_PREFIX_AND_KEY_SEPARATOR.length);
|
|
200
|
+
return en_US_default[resKey] || key;
|
|
201
|
+
}
|
|
202
|
+
const namaspacedArgKey = resolveKey(ARG_PREFIX_AND_KEY_SEPARATOR, ctx);
|
|
203
|
+
if (key.startsWith(namaspacedArgKey)) {
|
|
204
|
+
let argKey = key.slice(namaspacedArgKey.length);
|
|
205
|
+
let negatable = false;
|
|
206
|
+
if (argKey.startsWith(ARG_NEGATABLE_PREFIX)) {
|
|
207
|
+
argKey = argKey.slice(ARG_NEGATABLE_PREFIX.length);
|
|
208
|
+
negatable = true;
|
|
209
|
+
}
|
|
210
|
+
const schema = ctx.args[argKey];
|
|
211
|
+
if (!schema) return argKey;
|
|
212
|
+
return negatable && schema.type === "boolean" && schema.negatable ? `${en_US_default["NEGATABLE"]} ${makeShortLongOptionPair(schema, argKey, ctx.toKebab)}` : schema.description || "";
|
|
213
|
+
}
|
|
214
|
+
if (key === resolveKey("description", ctx)) return "";
|
|
215
|
+
else if (key === resolveKey("examples", ctx)) return await resolveExamples(ctx, cmd.examples);
|
|
216
|
+
else return key;
|
|
217
|
+
}
|
|
218
|
+
return localize;
|
|
219
|
+
}
|
|
150
220
|
|
|
151
221
|
//#endregion
|
|
152
|
-
export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, en_US_default as DefaultResource, PLUGIN_PREFIX, create, deepFreeze, isLazyCommand, kebabnize, log, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveLazyCommand };
|
|
222
|
+
export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, en_US_default as DefaultResource, PLUGIN_PREFIX, create, deepFreeze, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/shared",
|
|
3
3
|
"description": "shared utils for gunshi",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.27.0-alpha.10",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -50,13 +50,13 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"deno": "^2.
|
|
54
|
-
"jsr": "^0.13.
|
|
53
|
+
"deno": "^2.4.2",
|
|
54
|
+
"jsr": "^0.13.5",
|
|
55
55
|
"jsr-exports-lint": "^0.4.1",
|
|
56
56
|
"publint": "^0.3.12",
|
|
57
|
-
"tsdown": "^0.
|
|
58
|
-
"gunshi": "0.
|
|
59
|
-
"
|
|
57
|
+
"tsdown": "^0.13.0",
|
|
58
|
+
"@gunshi/resources": "0.27.0-alpha.10",
|
|
59
|
+
"gunshi": "0.27.0-alpha.10"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|