@gunshi/shared 0.27.0-alpha.6 → 0.27.0-alpha.7
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 +177 -102
- package/lib/index.js +38 -3
- package/package.json +3 -3
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,154 +3,189 @@ 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
56
|
}
|
|
57
57
|
/**
|
|
58
|
-
* Parser Options.
|
|
59
|
-
*/
|
|
58
|
+
* Parser Options.
|
|
59
|
+
*/
|
|
60
60
|
//#endregion
|
|
61
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
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
|
/**
|
|
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
|
-
*/
|
|
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
|
+
*/
|
|
72
72
|
interface ArgSchema {
|
|
73
73
|
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
type:
|
|
74
|
+
* Type of argument.
|
|
75
|
+
*/
|
|
76
|
+
type: 'string' | 'boolean' | 'number' | 'enum' | 'positional' | 'custom';
|
|
77
77
|
/**
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
* A single character alias for the argument.
|
|
79
|
+
*/
|
|
80
80
|
short?: string;
|
|
81
81
|
/**
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
* A description of the argument.
|
|
83
|
+
*/
|
|
84
84
|
description?: string;
|
|
85
85
|
/**
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
* Whether the argument is required or not.
|
|
87
|
+
*/
|
|
88
88
|
required?: true;
|
|
89
89
|
/**
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
* Whether the argument allow multiple values or not.
|
|
91
|
+
*/
|
|
92
92
|
multiple?: true;
|
|
93
93
|
/**
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
* Whether the negatable option for `boolean` type
|
|
95
|
+
*/
|
|
96
96
|
negatable?: boolean;
|
|
97
97
|
/**
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
99
|
+
*/
|
|
100
100
|
choices?: string[] | readonly string[];
|
|
101
101
|
/**
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
* The default value of the argument.
|
|
103
|
+
* if the type is 'enum', the default value must be one of the allowed values.
|
|
104
|
+
*/
|
|
105
105
|
default?: string | boolean | number;
|
|
106
106
|
/**
|
|
107
|
-
|
|
108
|
-
|
|
107
|
+
* Whether to convert the argument name to kebab-case.
|
|
108
|
+
*/
|
|
109
109
|
toKebab?: true;
|
|
110
110
|
/**
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// 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
|
+
*/
|
|
118
117
|
parse?: (value: string) => any;
|
|
119
118
|
}
|
|
120
119
|
/**
|
|
121
|
-
* An object that contains {@link ArgSchema | argument schema}.
|
|
122
|
-
*/
|
|
120
|
+
* An object that contains {@link ArgSchema | argument schema}.
|
|
121
|
+
*/
|
|
123
122
|
interface Args {
|
|
124
123
|
[option: string]: ArgSchema;
|
|
125
124
|
}
|
|
126
125
|
/**
|
|
127
|
-
* An object that contains the values of the arguments.
|
|
128
|
-
*/
|
|
126
|
+
* An object that contains the values of the arguments.
|
|
127
|
+
*/
|
|
129
128
|
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
130
129
|
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
131
130
|
};
|
|
132
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
133
131
|
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
134
132
|
/**
|
|
135
|
-
* @internal
|
|
136
|
-
*/
|
|
137
|
-
type ExtractOptionValue<A extends ArgSchema> = A[
|
|
138
|
-
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;
|
|
139
137
|
/**
|
|
140
|
-
* @internal
|
|
141
|
-
*/
|
|
142
|
-
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;
|
|
143
141
|
/**
|
|
144
|
-
* @internal
|
|
145
|
-
*/
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
146
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] };
|
|
147
145
|
/**
|
|
148
|
-
* @internal
|
|
149
|
-
*/
|
|
150
|
-
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
|
+
*/
|
|
152
|
+
|
|
151
153
|
/**
|
|
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
|
+
*/
|
|
154
189
|
//#endregion
|
|
155
190
|
//#region ../gunshi/src/types.d.ts
|
|
156
191
|
type Awaitable<T> = T | Promise<T>;
|
|
@@ -202,6 +237,11 @@ type GunshiParamsConstraint = GunshiParams<any> | {
|
|
|
202
237
|
* @internal
|
|
203
238
|
*/
|
|
204
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>>;
|
|
205
245
|
/**
|
|
206
246
|
* Type helper to extract extensions from G
|
|
207
247
|
* @internal
|
|
@@ -338,6 +378,15 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
338
378
|
* The command arguments is same {@link Command.args}.
|
|
339
379
|
*/
|
|
340
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>;
|
|
341
390
|
/**
|
|
342
391
|
* Command values, that is the values of the command that is executed.
|
|
343
392
|
* Resolve values with `resolveArgs` from command arguments and {@link Command.args}.
|
|
@@ -432,6 +481,19 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
432
481
|
* If you will set to `true`, All {@link Command.args} names will be converted to kebab-case.
|
|
433
482
|
*/
|
|
434
483
|
toKebab?: boolean;
|
|
484
|
+
/**
|
|
485
|
+
* Whether this is an internal command.
|
|
486
|
+
* Internal commands are not shown in help usage.
|
|
487
|
+
* @default false
|
|
488
|
+
* @since v0.27.0
|
|
489
|
+
*/
|
|
490
|
+
internal?: boolean;
|
|
491
|
+
/**
|
|
492
|
+
* Whether this command is an entry command.
|
|
493
|
+
* @default undefined
|
|
494
|
+
* @since v0.27.0
|
|
495
|
+
*/
|
|
496
|
+
entry?: boolean;
|
|
435
497
|
}
|
|
436
498
|
/**
|
|
437
499
|
* Lazy command interface.
|
|
@@ -470,19 +532,19 @@ type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ct
|
|
|
470
532
|
* @returns A command or command runner
|
|
471
533
|
*/
|
|
472
534
|
//#endregion
|
|
473
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
535
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.22.0/node_modules/args-tokens/lib/utils.d.ts
|
|
474
536
|
//#region src/utils.d.ts
|
|
475
537
|
/**
|
|
476
|
-
* Entry point of utils.
|
|
477
|
-
*
|
|
478
|
-
* Note that this entry point is used by gunshi to import utility functions.
|
|
479
|
-
*
|
|
480
|
-
* @module
|
|
481
|
-
*/
|
|
538
|
+
* Entry point of utils.
|
|
539
|
+
*
|
|
540
|
+
* Note that this entry point is used by gunshi to import utility functions.
|
|
541
|
+
*
|
|
542
|
+
* @module
|
|
543
|
+
*/
|
|
482
544
|
/**
|
|
483
|
-
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
484
|
-
* @license MIT
|
|
485
|
-
*/
|
|
545
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
546
|
+
* @license MIT
|
|
547
|
+
*/
|
|
486
548
|
declare function kebabnize(str: string): string;
|
|
487
549
|
//#endregion
|
|
488
550
|
//#endregion
|
|
@@ -554,11 +616,24 @@ type CommandBuiltinKeys = GenerateNamespacedKey<BuiltinResourceKeys> | 'descript
|
|
|
554
616
|
* The command i18n option keys are used by the i18n plugin for translation.
|
|
555
617
|
*/
|
|
556
618
|
type CommandArgKeys<A extends Args> = GenerateNamespacedKey<KeyOfArgs<RemovedIndex<A>>, typeof ARG_PREFIX>;
|
|
619
|
+
/**
|
|
620
|
+
* Translation function interface
|
|
621
|
+
*/
|
|
622
|
+
interface Translation<T extends string = CommandBuiltinKeys, G extends GunshiParams<any> = DefaultGunshiParams> {
|
|
623
|
+
<O = CommandArgKeys<G['args']>, K = CommandBuiltinKeys | O | T>(key: K, values?: Record<string, unknown>): string;
|
|
624
|
+
}
|
|
625
|
+
//#endregion
|
|
626
|
+
//#region src/localize.d.ts
|
|
627
|
+
interface Localization<T extends string = CommandBuiltinKeys, G extends GunshiParams<any> = DefaultGunshiParams> {
|
|
628
|
+
<O = CommandArgKeys<G['args']>, K = CommandBuiltinKeys | O | T>(key: K, values?: Record<string, unknown>): Promise<string>;
|
|
629
|
+
}
|
|
630
|
+
declare function localizable<T extends string = CommandBuiltinKeys>(ctx: CommandContext, cmd: Command, translate?: Translation<T>): Localization<T>;
|
|
557
631
|
//#endregion
|
|
558
632
|
//#region src/utils.d.ts
|
|
559
633
|
declare function resolveBuiltInKey<K extends string = CommandBuiltinArgsKeys | CommandBuiltinResourceKeys>(key: K): GenerateNamespacedKey<K>;
|
|
560
634
|
declare function resolveArgKey<A extends Args = DefaultGunshiParams['args'], K extends string = KeyOfArgs<RemovedIndex<A>>>(key: K): GenerateNamespacedKey<K, typeof ARG_PREFIX>;
|
|
561
635
|
declare function resolveExamples<G extends GunshiParamsConstraint = DefaultGunshiParams>(ctx: Readonly<CommandContext<G>>, examples?: string | CommandExamplesFetcher<G>): Promise<string>;
|
|
562
636
|
declare function namespacedId<K extends string>(id: K): GenerateNamespacedKey<K, typeof PLUGIN_PREFIX>;
|
|
637
|
+
declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKebab?: boolean): string;
|
|
563
638
|
//#endregion
|
|
564
|
-
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 };
|
|
639
|
+
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, Translation, create, deepFreeze, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, 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
|
}
|
|
@@ -147,6 +151,37 @@ async function resolveExamples(ctx, examples) {
|
|
|
147
151
|
function namespacedId(id) {
|
|
148
152
|
return `${PLUGIN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${id}`;
|
|
149
153
|
}
|
|
154
|
+
function makeShortLongOptionPair(schema, name, toKebab) {
|
|
155
|
+
const displayName = toKebab || schema.toKebab ? kebabnize(name) : name;
|
|
156
|
+
let key = `--${displayName}`;
|
|
157
|
+
if (schema.short) key = `-${schema.short}, ${key}`;
|
|
158
|
+
return key;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/localize.ts
|
|
163
|
+
function localizable(ctx, cmd, translate) {
|
|
164
|
+
async function localize(key, values) {
|
|
165
|
+
if (translate) return translate(key, values);
|
|
166
|
+
else if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) {
|
|
167
|
+
const resKey = key.slice(BUILD_IN_PREFIX_AND_KEY_SEPARATOR.length);
|
|
168
|
+
return en_US_default[resKey] || key;
|
|
169
|
+
} else if (key.startsWith(ARG_PREFIX_AND_KEY_SEPARATOR)) {
|
|
170
|
+
let argKey = key.slice(ARG_PREFIX_AND_KEY_SEPARATOR.length);
|
|
171
|
+
let negatable = false;
|
|
172
|
+
if (argKey.startsWith(ARG_NEGATABLE_PREFIX)) {
|
|
173
|
+
argKey = argKey.slice(ARG_NEGATABLE_PREFIX.length);
|
|
174
|
+
negatable = true;
|
|
175
|
+
}
|
|
176
|
+
const schema = ctx.args[argKey];
|
|
177
|
+
if (!schema) return argKey;
|
|
178
|
+
return negatable && schema.type === "boolean" && schema.negatable ? `${en_US_default["NEGATABLE"]} ${makeShortLongOptionPair(schema, argKey, ctx.toKebab)}` : schema.description || "";
|
|
179
|
+
} else if (key === "description") return "";
|
|
180
|
+
else if (key === "examples") return await resolveExamples(ctx, cmd.examples);
|
|
181
|
+
else return key;
|
|
182
|
+
}
|
|
183
|
+
return localize;
|
|
184
|
+
}
|
|
150
185
|
|
|
151
186
|
//#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 };
|
|
187
|
+
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, 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.27.0-alpha.
|
|
4
|
+
"version": "0.27.0-alpha.7",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"jsr-exports-lint": "^0.4.1",
|
|
56
56
|
"publint": "^0.3.12",
|
|
57
57
|
"tsdown": "^0.12.9",
|
|
58
|
-
"@gunshi/resources": "0.27.0-alpha.
|
|
59
|
-
"gunshi": "0.27.0-alpha.
|
|
58
|
+
"@gunshi/resources": "0.27.0-alpha.7",
|
|
59
|
+
"gunshi": "0.27.0-alpha.7"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|