@gunshi/shared 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 +84 -25
- package/lib/index.js +40 -4
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/parser-DT7Ztcch.d.ts
|
|
2
2
|
//#region src/parser.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Entry point of argument parser.
|
|
@@ -55,7 +55,7 @@ interface ArgToken {
|
|
|
55
55
|
* Parser Options.
|
|
56
56
|
*/
|
|
57
57
|
//#endregion
|
|
58
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
58
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/resolver.d.ts
|
|
59
59
|
//#region src/resolver.d.ts
|
|
60
60
|
/**
|
|
61
61
|
* An argument schema definition for command-line argument parsing.
|
|
@@ -476,7 +476,7 @@ interface ArgSchema {
|
|
|
476
476
|
*
|
|
477
477
|
* @param value - Raw string value from command line
|
|
478
478
|
* @returns Parsed value of any type
|
|
479
|
-
* @throws Error or subclass when value is invalid
|
|
479
|
+
* @throws {Error} Error or subclass when value is invalid
|
|
480
480
|
*
|
|
481
481
|
* @example
|
|
482
482
|
* Custom parsing functions:
|
|
@@ -508,6 +508,13 @@ interface ArgSchema {
|
|
|
508
508
|
*/
|
|
509
509
|
parse?: (value: string) => any;
|
|
510
510
|
}
|
|
511
|
+
/**
|
|
512
|
+
* Machine-readable error codes for {@link ArgsValidationError}.
|
|
513
|
+
*
|
|
514
|
+
* Each code identifies a validation failure category and is also suitable as an
|
|
515
|
+
* i18n resource key for localized rendering.
|
|
516
|
+
*/
|
|
517
|
+
|
|
511
518
|
/**
|
|
512
519
|
* An object that contains {@link ArgSchema | argument schema}.
|
|
513
520
|
*
|
|
@@ -759,6 +766,13 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
759
766
|
* @see {@linkcode CliOptions.usageSilent}
|
|
760
767
|
*/
|
|
761
768
|
usageSilent: boolean;
|
|
769
|
+
/**
|
|
770
|
+
* Whether to treat undefined options as argument validation errors.
|
|
771
|
+
*
|
|
772
|
+
* @default false
|
|
773
|
+
* @see {@linkcode CliOptions.strict}
|
|
774
|
+
*/
|
|
775
|
+
strict: boolean;
|
|
762
776
|
/**
|
|
763
777
|
* Sub commands.
|
|
764
778
|
*
|
|
@@ -1112,7 +1126,7 @@ type CommandExamplesFetcher<G extends GunshiParamsConstraint = DefaultGunshiPara
|
|
|
1112
1126
|
*/
|
|
1113
1127
|
type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<string | void>;
|
|
1114
1128
|
//#endregion
|
|
1115
|
-
//#region ../../node_modules/.pnpm/args-tokens@0.
|
|
1129
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/utils.d.ts
|
|
1116
1130
|
//#region src/utils.d.ts
|
|
1117
1131
|
/**
|
|
1118
1132
|
* Entry point of utils.
|
|
@@ -1132,7 +1146,12 @@ type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ct
|
|
|
1132
1146
|
* @returns Converted string into kebab-case.
|
|
1133
1147
|
*/
|
|
1134
1148
|
declare function kebabnize(str: string): string;
|
|
1135
|
-
|
|
1149
|
+
/**
|
|
1150
|
+
* Format allowed argument choices for validation error values and fallback messages.
|
|
1151
|
+
*
|
|
1152
|
+
* @param choices - Choice values to format
|
|
1153
|
+
* @returns JSON-quoted choices joined with `, `.
|
|
1154
|
+
*/
|
|
1136
1155
|
//#endregion
|
|
1137
1156
|
//#region ../gunshi/src/utils.d.ts
|
|
1138
1157
|
/**
|
|
@@ -1188,9 +1207,11 @@ declare function deepFreeze<T extends Record<string, any>>(obj: T, ignores?: str
|
|
|
1188
1207
|
declare const BUILT_IN_PREFIX = "_";
|
|
1189
1208
|
declare const PLUGIN_PREFIX = "g";
|
|
1190
1209
|
declare const ARG_PREFIX = "arg";
|
|
1210
|
+
declare const ERROR_PREFIX = "err";
|
|
1191
1211
|
declare const BUILT_IN_KEY_SEPARATOR = ":";
|
|
1192
1212
|
declare const BUILD_IN_PREFIX_AND_KEY_SEPARATOR: string;
|
|
1193
1213
|
declare const ARG_PREFIX_AND_KEY_SEPARATOR: string;
|
|
1214
|
+
declare const ERROR_PREFIX_AND_KEY_SEPARATOR: string;
|
|
1194
1215
|
declare const ARG_NEGATABLE_PREFIX = "no-";
|
|
1195
1216
|
type CommonArgType = {
|
|
1196
1217
|
readonly help: {
|
|
@@ -1206,6 +1227,9 @@ type CommonArgType = {
|
|
|
1206
1227
|
};
|
|
1207
1228
|
declare const COMMON_ARGS: CommonArgType;
|
|
1208
1229
|
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
1230
|
+
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"];
|
|
1231
|
+
declare const COMMAND_ERROR_RESOURCE_KEYS: readonly ["err:cmd:not-found"];
|
|
1232
|
+
declare const SUGGESTION_ERROR_RESOURCE_KEYS: readonly ["err:suggestion:did-you-mean"];
|
|
1209
1233
|
//#endregion
|
|
1210
1234
|
//#region src/types.d.ts
|
|
1211
1235
|
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K] };
|
|
@@ -1233,14 +1257,34 @@ type CommandBuiltinArgsKeys = keyof typeof COMMON_ARGS;
|
|
|
1233
1257
|
* Command i18n built-in resource keys.
|
|
1234
1258
|
*/
|
|
1235
1259
|
type CommandBuiltinResourceKeys = (typeof COMMAND_BUILTIN_RESOURCE_KEYS)[number];
|
|
1260
|
+
/**
|
|
1261
|
+
* Args validation error resource keys.
|
|
1262
|
+
*/
|
|
1263
|
+
type ArgErrorResourceKeys = (typeof ARG_ERROR_RESOURCE_KEYS)[number];
|
|
1264
|
+
/**
|
|
1265
|
+
* Command validation error resource keys.
|
|
1266
|
+
*/
|
|
1267
|
+
type CommandErrorResourceKeys = (typeof COMMAND_ERROR_RESOURCE_KEYS)[number];
|
|
1268
|
+
/**
|
|
1269
|
+
* Suggestion error resource keys.
|
|
1270
|
+
*/
|
|
1271
|
+
type SuggestionErrorResourceKeys = (typeof SUGGESTION_ERROR_RESOURCE_KEYS)[number];
|
|
1272
|
+
/**
|
|
1273
|
+
* Error resource keys.
|
|
1274
|
+
*/
|
|
1275
|
+
type ErrorResourceKeys = ArgErrorResourceKeys | CommandErrorResourceKeys | SuggestionErrorResourceKeys;
|
|
1236
1276
|
/**
|
|
1237
1277
|
* Built-in resource keys.
|
|
1238
1278
|
*/
|
|
1239
|
-
type BuiltinResourceKeys = CommandBuiltinArgsKeys | CommandBuiltinResourceKeys;
|
|
1279
|
+
type BuiltinResourceKeys = ErrorResourceKeys | CommandBuiltinArgsKeys | CommandBuiltinResourceKeys;
|
|
1280
|
+
/**
|
|
1281
|
+
* Built-in resource.
|
|
1282
|
+
*/
|
|
1283
|
+
type BuiltinResource = Partial<Record<BuiltinResourceKeys, string>>;
|
|
1240
1284
|
/**
|
|
1241
1285
|
* Command built-in keys.
|
|
1242
1286
|
*/
|
|
1243
|
-
type CommandBuiltinKeys = GenerateNamespacedKey<
|
|
1287
|
+
type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinArgsKeys | CommandBuiltinResourceKeys>;
|
|
1244
1288
|
/**
|
|
1245
1289
|
* Command i18n option keys.
|
|
1246
1290
|
* The command i18n option keys are used by the i18n plugin for translation.
|
|
@@ -1257,7 +1301,7 @@ E extends Record<string, string> = {},
|
|
|
1257
1301
|
// for extended resources
|
|
1258
1302
|
R extends string = keyof RemovedIndex<E>, T extends string = (C extends {
|
|
1259
1303
|
name: infer N;
|
|
1260
|
-
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys), O = CommandArgKeys<A, C>> = CommandBuiltinKeys | O | T;
|
|
1304
|
+
} ? N extends string ? GenerateNamespacedKey<R, N> : R : R | CommandBuiltinKeys), O = CommandArgKeys<A, C>> = ErrorResourceKeys | CommandBuiltinKeys | O | T;
|
|
1261
1305
|
/**
|
|
1262
1306
|
* Translation function interface
|
|
1263
1307
|
*/
|
|
@@ -1301,24 +1345,39 @@ declare function localizable<A extends Args, C = {},
|
|
|
1301
1345
|
E extends Record<string, string> = {},
|
|
1302
1346
|
// for extended resources
|
|
1303
1347
|
K = ResolveTranslationKeys<A, C, E>>(ctx: CommandContext, cmd: Command, translate?: Translation<A, C, E, K>): Localization<A, C, E>;
|
|
1348
|
+
/**
|
|
1349
|
+
* Format a resource message by replacing placeholder values.
|
|
1350
|
+
*
|
|
1351
|
+
* @param resource - Resource message template
|
|
1352
|
+
* @param values - Placeholder values
|
|
1353
|
+
* @returns Formatted resource message
|
|
1354
|
+
*/
|
|
1355
|
+
declare function formatResource(resource: string | undefined, values?: Record<string, unknown>): string | undefined;
|
|
1304
1356
|
//#endregion
|
|
1305
1357
|
//#region ../resources/locales/en-US.d.ts
|
|
1306
|
-
declare
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1358
|
+
declare const _exports: {
|
|
1359
|
+
COMMAND: string;
|
|
1360
|
+
COMMANDS: string;
|
|
1361
|
+
SUBCOMMAND: string;
|
|
1362
|
+
USAGE: string;
|
|
1363
|
+
ARGUMENTS: string;
|
|
1364
|
+
OPTIONS: string;
|
|
1365
|
+
EXAMPLES: string;
|
|
1366
|
+
FORMORE: string;
|
|
1367
|
+
NEGATABLE: string;
|
|
1368
|
+
DEFAULT: string;
|
|
1369
|
+
CHOICES: string;
|
|
1370
|
+
help: string;
|
|
1371
|
+
version: string;
|
|
1372
|
+
"err:arg:required-option": string;
|
|
1373
|
+
"err:arg:required-positional": string;
|
|
1374
|
+
"err:arg:invalid-type": string;
|
|
1375
|
+
"err:arg:invalid-choice": string;
|
|
1376
|
+
"err:arg:custom-parse": string;
|
|
1377
|
+
"err:arg:unknown-option": string;
|
|
1378
|
+
"err:cmd:not-found": string;
|
|
1379
|
+
"err:suggestion:did-you-mean": string;
|
|
1380
|
+
};
|
|
1322
1381
|
//#endregion
|
|
1323
1382
|
//#region src/utils.d.ts
|
|
1324
1383
|
/**
|
|
@@ -1393,4 +1452,4 @@ declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKeba
|
|
|
1393
1452
|
*/
|
|
1394
1453
|
|
|
1395
1454
|
//#endregion
|
|
1396
|
-
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, DeepWriteable,
|
|
1455
|
+
export { ARG_ERROR_RESOURCE_KEYS, ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, ArgErrorResourceKeys, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, BuiltinResource, BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_ERROR_RESOURCE_KEYS, COMMON_ARGS, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandErrorResourceKeys, DeepWriteable, _exports as DefaultResource, ERROR_PREFIX, ERROR_PREFIX_AND_KEY_SEPARATOR, ErrorResourceKeys, GenerateNamespacedKey, KeyOfArgs, Localization, PLUGIN_PREFIX, RemovedIndex, ResolveTranslationKeys, SUGGESTION_ERROR_RESOURCE_KEYS, SuggestionErrorResourceKeys, Translation, create, deepFreeze, formatResource, getCommandSubCommands, 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.28.0/node_modules/args-tokens/lib/utils.js
|
|
2
2
|
/**
|
|
3
3
|
* Entry point of utils.
|
|
4
4
|
*
|
|
@@ -136,9 +136,11 @@ function deepFreeze(obj, ignores = []) {
|
|
|
136
136
|
const BUILT_IN_PREFIX = "_";
|
|
137
137
|
const PLUGIN_PREFIX = "g";
|
|
138
138
|
const ARG_PREFIX = "arg";
|
|
139
|
+
const ERROR_PREFIX = "err";
|
|
139
140
|
const BUILT_IN_KEY_SEPARATOR = ":";
|
|
140
141
|
const BUILD_IN_PREFIX_AND_KEY_SEPARATOR = `_:`;
|
|
141
142
|
const ARG_PREFIX_AND_KEY_SEPARATOR = `arg:`;
|
|
143
|
+
const ERROR_PREFIX_AND_KEY_SEPARATOR = `err:`;
|
|
142
144
|
const ARG_NEGATABLE_PREFIX = "no-";
|
|
143
145
|
const COMMON_ARGS = {
|
|
144
146
|
help: {
|
|
@@ -165,6 +167,16 @@ const COMMAND_BUILTIN_RESOURCE_KEYS = [
|
|
|
165
167
|
"DEFAULT",
|
|
166
168
|
"CHOICES"
|
|
167
169
|
];
|
|
170
|
+
const ARG_ERROR_RESOURCE_KEYS = [
|
|
171
|
+
"err:arg:required-option",
|
|
172
|
+
"err:arg:required-positional",
|
|
173
|
+
"err:arg:invalid-type",
|
|
174
|
+
"err:arg:invalid-choice",
|
|
175
|
+
"err:arg:custom-parse",
|
|
176
|
+
"err:arg:unknown-option"
|
|
177
|
+
];
|
|
178
|
+
const COMMAND_ERROR_RESOURCE_KEYS = ["err:cmd:not-found"];
|
|
179
|
+
const SUGGESTION_ERROR_RESOURCE_KEYS = ["err:suggestion:did-you-mean"];
|
|
168
180
|
//#endregion
|
|
169
181
|
//#region ../resources/locales/en-US.json
|
|
170
182
|
var NEGATABLE = "Negatable of";
|
|
@@ -181,7 +193,15 @@ var en_US_default = {
|
|
|
181
193
|
DEFAULT: "default",
|
|
182
194
|
CHOICES: "choices",
|
|
183
195
|
help: "Display this help message",
|
|
184
|
-
version: "Display this version"
|
|
196
|
+
version: "Display this version",
|
|
197
|
+
"err:arg:required-option": "Optional argument {$displayName} is required",
|
|
198
|
+
"err:arg:required-positional": "Positional argument {$name} is required",
|
|
199
|
+
"err:arg:invalid-type": "Invalid value for {$displayName}: expected {$expected}",
|
|
200
|
+
"err:arg:invalid-choice": "Invalid value for {$displayName}: expected one of {$choices}",
|
|
201
|
+
"err:arg:custom-parse": "Invalid value for {$displayName}: {$reason}",
|
|
202
|
+
"err:arg:unknown-option": "Unknown option: {$rawName}",
|
|
203
|
+
"err:cmd:not-found": "Command not found: {$commandName}",
|
|
204
|
+
"err:suggestion:did-you-mean": "Did you mean {$name}?"
|
|
185
205
|
};
|
|
186
206
|
//#endregion
|
|
187
207
|
//#region src/utils.ts
|
|
@@ -290,7 +310,8 @@ function makeShortLongOptionPair(schema, name, toKebab) {
|
|
|
290
310
|
function localizable(ctx, cmd, translate) {
|
|
291
311
|
async function localize(key, values) {
|
|
292
312
|
if (translate) return translate(key, values);
|
|
293
|
-
if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) return
|
|
313
|
+
if (key.startsWith(BUILD_IN_PREFIX_AND_KEY_SEPARATOR)) return resolveAndFormatResource(key.slice(BUILD_IN_PREFIX_AND_KEY_SEPARATOR.length), key, values);
|
|
314
|
+
if (key.startsWith(ERROR_PREFIX_AND_KEY_SEPARATOR)) return resolveAndFormatResource(key, key, values);
|
|
294
315
|
const namaspacedArgKey = resolveKey(ARG_PREFIX_AND_KEY_SEPARATOR, ctx.name);
|
|
295
316
|
if (key.startsWith(namaspacedArgKey)) {
|
|
296
317
|
let argKey = key.slice(namaspacedArgKey.length);
|
|
@@ -309,6 +330,21 @@ function localizable(ctx, cmd, translate) {
|
|
|
309
330
|
}
|
|
310
331
|
return localize;
|
|
311
332
|
}
|
|
333
|
+
function resolveAndFormatResource(resourceKey, fallbackKey, values) {
|
|
334
|
+
return formatResource(en_US_default[resourceKey], values) || fallbackKey;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Format a resource message by replacing placeholder values.
|
|
338
|
+
*
|
|
339
|
+
* @param resource - Resource message template
|
|
340
|
+
* @param values - Placeholder values
|
|
341
|
+
* @returns Formatted resource message
|
|
342
|
+
*/
|
|
343
|
+
function formatResource(resource, values = Object.create(null)) {
|
|
344
|
+
return resource?.replaceAll(/\{\$(\w+)\}/g, (_, name) => {
|
|
345
|
+
return values[name] == null ? "" : String(values[name]);
|
|
346
|
+
});
|
|
347
|
+
}
|
|
312
348
|
//#endregion
|
|
313
349
|
//#region src/index.ts
|
|
314
350
|
/**
|
|
@@ -316,4 +352,4 @@ function localizable(ctx, cmd, translate) {
|
|
|
316
352
|
* @license MIT
|
|
317
353
|
*/
|
|
318
354
|
//#endregion
|
|
319
|
-
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, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
|
|
355
|
+
export { ARG_ERROR_RESOURCE_KEYS, 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, COMMAND_ERROR_RESOURCE_KEYS, COMMON_ARGS, en_US_default as DefaultResource, ERROR_PREFIX, ERROR_PREFIX_AND_KEY_SEPARATOR, PLUGIN_PREFIX, SUGGESTION_ERROR_RESOURCE_KEYS, create, deepFreeze, formatResource, getCommandSubCommands, 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.35.
|
|
4
|
+
"version": "0.35.3",
|
|
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.2",
|
|
56
56
|
"publint": "^0.3.20",
|
|
57
57
|
"tsdown": "0.21.0",
|
|
58
|
-
"@gunshi/resources": "0.35.
|
|
59
|
-
"gunshi": "0.35.
|
|
58
|
+
"@gunshi/resources": "0.35.3",
|
|
59
|
+
"gunshi": "0.35.3"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|