@gunshi/plugin-renderer 0.35.1 → 0.35.3
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 +21 -6
- package/lib/index.js +66 -8
- package/package.json +4 -4
package/lib/index.d.ts
CHANGED
|
@@ -35,6 +35,9 @@ type CommonArgType = {
|
|
|
35
35
|
};
|
|
36
36
|
declare const COMMON_ARGS: CommonArgType;
|
|
37
37
|
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
38
|
+
declare const ARG_ERROR_RESOURCE_KEYS: readonly ["err:arg:required-option", "err:arg:required-positional", "err:arg:invalid-type", "err:arg:invalid-choice", "err:arg:custom-parse", "err:arg:unknown-option"];
|
|
39
|
+
declare const COMMAND_ERROR_RESOURCE_KEYS: readonly ["err:cmd:not-found"];
|
|
40
|
+
declare const SUGGESTION_ERROR_RESOURCE_KEYS: readonly ["err:suggestion:did-you-mean"];
|
|
38
41
|
//#endregion
|
|
39
42
|
//#region ../plugin-i18n/src/types.d.ts
|
|
40
43
|
|
|
@@ -75,13 +78,25 @@ type CommandBuiltinArgsKeys = keyof typeof COMMON_ARGS;
|
|
|
75
78
|
*/
|
|
76
79
|
type CommandBuiltinResourceKeys = (typeof COMMAND_BUILTIN_RESOURCE_KEYS)[number];
|
|
77
80
|
/**
|
|
78
|
-
*
|
|
81
|
+
* Args validation error resource keys.
|
|
79
82
|
*/
|
|
80
|
-
type
|
|
83
|
+
type ArgErrorResourceKeys = (typeof ARG_ERROR_RESOURCE_KEYS)[number];
|
|
84
|
+
/**
|
|
85
|
+
* Command validation error resource keys.
|
|
86
|
+
*/
|
|
87
|
+
type CommandErrorResourceKeys = (typeof COMMAND_ERROR_RESOURCE_KEYS)[number];
|
|
88
|
+
/**
|
|
89
|
+
* Suggestion error resource keys.
|
|
90
|
+
*/
|
|
91
|
+
type SuggestionErrorResourceKeys = (typeof SUGGESTION_ERROR_RESOURCE_KEYS)[number];
|
|
92
|
+
/**
|
|
93
|
+
* Error resource keys.
|
|
94
|
+
*/
|
|
95
|
+
type ErrorResourceKeys = ArgErrorResourceKeys | CommandErrorResourceKeys | SuggestionErrorResourceKeys;
|
|
81
96
|
/**
|
|
82
97
|
* Command built-in keys.
|
|
83
98
|
*/
|
|
84
|
-
type CommandBuiltinKeys = GenerateNamespacedKey<
|
|
99
|
+
type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinArgsKeys | CommandBuiltinResourceKeys>;
|
|
85
100
|
/**
|
|
86
101
|
* Command i18n option keys.
|
|
87
102
|
* The command i18n option keys are used by the i18n plugin for translation.
|
|
@@ -98,7 +113,7 @@ E extends Record<string, string> = {},
|
|
|
98
113
|
// for extended resources
|
|
99
114
|
R extends string = keyof RemovedIndex<E>, T extends string = (C extends {
|
|
100
115
|
name: infer N;
|
|
101
|
-
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys), O = CommandArgKeys<A, C>> = CommandBuiltinKeys | O | T;
|
|
116
|
+
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys), O = CommandArgKeys<A, C>> = ErrorResourceKeys | CommandBuiltinKeys | O | T;
|
|
102
117
|
//#endregion
|
|
103
118
|
//#region ../shared/src/localization.d.ts
|
|
104
119
|
|
|
@@ -173,11 +188,11 @@ declare function renderUsage<G extends GunshiParams = DefaultGunshiParams>(ctx:
|
|
|
173
188
|
/**
|
|
174
189
|
* Render the validation errors.
|
|
175
190
|
*
|
|
176
|
-
* @param
|
|
191
|
+
* @param ctx - A {@link CommandContext | command context}
|
|
177
192
|
* @param error - An {@link AggregateError} of option in `args-token` validation
|
|
178
193
|
* @returns A rendered validation error.
|
|
179
194
|
*/
|
|
180
|
-
declare function renderValidationErrors<G extends GunshiParams = DefaultGunshiParams>(
|
|
195
|
+
declare function renderValidationErrors<G extends GunshiParams = DefaultGunshiParams>(ctx: CommandContext<G>, error: AggregateError): Promise<string>;
|
|
181
196
|
//#endregion
|
|
182
197
|
//#region src/index.d.ts
|
|
183
198
|
/**
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ANONYMOUS_COMMAND_NAME, plugin } from "@gunshi/plugin";
|
|
2
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
import { ANONYMOUS_COMMAND_NAME, ArgsValidationErrorKeys, isArgsValidationError, isCommandNotFoundError, plugin } from "@gunshi/plugin";
|
|
2
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/utils.js
|
|
3
3
|
/**
|
|
4
4
|
* Entry point of utils.
|
|
5
5
|
*
|
|
@@ -108,6 +108,7 @@ function deepFreeze(obj, ignores = []) {
|
|
|
108
108
|
*/
|
|
109
109
|
const BUILD_IN_PREFIX_AND_KEY_SEPARATOR = `_:`;
|
|
110
110
|
const ARG_PREFIX_AND_KEY_SEPARATOR = `arg:`;
|
|
111
|
+
const ERROR_PREFIX_AND_KEY_SEPARATOR = `err:`;
|
|
111
112
|
const COMMON_ARGS = {
|
|
112
113
|
help: {
|
|
113
114
|
type: "boolean",
|
|
@@ -136,7 +137,15 @@ var en_US_default = {
|
|
|
136
137
|
DEFAULT: "default",
|
|
137
138
|
CHOICES: "choices",
|
|
138
139
|
help: "Display this help message",
|
|
139
|
-
version: "Display this version"
|
|
140
|
+
version: "Display this version",
|
|
141
|
+
"err:arg:required-option": "Optional argument {$displayName} is required",
|
|
142
|
+
"err:arg:required-positional": "Positional argument {$name} is required",
|
|
143
|
+
"err:arg:invalid-type": "Invalid value for {$displayName}: expected {$expected}",
|
|
144
|
+
"err:arg:invalid-choice": "Invalid value for {$displayName}: expected one of {$choices}",
|
|
145
|
+
"err:arg:custom-parse": "Invalid value for {$displayName}: {$reason}",
|
|
146
|
+
"err:arg:unknown-option": "Unknown option: {$rawName}",
|
|
147
|
+
"err:cmd:not-found": "Command not found: {$commandName}",
|
|
148
|
+
"err:suggestion:did-you-mean": "Did you mean {$name}?"
|
|
140
149
|
};
|
|
141
150
|
//#endregion
|
|
142
151
|
//#region ../shared/src/utils.ts
|
|
@@ -245,7 +254,8 @@ function makeShortLongOptionPair(schema, name, toKebab) {
|
|
|
245
254
|
function localizable(ctx, cmd, translate) {
|
|
246
255
|
async function localize(key, values) {
|
|
247
256
|
if (translate) return translate(key, values);
|
|
248
|
-
if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) return
|
|
257
|
+
if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) return resolveAndFormatResource(key.slice(BUILD_IN_PREFIX_AND_KEY_SEPARATOR.length), key, values);
|
|
258
|
+
if (key.startsWith(ERROR_PREFIX_AND_KEY_SEPARATOR)) return resolveAndFormatResource(key, key, values);
|
|
249
259
|
const namaspacedArgKey = resolveKey(ARG_PREFIX_AND_KEY_SEPARATOR, ctx.name);
|
|
250
260
|
if (key.startsWith(namaspacedArgKey)) {
|
|
251
261
|
let argKey = key.slice(namaspacedArgKey.length);
|
|
@@ -264,6 +274,21 @@ function localizable(ctx, cmd, translate) {
|
|
|
264
274
|
}
|
|
265
275
|
return localize;
|
|
266
276
|
}
|
|
277
|
+
function resolveAndFormatResource(resourceKey, fallbackKey, values) {
|
|
278
|
+
return formatResource(en_US_default[resourceKey], values) || fallbackKey;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Format a resource message by replacing placeholder values.
|
|
282
|
+
*
|
|
283
|
+
* @param resource - Resource message template
|
|
284
|
+
* @param values - Placeholder values
|
|
285
|
+
* @returns Formatted resource message
|
|
286
|
+
*/
|
|
287
|
+
function formatResource(resource, values = Object.create(null)) {
|
|
288
|
+
return resource?.replaceAll(/\{\$(\w+)\}/g, (_, name) => {
|
|
289
|
+
return values[name] == null ? "" : String(values[name]);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
267
292
|
//#endregion
|
|
268
293
|
//#region ../shared/src/index.ts
|
|
269
294
|
/**
|
|
@@ -626,17 +651,50 @@ function isRequiredSinglePositionalArg(arg) {
|
|
|
626
651
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
627
652
|
* @license MIT
|
|
628
653
|
*/
|
|
654
|
+
const i18nPluginId$1 = namespacedId("i18n");
|
|
629
655
|
/**
|
|
630
656
|
* Render the validation errors.
|
|
631
657
|
*
|
|
632
|
-
* @param
|
|
658
|
+
* @param ctx - A {@link CommandContext | command context}
|
|
633
659
|
* @param error - An {@link AggregateError} of option in `args-token` validation
|
|
634
660
|
* @returns A rendered validation error.
|
|
635
661
|
*/
|
|
636
|
-
function renderValidationErrors(
|
|
662
|
+
async function renderValidationErrors(ctx, error) {
|
|
637
663
|
const messages = [];
|
|
638
|
-
for (const err of error.errors) messages.push(err
|
|
639
|
-
return
|
|
664
|
+
for (const err of error.errors) messages.push(await renderValidationError(ctx, err));
|
|
665
|
+
return messages.join("\n");
|
|
666
|
+
}
|
|
667
|
+
async function renderValidationError(ctx, error) {
|
|
668
|
+
if (isCommandNotFoundError(error) && error.code) {
|
|
669
|
+
const message = await localize(ctx, error.code, error.values);
|
|
670
|
+
if (message && message !== error.code) return message;
|
|
671
|
+
}
|
|
672
|
+
if (isArgsValidationError(error) && error.code) {
|
|
673
|
+
const values = await resolveValidationValues(ctx, error);
|
|
674
|
+
const message = await localize(ctx, error.code, values);
|
|
675
|
+
if (message && message !== error.code) return message;
|
|
676
|
+
}
|
|
677
|
+
return error.message;
|
|
678
|
+
}
|
|
679
|
+
async function resolveValidationValues(ctx, error) {
|
|
680
|
+
if (error.code !== ArgsValidationErrorKeys.customParse) return error.values;
|
|
681
|
+
const { reasonKey, reasonValues } = error.values;
|
|
682
|
+
if (typeof reasonKey !== "string") return error.values;
|
|
683
|
+
const key = resolveKey(reasonKey, ctx.name);
|
|
684
|
+
const localizedReason = await localize(ctx, key, toRecord(reasonValues));
|
|
685
|
+
if (!localizedReason || localizedReason === key) return error.values;
|
|
686
|
+
return {
|
|
687
|
+
...error.values,
|
|
688
|
+
reason: localizedReason
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
async function localize(ctx, key, values) {
|
|
692
|
+
const i18n = ctx.extensions?.[i18nPluginId$1];
|
|
693
|
+
if (i18n?.translate) return i18n.translate(key, values);
|
|
694
|
+
return key;
|
|
695
|
+
}
|
|
696
|
+
function toRecord(value) {
|
|
697
|
+
return value != null && typeof value === "object" ? value : Object.create(null);
|
|
640
698
|
}
|
|
641
699
|
//#endregion
|
|
642
700
|
//#region src/index.ts
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/plugin-renderer",
|
|
3
3
|
"description": "usage renderer plugin for gunshi",
|
|
4
|
-
"version": "0.35.
|
|
4
|
+
"version": "0.35.3",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -53,10 +53,10 @@
|
|
|
53
53
|
}
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@gunshi/plugin": "0.35.
|
|
56
|
+
"@gunshi/plugin": "0.35.3"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@gunshi/plugin-i18n": "0.35.
|
|
59
|
+
"@gunshi/plugin-i18n": "0.35.3"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"deno": "^2.7.14",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"jsr-exports-lint": "^0.4.2",
|
|
65
65
|
"publint": "^0.3.20",
|
|
66
66
|
"tsdown": "0.21.0",
|
|
67
|
-
"@gunshi/shared": "0.35.
|
|
67
|
+
"@gunshi/shared": "0.35.3"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
70
|
"build": "tsdown",
|