@gunshi/plugin 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 +155 -4
- package/lib/index.js +146 -1
- package/package.json +2 -2
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,64 @@ 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
|
+
declare const ArgsValidationErrorKeys: {
|
|
518
|
+
readonly requiredOption: "err:arg:required-option";
|
|
519
|
+
readonly requiredPositional: "err:arg:required-positional";
|
|
520
|
+
readonly invalidType: "err:arg:invalid-type";
|
|
521
|
+
readonly invalidChoice: "err:arg:invalid-choice";
|
|
522
|
+
readonly customParse: "err:arg:custom-parse";
|
|
523
|
+
readonly unknownOption: "err:arg:unknown-option";
|
|
524
|
+
};
|
|
525
|
+
/**
|
|
526
|
+
* A machine-readable argument validation error code.
|
|
527
|
+
*
|
|
528
|
+
* Each code is one of {@link ArgsValidationErrorKeys} and can also be used as
|
|
529
|
+
* an i18n resource key.
|
|
530
|
+
*/
|
|
531
|
+
type ArgsValidationErrorCode = (typeof ArgsValidationErrorKeys)[keyof typeof ArgsValidationErrorKeys];
|
|
532
|
+
/**
|
|
533
|
+
* An error that contains structured metadata for argument validation failures.
|
|
534
|
+
*
|
|
535
|
+
* The `message` remains the English fallback message. Renderers can use `code`
|
|
536
|
+
* and `values` to localize the error, falling back to `message` when localization
|
|
537
|
+
* is unavailable.
|
|
538
|
+
*/
|
|
539
|
+
declare class ArgsValidationError extends Error {
|
|
540
|
+
/**
|
|
541
|
+
* Machine-readable error code for this validation failure.
|
|
542
|
+
*
|
|
543
|
+
* This code can also be used as an i18n resource key.
|
|
544
|
+
*/
|
|
545
|
+
readonly code?: ArgsValidationErrorCode;
|
|
546
|
+
/**
|
|
547
|
+
* Interpolation values for `code`.
|
|
548
|
+
*/
|
|
549
|
+
readonly values: Record<string, unknown>;
|
|
550
|
+
/**
|
|
551
|
+
* Create an `ArgsValidationError` instance.
|
|
552
|
+
*
|
|
553
|
+
* @param message - fallback error message
|
|
554
|
+
* @param options - structured validation metadata
|
|
555
|
+
*/
|
|
556
|
+
constructor(message: string, options?: {
|
|
557
|
+
code?: ArgsValidationErrorCode;
|
|
558
|
+
values?: Record<string, unknown>;
|
|
559
|
+
cause?: unknown;
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Check whether the given value is an {@link ArgsValidationError}.
|
|
564
|
+
*
|
|
565
|
+
* @param error - value to check
|
|
566
|
+
* @returns `true` when the value is an `ArgsValidationError`
|
|
567
|
+
*/
|
|
568
|
+
declare function isArgsValidationError(error: unknown): error is ArgsValidationError;
|
|
511
569
|
/**
|
|
512
570
|
* An object that contains {@link ArgSchema | argument schema}.
|
|
513
571
|
*
|
|
@@ -1132,6 +1190,13 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
|
|
|
1132
1190
|
* @see {@linkcode CliOptions.usageSilent}
|
|
1133
1191
|
*/
|
|
1134
1192
|
usageSilent: boolean;
|
|
1193
|
+
/**
|
|
1194
|
+
* Whether to treat undefined options as argument validation errors.
|
|
1195
|
+
*
|
|
1196
|
+
* @default false
|
|
1197
|
+
* @see {@linkcode CliOptions.strict}
|
|
1198
|
+
*/
|
|
1199
|
+
strict: boolean;
|
|
1135
1200
|
/**
|
|
1136
1201
|
* Sub commands.
|
|
1137
1202
|
*
|
|
@@ -1219,6 +1284,16 @@ interface CliOptions<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
1219
1284
|
* Whether to display the command usage.
|
|
1220
1285
|
*/
|
|
1221
1286
|
usageSilent?: boolean;
|
|
1287
|
+
/**
|
|
1288
|
+
* Whether to treat undefined options as argument validation errors.
|
|
1289
|
+
*
|
|
1290
|
+
* When enabled, option tokens that are not declared by the resolved command
|
|
1291
|
+
* arguments or installed global options are reported as validation errors
|
|
1292
|
+
* before the command runner is executed.
|
|
1293
|
+
*
|
|
1294
|
+
* @default false
|
|
1295
|
+
*/
|
|
1296
|
+
strict?: boolean;
|
|
1222
1297
|
/**
|
|
1223
1298
|
* Render function the command usage.
|
|
1224
1299
|
*/
|
|
@@ -1754,6 +1829,82 @@ declare function createCommandContext<G extends GunshiParamsConstraint = Default
|
|
|
1754
1829
|
validationError
|
|
1755
1830
|
}: CommandContextParams<G, V, C, E>): Promise<CommandContextResult<G, E>>;
|
|
1756
1831
|
//#endregion
|
|
1832
|
+
//#region ../gunshi/src/error.d.ts
|
|
1833
|
+
/**
|
|
1834
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
1835
|
+
* @license MIT
|
|
1836
|
+
*/
|
|
1837
|
+
/**
|
|
1838
|
+
* Command not found error resource keys.
|
|
1839
|
+
*/
|
|
1840
|
+
declare const CommandNotFoundErrorKeys: {
|
|
1841
|
+
readonly notFound: "err:cmd:not-found";
|
|
1842
|
+
};
|
|
1843
|
+
/**
|
|
1844
|
+
* Command not found error code.
|
|
1845
|
+
*/
|
|
1846
|
+
type CommandNotFoundErrorCode = (typeof CommandNotFoundErrorKeys)[keyof typeof CommandNotFoundErrorKeys];
|
|
1847
|
+
/**
|
|
1848
|
+
* Options for {@link CommandNotFoundError}.
|
|
1849
|
+
*/
|
|
1850
|
+
type CommandNotFoundErrorOptions = {
|
|
1851
|
+
/**
|
|
1852
|
+
* Localization resource key for command-not-found rendering.
|
|
1853
|
+
*/
|
|
1854
|
+
code?: CommandNotFoundErrorCode;
|
|
1855
|
+
/**
|
|
1856
|
+
* Values used when localizing the message.
|
|
1857
|
+
*/
|
|
1858
|
+
values?: Record<string, unknown>;
|
|
1859
|
+
/**
|
|
1860
|
+
* The command name that could not be resolved.
|
|
1861
|
+
*/
|
|
1862
|
+
commandName: string;
|
|
1863
|
+
/**
|
|
1864
|
+
* Command names available at the same level.
|
|
1865
|
+
*/
|
|
1866
|
+
candidates?: readonly string[];
|
|
1867
|
+
/**
|
|
1868
|
+
* Parent command path where resolution failed.
|
|
1869
|
+
*/
|
|
1870
|
+
commandPath?: readonly string[];
|
|
1871
|
+
/**
|
|
1872
|
+
* Underlying cause.
|
|
1873
|
+
*/
|
|
1874
|
+
cause?: unknown;
|
|
1875
|
+
};
|
|
1876
|
+
/**
|
|
1877
|
+
* Error raised when a command cannot be resolved.
|
|
1878
|
+
*/
|
|
1879
|
+
declare class CommandNotFoundError extends Error {
|
|
1880
|
+
readonly code?: CommandNotFoundErrorCode;
|
|
1881
|
+
readonly values: Record<string, unknown>;
|
|
1882
|
+
readonly commandName: string;
|
|
1883
|
+
readonly candidates: readonly string[];
|
|
1884
|
+
readonly commandPath: readonly string[];
|
|
1885
|
+
/**
|
|
1886
|
+
* Create a command-not-found error.
|
|
1887
|
+
*
|
|
1888
|
+
* @param message - Fallback error message
|
|
1889
|
+
* @param options - Command-not-found metadata
|
|
1890
|
+
*/
|
|
1891
|
+
constructor(message: string, options: CommandNotFoundErrorOptions);
|
|
1892
|
+
}
|
|
1893
|
+
/**
|
|
1894
|
+
* Check whether an error is a {@link CommandNotFoundError}.
|
|
1895
|
+
*
|
|
1896
|
+
* @param error - An unknown error
|
|
1897
|
+
* @returns `true` if the error is a {@link CommandNotFoundError}
|
|
1898
|
+
*/
|
|
1899
|
+
declare function isCommandNotFoundError(error: unknown): error is CommandNotFoundError;
|
|
1900
|
+
/**
|
|
1901
|
+
* Check whether validation errors should be handled before version, help, or command execution.
|
|
1902
|
+
*
|
|
1903
|
+
* @param error - An aggregate validation error
|
|
1904
|
+
* @returns `true` if the validation error must be handled with priority
|
|
1905
|
+
*/
|
|
1906
|
+
declare function hasPriorityValidationError(error: AggregateError | undefined): boolean;
|
|
1907
|
+
//#endregion
|
|
1757
1908
|
//#region src/index.d.ts
|
|
1758
1909
|
/**
|
|
1759
1910
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
@@ -1761,4 +1912,4 @@ declare function createCommandContext<G extends GunshiParamsConstraint = Default
|
|
|
1761
1912
|
*/
|
|
1762
1913
|
|
|
1763
1914
|
//#endregion
|
|
1764
|
-
export { ANONYMOUS_COMMAND_NAME, type ArgSchema, type ArgToken, type ArgValues, type Args, Awaitable, CLI_OPTIONS_DEFAULT, Command, CommandContext, CommandContextCore, CommandContextExtension, CommandContextParams, CommandDecorator, CommandExamplesFetcher, CommandRunner, DefaultGunshiParams, ExtendContext, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, LazyCommand, MergeGunshiExtensions, NormalizeToGunshiParams, OnPluginExtension, Plugin, PluginContext, PluginDependency, PluginExtension, PluginFunction, PluginOptions, PluginWithExtension, PluginWithoutExtension, Prettify, RendererDecorator, ValidationErrorsDecorator, createCommandContext, plugin };
|
|
1915
|
+
export { ANONYMOUS_COMMAND_NAME, type ArgSchema, type ArgToken, type ArgValues, type Args, ArgsValidationError, ArgsValidationErrorCode, ArgsValidationErrorKeys, Awaitable, CLI_OPTIONS_DEFAULT, Command, CommandContext, CommandContextCore, CommandContextExtension, CommandContextParams, CommandDecorator, CommandExamplesFetcher, CommandNotFoundError, CommandNotFoundErrorCode, CommandNotFoundErrorKeys, CommandNotFoundErrorOptions, CommandRunner, DefaultGunshiParams, ExtendContext, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, LazyCommand, MergeGunshiExtensions, NormalizeToGunshiParams, OnPluginExtension, Plugin, PluginContext, PluginDependency, PluginExtension, PluginFunction, PluginOptions, PluginWithExtension, PluginWithoutExtension, Prettify, RendererDecorator, ValidationErrorsDecorator, createCommandContext, hasPriorityValidationError, isArgsValidationError, isCommandNotFoundError, plugin };
|
package/lib/index.js
CHANGED
|
@@ -19,6 +19,7 @@ const CLI_OPTIONS_DEFAULT = {
|
|
|
19
19
|
middleMargin: 10,
|
|
20
20
|
usageOptionType: false,
|
|
21
21
|
usageOptionValue: true,
|
|
22
|
+
strict: false,
|
|
22
23
|
renderHeader: void 0,
|
|
23
24
|
renderUsage: void 0,
|
|
24
25
|
renderValidationErrors: void 0,
|
|
@@ -161,6 +162,144 @@ function getCommandName(cmd) {
|
|
|
161
162
|
else if (typeof cmd === "object") return cmd.name || "(anonymous)";
|
|
162
163
|
else return ANONYMOUS_COMMAND_NAME;
|
|
163
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* forked from `nodejs/node` (`pkgjs/parseargs`)
|
|
167
|
+
* repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
|
|
168
|
+
* code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
|
|
169
|
+
*
|
|
170
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
171
|
+
* @license MIT
|
|
172
|
+
*/
|
|
173
|
+
"-".codePointAt(0);
|
|
174
|
+
"=".codePointAt(0);
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/resolver.js
|
|
177
|
+
/**
|
|
178
|
+
* Entry point of argument options resolver.
|
|
179
|
+
*
|
|
180
|
+
* @module
|
|
181
|
+
*/
|
|
182
|
+
/**
|
|
183
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
184
|
+
* @license MIT
|
|
185
|
+
*/
|
|
186
|
+
/**
|
|
187
|
+
* Machine-readable error codes for {@link ArgsValidationError}.
|
|
188
|
+
*
|
|
189
|
+
* Each code identifies a validation failure category and is also suitable as an
|
|
190
|
+
* i18n resource key for localized rendering.
|
|
191
|
+
*/
|
|
192
|
+
const ArgsValidationErrorKeys = {
|
|
193
|
+
requiredOption: "err:arg:required-option",
|
|
194
|
+
requiredPositional: "err:arg:required-positional",
|
|
195
|
+
invalidType: "err:arg:invalid-type",
|
|
196
|
+
invalidChoice: "err:arg:invalid-choice",
|
|
197
|
+
customParse: "err:arg:custom-parse",
|
|
198
|
+
unknownOption: "err:arg:unknown-option"
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* An error that contains structured metadata for argument validation failures.
|
|
202
|
+
*
|
|
203
|
+
* The `message` remains the English fallback message. Renderers can use `code`
|
|
204
|
+
* and `values` to localize the error, falling back to `message` when localization
|
|
205
|
+
* is unavailable.
|
|
206
|
+
*/
|
|
207
|
+
var ArgsValidationError = class extends Error {
|
|
208
|
+
/**
|
|
209
|
+
* Machine-readable error code for this validation failure.
|
|
210
|
+
*
|
|
211
|
+
* This code can also be used as an i18n resource key.
|
|
212
|
+
*/
|
|
213
|
+
code;
|
|
214
|
+
/**
|
|
215
|
+
* Interpolation values for `code`.
|
|
216
|
+
*/
|
|
217
|
+
values;
|
|
218
|
+
/**
|
|
219
|
+
* Create an `ArgsValidationError` instance.
|
|
220
|
+
*
|
|
221
|
+
* @param message - fallback error message
|
|
222
|
+
* @param options - structured validation metadata
|
|
223
|
+
*/
|
|
224
|
+
constructor(message, options = {}) {
|
|
225
|
+
super(message, { cause: options.cause });
|
|
226
|
+
this.name = "ArgsValidationError";
|
|
227
|
+
this.code = options.code;
|
|
228
|
+
this.values = options.values ?? {};
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Check whether the given value is an {@link ArgsValidationError}.
|
|
233
|
+
*
|
|
234
|
+
* @param error - value to check
|
|
235
|
+
* @returns `true` when the value is an `ArgsValidationError`
|
|
236
|
+
*/
|
|
237
|
+
function isArgsValidationError(error) {
|
|
238
|
+
return error instanceof ArgsValidationError;
|
|
239
|
+
}
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region ../../node_modules/.pnpm/args-tokens@0.28.0/node_modules/args-tokens/lib/index.js
|
|
242
|
+
/**
|
|
243
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
244
|
+
* @license MIT
|
|
245
|
+
*/
|
|
246
|
+
/**
|
|
247
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
248
|
+
* @license MIT
|
|
249
|
+
*/
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region ../gunshi/src/error.ts
|
|
252
|
+
/**
|
|
253
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
254
|
+
* @license MIT
|
|
255
|
+
*/
|
|
256
|
+
/**
|
|
257
|
+
* Command not found error resource keys.
|
|
258
|
+
*/
|
|
259
|
+
const CommandNotFoundErrorKeys = { notFound: "err:cmd:not-found" };
|
|
260
|
+
/**
|
|
261
|
+
* Error raised when a command cannot be resolved.
|
|
262
|
+
*/
|
|
263
|
+
var CommandNotFoundError = class extends Error {
|
|
264
|
+
code;
|
|
265
|
+
values;
|
|
266
|
+
commandName;
|
|
267
|
+
candidates;
|
|
268
|
+
commandPath;
|
|
269
|
+
/**
|
|
270
|
+
* Create a command-not-found error.
|
|
271
|
+
*
|
|
272
|
+
* @param message - Fallback error message
|
|
273
|
+
* @param options - Command-not-found metadata
|
|
274
|
+
*/
|
|
275
|
+
constructor(message, options) {
|
|
276
|
+
super(message, { cause: options.cause });
|
|
277
|
+
this.name = "CommandNotFoundError";
|
|
278
|
+
this.code = options.code;
|
|
279
|
+
this.values = options.values || {};
|
|
280
|
+
this.commandName = options.commandName;
|
|
281
|
+
this.candidates = options.candidates || [];
|
|
282
|
+
this.commandPath = options.commandPath || [];
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
/**
|
|
286
|
+
* Check whether an error is a {@link CommandNotFoundError}.
|
|
287
|
+
*
|
|
288
|
+
* @param error - An unknown error
|
|
289
|
+
* @returns `true` if the error is a {@link CommandNotFoundError}
|
|
290
|
+
*/
|
|
291
|
+
function isCommandNotFoundError(error) {
|
|
292
|
+
return error instanceof CommandNotFoundError;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Check whether validation errors should be handled before version, help, or command execution.
|
|
296
|
+
*
|
|
297
|
+
* @param error - An aggregate validation error
|
|
298
|
+
* @returns `true` if the validation error must be handled with priority
|
|
299
|
+
*/
|
|
300
|
+
function hasPriorityValidationError(error) {
|
|
301
|
+
return error?.errors.some((error) => isCommandNotFoundError(error) || isArgsValidationError(error) && error.code === ArgsValidationErrorKeys.unknownOption) ?? false;
|
|
302
|
+
}
|
|
164
303
|
//#endregion
|
|
165
304
|
//#region ../gunshi/src/plugin/core.ts
|
|
166
305
|
/**
|
|
@@ -213,10 +352,16 @@ function plugin(options = {}) {
|
|
|
213
352
|
});
|
|
214
353
|
}
|
|
215
354
|
//#endregion
|
|
355
|
+
//#region ../gunshi/src/plugin.ts
|
|
356
|
+
/**
|
|
357
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
358
|
+
* @license MIT
|
|
359
|
+
*/
|
|
360
|
+
//#endregion
|
|
216
361
|
//#region src/index.ts
|
|
217
362
|
/**
|
|
218
363
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
219
364
|
* @license MIT
|
|
220
365
|
*/
|
|
221
366
|
//#endregion
|
|
222
|
-
export { ANONYMOUS_COMMAND_NAME, CLI_OPTIONS_DEFAULT, createCommandContext, plugin };
|
|
367
|
+
export { ANONYMOUS_COMMAND_NAME, ArgsValidationError, ArgsValidationErrorKeys, CLI_OPTIONS_DEFAULT, CommandNotFoundError, CommandNotFoundErrorKeys, createCommandContext, hasPriorityValidationError, isArgsValidationError, isCommandNotFoundError, plugin };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gunshi/plugin",
|
|
3
3
|
"description": "plugin development kit 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"
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"jsr-exports-lint": "^0.4.2",
|
|
57
57
|
"publint": "^0.3.20",
|
|
58
58
|
"tsdown": "0.21.0",
|
|
59
|
-
"gunshi": "0.35.
|
|
59
|
+
"gunshi": "0.35.3"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|