@gunshi/shared 0.27.0-alpha.8 → 0.27.0-alpha.9
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/lib/index.d.ts +96 -13
- package/lib/index.js +44 -9
- package/package.json +5 -5
package/lib/index.d.ts
CHANGED
|
@@ -448,6 +448,33 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
448
448
|
* @since v0.27.0
|
|
449
449
|
*/
|
|
450
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
|
+
}
|
|
451
478
|
/**
|
|
452
479
|
* Command interface.
|
|
453
480
|
*/
|
|
@@ -494,6 +521,11 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
494
521
|
* @since v0.27.0
|
|
495
522
|
*/
|
|
496
523
|
entry?: boolean;
|
|
524
|
+
/**
|
|
525
|
+
* Rendering control options
|
|
526
|
+
* @since v0.27.0
|
|
527
|
+
*/
|
|
528
|
+
rendering?: RenderingOptions<G>;
|
|
497
529
|
}
|
|
498
530
|
/**
|
|
499
531
|
* Lazy command interface.
|
|
@@ -603,37 +635,88 @@ type CommandBuiltinArgsKeys = keyof (typeof constants_d_exports)['COMMON_ARGS'];
|
|
|
603
635
|
*/
|
|
604
636
|
type CommandBuiltinResourceKeys = (typeof constants_d_exports)['COMMAND_BUILTIN_RESOURCE_KEYS'][number];
|
|
605
637
|
/**
|
|
606
|
-
*
|
|
638
|
+
* Built-in resource keys.
|
|
607
639
|
*/
|
|
608
640
|
type BuiltinResourceKeys = CommandBuiltinArgsKeys | CommandBuiltinResourceKeys;
|
|
609
641
|
/**
|
|
610
|
-
* Command
|
|
611
|
-
* The command i18n built-in keys are used by the i18n plugin for translation.
|
|
642
|
+
* Command built-in keys.
|
|
612
643
|
*/
|
|
613
|
-
type CommandBuiltinKeys = GenerateNamespacedKey<BuiltinResourceKeys
|
|
644
|
+
type CommandBuiltinKeys = GenerateNamespacedKey<BuiltinResourceKeys>;
|
|
614
645
|
/**
|
|
615
646
|
* Command i18n option keys.
|
|
616
647
|
* The command i18n option keys are used by the i18n plugin for translation.
|
|
617
648
|
*/
|
|
618
|
-
type CommandArgKeys<A extends Args
|
|
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;
|
|
619
662
|
/**
|
|
620
663
|
* Translation function interface
|
|
621
664
|
*/
|
|
622
|
-
interface Translation<
|
|
623
|
-
|
|
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;
|
|
624
671
|
}
|
|
625
672
|
//#endregion
|
|
626
|
-
//#region src/
|
|
627
|
-
interface Localization<
|
|
628
|
-
|
|
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;
|
|
629
678
|
}
|
|
630
|
-
|
|
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>;
|
|
631
692
|
//#endregion
|
|
632
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
|
+
*/
|
|
633
700
|
declare function resolveBuiltInKey<K extends string = CommandBuiltinArgsKeys | CommandBuiltinResourceKeys>(key: K): GenerateNamespacedKey<K>;
|
|
634
|
-
|
|
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;
|
|
635
718
|
declare function resolveExamples<G extends GunshiParamsConstraint = DefaultGunshiParams>(ctx: Readonly<CommandContext<G>>, examples?: string | CommandExamplesFetcher<G>): Promise<string>;
|
|
636
719
|
declare function namespacedId<K extends string>(id: K): GenerateNamespacedKey<K, typeof PLUGIN_PREFIX>;
|
|
637
720
|
declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKebab?: boolean): string;
|
|
638
721
|
//#endregion
|
|
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 };
|
|
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
|
@@ -139,11 +139,35 @@ var en_US_default = {
|
|
|
139
139
|
|
|
140
140
|
//#endregion
|
|
141
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
|
+
*/
|
|
142
148
|
function resolveBuiltInKey(key) {
|
|
143
149
|
return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
144
150
|
}
|
|
145
|
-
|
|
146
|
-
|
|
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}`;
|
|
147
171
|
}
|
|
148
172
|
async function resolveExamples(ctx, examples) {
|
|
149
173
|
return typeof examples === "string" ? examples : typeof examples === "function" ? await examples(ctx) : "";
|
|
@@ -159,15 +183,25 @@ function makeShortLongOptionPair(schema, name, toKebab) {
|
|
|
159
183
|
}
|
|
160
184
|
|
|
161
185
|
//#endregion
|
|
162
|
-
//#region src/
|
|
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
|
+
*/
|
|
163
195
|
function localizable(ctx, cmd, translate) {
|
|
164
196
|
async function localize(key, values) {
|
|
165
197
|
if (translate) return translate(key, values);
|
|
166
|
-
|
|
198
|
+
if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) {
|
|
167
199
|
const resKey = key.slice(BUILD_IN_PREFIX_AND_KEY_SEPARATOR.length);
|
|
168
200
|
return en_US_default[resKey] || key;
|
|
169
|
-
}
|
|
170
|
-
|
|
201
|
+
}
|
|
202
|
+
const namaspacedArgKey = resolveKey(ARG_PREFIX_AND_KEY_SEPARATOR, ctx);
|
|
203
|
+
if (key.startsWith(namaspacedArgKey)) {
|
|
204
|
+
let argKey = key.slice(namaspacedArgKey.length);
|
|
171
205
|
let negatable = false;
|
|
172
206
|
if (argKey.startsWith(ARG_NEGATABLE_PREFIX)) {
|
|
173
207
|
argKey = argKey.slice(ARG_NEGATABLE_PREFIX.length);
|
|
@@ -176,12 +210,13 @@ function localizable(ctx, cmd, translate) {
|
|
|
176
210
|
const schema = ctx.args[argKey];
|
|
177
211
|
if (!schema) return argKey;
|
|
178
212
|
return negatable && schema.type === "boolean" && schema.negatable ? `${en_US_default["NEGATABLE"]} ${makeShortLongOptionPair(schema, argKey, ctx.toKebab)}` : schema.description || "";
|
|
179
|
-
}
|
|
180
|
-
|
|
213
|
+
}
|
|
214
|
+
if (key === resolveKey("description", ctx)) return "";
|
|
215
|
+
else if (key === resolveKey("examples", ctx)) return await resolveExamples(ctx, cmd.examples);
|
|
181
216
|
else return key;
|
|
182
217
|
}
|
|
183
218
|
return localize;
|
|
184
219
|
}
|
|
185
220
|
|
|
186
221
|
//#endregion
|
|
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 };
|
|
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.27.0-alpha.
|
|
4
|
+
"version": "0.27.0-alpha.9",
|
|
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.4.
|
|
53
|
+
"deno": "^2.4.2",
|
|
54
54
|
"jsr": "^0.13.5",
|
|
55
55
|
"jsr-exports-lint": "^0.4.1",
|
|
56
56
|
"publint": "^0.3.12",
|
|
57
|
-
"tsdown": "^0.
|
|
58
|
-
"@gunshi/resources": "0.27.0-alpha.
|
|
59
|
-
"gunshi": "0.27.0-alpha.
|
|
57
|
+
"tsdown": "^0.13.0",
|
|
58
|
+
"@gunshi/resources": "0.27.0-alpha.9",
|
|
59
|
+
"gunshi": "0.27.0-alpha.9"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|