@gunshi/plugin 0.26.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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 kazuya kawaguchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # `@gunshi/plugin`
2
+
3
+ > utilities for gunshi plugin
4
+
5
+ This package provides APIs and type definitions for making plugins for gunshi.
6
+
7
+ <!-- eslint-disable markdown/no-missing-label-refs -->
8
+
9
+ > [!TIP]
10
+ > **The APIs and type definitions available in this package are the same as those in the `gunshi/plugin` entry in the `gunshi` package.**
11
+ > This package is smaller in file size than the `gunshi` package, making it suitable for use when you want to reduce the size of the 'node_modules' in the plugin package you are creating.
12
+
13
+ <!-- eslint-enable markdown/no-missing-label-refs -->
14
+
15
+ ## 💿 Installation
16
+
17
+ ```sh
18
+ # npm
19
+ npm install --save @gunshi/plugin
20
+
21
+ ## pnpm
22
+ pnpm add @gunshi/plugin
23
+
24
+ ## yarn
25
+ yarn add @gunshi/plugin
26
+
27
+ ## deno
28
+ deno add jsr:@gunshi/plugin
29
+
30
+ ## bun
31
+ bun add @gunshi/plugin
32
+ ```
33
+
34
+ ## 🚀 Usage
35
+
36
+ ```js
37
+ // TODO(kazupon): prepare the example
38
+ ```
39
+
40
+ ## ©️ License
41
+
42
+ [MIT](http://opensource.org/licenses/MIT)
package/lib/index.d.ts ADDED
@@ -0,0 +1,667 @@
1
+ //#region ../../node_modules/.pnpm/args-tokens@0.20.1/node_modules/args-tokens/lib/parser-FiQIAw-2.d.ts
2
+ //#region src/parser.d.ts
3
+ /**
4
+ * Entry point of argument parser.
5
+ * @module
6
+ */
7
+ /**
8
+ * forked from `nodejs/node` (`pkgjs/parseargs`)
9
+ * repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
10
+ * code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
11
+ *
12
+ * @author kazuya kawaguchi (a.k.a. kazupon)
13
+ * @license MIT
14
+ */
15
+ /**
16
+ * Argument token Kind.
17
+ * - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
18
+ * - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
19
+ * - `positional`: positional token
20
+ */
21
+ type ArgTokenKind = "option" | "option-terminator" | "positional";
22
+ /**
23
+ * Argument token.
24
+ */
25
+ interface ArgToken {
26
+ /**
27
+ * Argument token kind.
28
+ */
29
+ kind: ArgTokenKind;
30
+ /**
31
+ * Argument token index, e.g `--foo bar` => `--foo` index is 0, `bar` index is 1.
32
+ */
33
+ index: number;
34
+ /**
35
+ * Option name, e.g. `--foo` => `foo`, `-x` => `x`.
36
+ */
37
+ name?: string;
38
+ /**
39
+ * Raw option name, e.g. `--foo` => `--foo`, `-x` => `-x`.
40
+ */
41
+ rawName?: string;
42
+ /**
43
+ * Option value, e.g. `--foo=bar` => `bar`, `-x=bar` => `bar`.
44
+ * If the `allowCompatible` option is `true`, short option value will be same as Node.js `parseArgs` behavior.
45
+ */
46
+ value?: string;
47
+ /**
48
+ * Inline value, e.g. `--foo=bar` => `true`, `-x=bar` => `true`.
49
+ */
50
+ inlineValue?: boolean;
51
+ } //#endregion
52
+ //#region ../../node_modules/.pnpm/args-tokens@0.20.1/node_modules/args-tokens/lib/resolver-U72Jg6Ll.d.ts
53
+
54
+ /**
55
+ * Parser Options.
56
+ */
57
+ //#region src/resolver.d.ts
58
+ /**
59
+ * An argument schema
60
+ * This schema is similar to the schema of the `node:utils`.
61
+ * difference is that:
62
+ * - `required` property and `description` property are added
63
+ * - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
64
+ * - `default` property type, not support multiple types
65
+ */
66
+ interface ArgSchema {
67
+ /**
68
+ * Type of argument.
69
+ */
70
+ type: "string" | "boolean" | "number" | "enum" | "positional" | "custom";
71
+ /**
72
+ * A single character alias for the argument.
73
+ */
74
+ short?: string;
75
+ /**
76
+ * A description of the argument.
77
+ */
78
+ description?: string;
79
+ /**
80
+ * Whether the argument is required or not.
81
+ */
82
+ required?: true;
83
+ /**
84
+ * Whether the argument allow multiple values or not.
85
+ */
86
+ multiple?: true;
87
+ /**
88
+ * Whether the negatable option for `boolean` type
89
+ */
90
+ negatable?: boolean;
91
+ /**
92
+ * The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
93
+ */
94
+ choices?: string[] | readonly string[];
95
+ /**
96
+ * The default value of the argument.
97
+ * if the type is 'enum', the default value must be one of the allowed values.
98
+ */
99
+ default?: string | boolean | number;
100
+ /**
101
+ * Whether to convert the argument name to kebab-case.
102
+ */
103
+ toKebab?: true;
104
+ /**
105
+ * A function to parse the value of the argument. if the type is 'custom', this function is required.
106
+ * If argument value will be invalid, this function have to throw an error.
107
+ * @param value
108
+ * @returns Parsed value
109
+ * @throws An Error, If the value is invalid. Error type should be `Error` or extends it
110
+ */
111
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
112
+ parse?: (value: string) => any;
113
+ }
114
+ /**
115
+ * An object that contains {@link ArgSchema | argument schema}.
116
+ */
117
+ interface Args {
118
+ [option: string]: ArgSchema;
119
+ }
120
+ /**
121
+ * An object that contains the values of the arguments.
122
+ */
123
+ type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
124
+ [option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
125
+ };
126
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
+ type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
128
+ /**
129
+ * @internal
130
+ */
131
+ 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>;
132
+ type ResolveOptionValue<A extends ArgSchema, T> = A["multiple"] extends true ? T[] : T;
133
+ /**
134
+ * @internal
135
+ */
136
+ 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;
137
+ /**
138
+ * @internal
139
+ */
140
+ 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] };
141
+ /**
142
+ * @internal
143
+ */
144
+ 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] };
145
+
146
+ //#endregion
147
+ //#region ../gunshi/src/types.d.ts
148
+ /**
149
+ * An arguments for {@link resolveArgs | resolve arguments}.
150
+ */
151
+ type Awaitable<T> = T | Promise<T>;
152
+ /**
153
+ * Extend command context type. This type is used to extend the command context with additional properties at {@link CommandContext.extensions}.
154
+ */
155
+ type ExtendContext = Record<string, unknown>;
156
+ /**
157
+ * Gunshi unified parameter type.
158
+ * This type combines both argument definitions and command context extensions.
159
+ */
160
+ interface GunshiParams<P extends {
161
+ args?: Args;
162
+ extensions?: ExtendContext;
163
+ } = {
164
+ args: Args;
165
+ extensions: {};
166
+ }> {
167
+ /**
168
+ * Command argument definitions
169
+ */
170
+ args: P extends {
171
+ args: infer A extends Args;
172
+ } ? A : Args;
173
+ /**
174
+ * Command context extensions
175
+ */
176
+ extensions: P extends {
177
+ extensions: infer E extends ExtendContext;
178
+ } ? E : {};
179
+ }
180
+ /**
181
+ * Default Gunshi parameters
182
+ */
183
+ type DefaultGunshiParams = GunshiParams;
184
+ /**
185
+ * Generic constraint for command-related types.
186
+ * This type constraint allows both GunshiParams and objects with extensions.
187
+ */
188
+ type GunshiParamsConstraint = GunshiParams<any> | {
189
+ extensions: ExtendContext;
190
+ };
191
+ /**
192
+ * Type helper to extract args from G
193
+ * @internal
194
+ */
195
+ type ExtractArgs<G> = G extends GunshiParams<any> ? G['args'] : Args;
196
+ /**
197
+ * Type helper to extract extensions from G
198
+ * @internal
199
+ */
200
+ type ExtractExtensions<G> = G extends GunshiParams<any> ? G['extensions'] : G extends {
201
+ extensions: infer E;
202
+ } ? E : {};
203
+ /**
204
+ * Type helper to normalize G to GunshiParams
205
+ * @internal
206
+ */
207
+ type NormalizeToGunshiParams<G> = G extends GunshiParams<any> ? G : G extends {
208
+ extensions: ExtendContext;
209
+ } ? GunshiParams<{
210
+ args: Args;
211
+ extensions: G['extensions'];
212
+ }> : DefaultGunshiParams;
213
+ /**
214
+ * Command environment.
215
+ */
216
+ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiParams> {
217
+ /**
218
+ * Current working directory.
219
+ * @see {@link CliOptions.cwd}
220
+ */
221
+ cwd: string | undefined;
222
+ /**
223
+ * Command name.
224
+ * @see {@link CliOptions.name}
225
+ */
226
+ name: string | undefined;
227
+ /**
228
+ * Command description.
229
+ * @see {@link CliOptions.description}
230
+ *
231
+ */
232
+ description: string | undefined;
233
+ /**
234
+ * Command version.
235
+ * @see {@link CliOptions.version}
236
+ */
237
+ version: string | undefined;
238
+ /**
239
+ * Left margin of the command output.
240
+ * @default 2
241
+ * @see {@link CliOptions.leftMargin}
242
+ */
243
+ leftMargin: number;
244
+ /**
245
+ * Middle margin of the command output.
246
+ * @default 10
247
+ * @see {@link CliOptions.middleMargin}
248
+ */
249
+ middleMargin: number;
250
+ /**
251
+ * Whether to display the usage option type.
252
+ * @default false
253
+ * @see {@link CliOptions.usageOptionType}
254
+ */
255
+ usageOptionType: boolean;
256
+ /**
257
+ * Whether to display the option value.
258
+ * @default true
259
+ * @see {@link CliOptions.usageOptionValue}
260
+ */
261
+ usageOptionValue: boolean;
262
+ /**
263
+ * Whether to display the command usage.
264
+ * @default false
265
+ * @see {@link CliOptions.usageSilent}
266
+ */
267
+ usageSilent: boolean;
268
+ /**
269
+ * Sub commands.
270
+ * @see {@link CliOptions.subCommands}
271
+ */
272
+ subCommands: Map<string, Command<any> | LazyCommand<any>> | undefined;
273
+ /**
274
+ * Render function the command usage.
275
+ */
276
+ renderUsage: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null | undefined;
277
+ /**
278
+ * Render function the header section in the command usage.
279
+ */
280
+ renderHeader: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null | undefined;
281
+ /**
282
+ * Render function the validation errors.
283
+ */
284
+ renderValidationErrors: ((ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>) | null | undefined;
285
+ /**
286
+ * Hook that runs before any command execution
287
+ * @see {@link CliOptions.onBeforeCommand}
288
+ */
289
+ onBeforeCommand: ((ctx: Readonly<CommandContext<G>>) => Awaitable<void>) | undefined;
290
+ /**
291
+ * Hook that runs after successful command execution
292
+ * @see {@link CliOptions.onAfterCommand}
293
+ */
294
+ onAfterCommand: ((ctx: Readonly<CommandContext<G>>, result: string | void) => Awaitable<void>) | undefined;
295
+ /**
296
+ * Hook that runs when a command throws an error
297
+ * @see {@link CliOptions.onErrorCommand}
298
+ */
299
+ onErrorCommand: ((ctx: Readonly<CommandContext<G>>, error: Error) => Awaitable<void>) | undefined;
300
+ }
301
+ /**
302
+ * CLI options of `cli` function.
303
+ */
304
+
305
+ /**
306
+ * Command call mode.
307
+ */
308
+ type CommandCallMode = 'entry' | 'subCommand' | 'unexpected';
309
+ /**
310
+ * Command context.
311
+ * Command context is the context of the command execution.
312
+ */
313
+ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams> {
314
+ /**
315
+ * Command name, that is the command that is executed.
316
+ * The command name is same {@link CommandEnvironment.name}.
317
+ */
318
+ name: string | undefined;
319
+ /**
320
+ * Command description, that is the description of the command that is executed.
321
+ * The command description is same {@link CommandEnvironment.description}.
322
+ */
323
+ description: string | undefined;
324
+ /**
325
+ * Command environment, that is the environment of the command that is executed.
326
+ * The command environment is same {@link CommandEnvironment}.
327
+ */
328
+ env: Readonly<CommandEnvironment<G>>;
329
+ /**
330
+ * Command arguments, that is the arguments of the command that is executed.
331
+ * The command arguments is same {@link Command.args}.
332
+ */
333
+ args: ExtractArgs<G>;
334
+ /**
335
+ * Command values, that is the values of the command that is executed.
336
+ * Resolve values with `resolveArgs` from command arguments and {@link Command.args}.
337
+ */
338
+ values: ArgValues<ExtractArgs<G>>;
339
+ /**
340
+ * Command positionals arguments, that is the positionals of the command that is executed.
341
+ * Resolve positionals with `resolveArgs` from command arguments.
342
+ */
343
+ positionals: string[];
344
+ /**
345
+ * Command rest arguments, that is the remaining argument not resolved by the optional command option delimiter `--`.
346
+ */
347
+ rest: string[];
348
+ /**
349
+ * Original command line arguments.
350
+ * This argument is passed from `cli` function.
351
+ */
352
+ _: string[];
353
+ /**
354
+ * Argument tokens, that is parsed by `parseArgs` function.
355
+ */
356
+ tokens: ArgToken[];
357
+ /**
358
+ * Whether the currently executing command has been executed with the sub-command name omitted.
359
+ */
360
+ omitted: boolean;
361
+ /**
362
+ * Command call mode.
363
+ * The command call mode is `entry` when the command is executed as an entry command, and `subCommand` when the command is executed as a sub-command.
364
+ */
365
+ callMode: CommandCallMode;
366
+ /**
367
+ * Whether to convert the camel-case style argument name to kebab-case.
368
+ * This context value is set from {@link Command.toKebab} option.
369
+ */
370
+ toKebab?: boolean;
371
+ /**
372
+ * Output a message.
373
+ * If {@link CommandEnvironment.usageSilent} is true, the message is not output.
374
+ * @param message an output message, @see {@link console.log}
375
+ * @param optionalParams an optional parameters, @see {@link console.log}
376
+ * @internal
377
+ */
378
+ log: (message?: any, ...optionalParams: any[]) => void;
379
+ /**
380
+ * Command context extensions.
381
+ */
382
+ extensions: keyof ExtractExtensions<G> extends never ? undefined : ExtractExtensions<G>;
383
+ /**
384
+ * Validation error from argument parsing.
385
+ * This will be set if argument validation fails during CLI execution.
386
+ */
387
+ validationError?: AggregateError;
388
+ }
389
+ /**
390
+ * CommandContextCore type (base type without extensions)
391
+ */
392
+ type CommandContextCore<G extends GunshiParamsConstraint = DefaultGunshiParams> = Readonly<CommandContext<G>>;
393
+ /**
394
+ * Command context extension
395
+ */
396
+ interface CommandContextExtension<E extends GunshiParams['extensions'] = DefaultGunshiParams['extensions']> {
397
+ readonly key: symbol;
398
+ readonly factory: (ctx: CommandContextCore, cmd: Command) => Awaitable<E>;
399
+ readonly onFactory?: (ctx: Readonly<CommandContext>, cmd: Readonly<Command>) => Awaitable<void>;
400
+ }
401
+ /**
402
+ * Command interface.
403
+ */
404
+ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
405
+ /**
406
+ * Command name.
407
+ * It's used to find command line arguments to execute from sub commands, and it's recommended to specify.
408
+ */
409
+ name?: string;
410
+ /**
411
+ * Command description.
412
+ * It's used to describe the command in usage and it's recommended to specify.
413
+ */
414
+ description?: string;
415
+ /**
416
+ * Command arguments.
417
+ * Each argument can include a description property to describe the argument in usage.
418
+ */
419
+ args?: ExtractArgs<G>;
420
+ /**
421
+ * Command examples.
422
+ * examples of how to use the command.
423
+ */
424
+ examples?: string | CommandExamplesFetcher<G>;
425
+ /**
426
+ * Command runner. it's the command to be executed
427
+ */
428
+ run?: CommandRunner<G>;
429
+ /**
430
+ * Whether to convert the camel-case style argument name to kebab-case.
431
+ * If you will set to `true`, All {@link Command.args} names will be converted to kebab-case.
432
+ */
433
+ toKebab?: boolean;
434
+ }
435
+ /**
436
+ * Lazy command interface.
437
+ * Lazy command that's not loaded until it is executed.
438
+ */
439
+ type LazyCommand<G extends GunshiParamsConstraint = DefaultGunshiParams> = {
440
+ /**
441
+ * Command load function
442
+ */
443
+ (): Awaitable<Command<G> | CommandRunner<G>>;
444
+ /**
445
+ * Command name
446
+ */
447
+ commandName?: string;
448
+ } & Omit<Command<G>, 'run' | 'name'>;
449
+ /**
450
+ * Define a command type.
451
+ */
452
+
453
+ /**
454
+ * Command examples fetcher.
455
+ * @param ctx A {@link CommandContext | command context}
456
+ * @returns A fetched command examples.
457
+ */
458
+ type CommandExamplesFetcher<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<string>;
459
+ /**
460
+ * Command runner.
461
+ * @param ctx A {@link CommandContext | command context}
462
+ * @returns void or string (for CLI output)
463
+ */
464
+ type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<void | string>;
465
+ /**
466
+ * Command loader.
467
+ * A function that returns a command or command runner.
468
+ * This is used to lazily load commands.
469
+ * @returns A command or command runner
470
+ */
471
+
472
+ /**
473
+ * Command decorator.
474
+ * A function that wraps a command runner to add or modify its behavior.
475
+ * @param baseRunner The base command runner to decorate
476
+ * @returns The decorated command runner
477
+ */
478
+ type CommandDecorator<G extends GunshiParamsConstraint = DefaultGunshiParams> = (baseRunner: (ctx: Readonly<CommandContext<G>>) => Awaitable<void | string>) => (ctx: Readonly<CommandContext<G>>) => Awaitable<void | string>;
479
+ /**
480
+ * Renderer decorator type.
481
+ * A function that wraps a base renderer to add or modify its behavior.
482
+ * @param baseRenderer The base renderer function to decorate
483
+ * @param ctx The command context
484
+ * @returns The decorated result
485
+ */
486
+ type RendererDecorator<T, G extends GunshiParamsConstraint = DefaultGunshiParams> = (baseRenderer: (ctx: Readonly<CommandContext<G>>) => Promise<T>, ctx: Readonly<CommandContext<G>>) => Promise<T>;
487
+ /**
488
+ * Validation errors renderer decorator type.
489
+ * A function that wraps a validation errors renderer to add or modify its behavior.
490
+ * @param baseRenderer The base validation errors renderer function to decorate
491
+ * @param ctx The command context
492
+ * @param error The aggregate error containing validation errors
493
+ * @returns The decorated result
494
+ */
495
+ type ValidationErrorsDecorator<G extends GunshiParamsConstraint = DefaultGunshiParams> = (baseRenderer: (ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>, ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>; //#endregion
496
+ //#region ../gunshi/src/plugin/context.d.ts
497
+ /**
498
+ * Type helper to create GunshiParams from extracted args and extensions
499
+ * @internal
500
+ */
501
+ type ExtractedParams<G extends GunshiParamsConstraint, L extends Record<string, unknown> = {}> = {
502
+ args: ExtractArgs<G>;
503
+ extensions: ExtractExtensions<G> & L;
504
+ };
505
+ /**
506
+ * Gunshi plugin context interface.
507
+ */
508
+ interface PluginContext<G extends GunshiParamsConstraint = DefaultGunshiParams> {
509
+ /**
510
+ * Get the global options
511
+ * @returns A map of global options.
512
+ */
513
+ readonly globalOptions: Map<string, ArgSchema>;
514
+ /**
515
+ * Add a global option.
516
+ * @param name An option name
517
+ * @param schema An {@link ArgSchema} for the option
518
+ */
519
+ addGlobalOption(name: string, schema: ArgSchema): void;
520
+ /**
521
+ * Decorate the header renderer.
522
+ * @param decorator - A decorator function that wraps the base header renderer.
523
+ */
524
+ decorateHeaderRenderer<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRenderer: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>, ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>): void;
525
+ /**
526
+ * Decorate the usage renderer.
527
+ * @param decorator - A decorator function that wraps the base usage renderer.
528
+ */
529
+ decorateUsageRenderer<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRenderer: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>, ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Promise<string>): void;
530
+ /**
531
+ * Decorate the validation errors renderer.
532
+ * @param decorator - A decorator function that wraps the base validation errors renderer.
533
+ */
534
+ decorateValidationErrorsRenderer<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRenderer: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>, error: AggregateError) => Promise<string>, ctx: Readonly<CommandContext<ExtractedParams<G, L>>>, error: AggregateError) => Promise<string>): void;
535
+ /**
536
+ * Decorate the command execution.
537
+ * Decorators are applied in reverse order (last registered is executed first).
538
+ * @param decorator - A decorator function that wraps the command runner
539
+ */
540
+ decorateCommand<L extends Record<string, unknown> = DefaultGunshiParams['extensions']>(decorator: (baseRunner: (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Awaitable<void | string>) => (ctx: Readonly<CommandContext<ExtractedParams<G, L>>>) => Awaitable<void | string>): void;
541
+ }
542
+
543
+ //#endregion
544
+ //#region ../gunshi/src/plugin/core.d.ts
545
+ /**
546
+ * Factory function for creating a plugin context.
547
+ * @param decorators - A {@link Decorators} instance.
548
+ * @returns A new {@link PluginContext} instance.
549
+ */
550
+ /**
551
+ * Plugin dependency definition
552
+ */
553
+ interface PluginDependency {
554
+ /**
555
+ * Dependency plugin id
556
+ */
557
+ id: string;
558
+ /**
559
+ * Optional dependency flag.
560
+ * If true, the plugin will not throw an error if the dependency is not found.
561
+ */
562
+ optional?: boolean;
563
+ }
564
+ /**
565
+ * Plugin function type
566
+ */
567
+ type PluginFunction<G extends GunshiParams = DefaultGunshiParams> = (ctx: Readonly<PluginContext<G>>) => Awaitable<void>;
568
+ /**
569
+ * Plugin extension for CommandContext
570
+ */
571
+ type PluginExtension<T = Record<string, unknown>, G extends GunshiParams = DefaultGunshiParams> = (ctx: CommandContextCore<G>, cmd: Command<G>) => T;
572
+ /**
573
+ * Plugin extension callback type
574
+ */
575
+ type OnPluginExtension<G extends GunshiParams = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>, cmd: Readonly<Command<G>>) => void;
576
+ /**
577
+ * Plugin definition options
578
+ */
579
+ interface PluginOptions<T extends Record<string, unknown> = Record<never, never>, G extends GunshiParams = DefaultGunshiParams> {
580
+ /**
581
+ * Plugin unique identifier
582
+ */
583
+ id: string;
584
+ /**
585
+ * Plugin name
586
+ */
587
+ name?: string;
588
+ /**
589
+ * Plugin dependencies
590
+ */
591
+ dependencies?: (PluginDependency | string)[];
592
+ /**
593
+ * Plugin setup function
594
+ */
595
+ setup?: PluginFunction<G>;
596
+ /**
597
+ * Plugin extension
598
+ */
599
+ extension?: PluginExtension<T, G>;
600
+ /**
601
+ * Callback for when the plugin is extended with `extension` option.
602
+ */
603
+ onExtension?: OnPluginExtension<G>;
604
+ }
605
+ /**
606
+ * Gunshi plugin, which is a function that receives a PluginContext.
607
+ * @param ctx - A {@link PluginContext}.
608
+ * @returns An {@link Awaitable} that resolves when the plugin is loaded.
609
+ */
610
+ type Plugin<E extends GunshiParams['extensions'] = DefaultGunshiParams['extensions']> = PluginFunction & {
611
+ id: string;
612
+ name?: string;
613
+ dependencies?: (PluginDependency | string)[];
614
+ extension?: CommandContextExtension<E>;
615
+ };
616
+ /**
617
+ * Plugin return type with extension
618
+ * @internal
619
+ */
620
+ interface PluginWithExtension<E extends GunshiParams['extensions'] = DefaultGunshiParams['extensions']> extends Plugin<E> {
621
+ id: string;
622
+ name: string;
623
+ dependencies?: (PluginDependency | string)[];
624
+ extension: CommandContextExtension<E>;
625
+ }
626
+ /**
627
+ * Plugin return type without extension
628
+ * @internal
629
+ */
630
+ interface PluginWithoutExtension<E extends GunshiParams['extensions'] = DefaultGunshiParams['extensions']> extends Plugin<E> {
631
+ id: string;
632
+ name: string;
633
+ dependencies?: (PluginDependency | string)[];
634
+ }
635
+ /**
636
+ * Define a plugin with extension capabilities
637
+ * @param options - {@link PluginOptions | plugin options}
638
+ * @return A defined plugin with extension capabilities.
639
+ */
640
+ declare function plugin<I extends string, P extends PluginExtension<any, DefaultGunshiParams>>(options: {
641
+ id: I;
642
+ name?: string;
643
+ dependencies?: (PluginDependency | string)[];
644
+ setup?: (ctx: Readonly<PluginContext<GunshiParams<{
645
+ args: Args;
646
+ extensions: { [K in I]: ReturnType<P> };
647
+ }>>>) => Awaitable<void>;
648
+ extension: P;
649
+ onExtension?: OnPluginExtension<{
650
+ args: Args;
651
+ extensions: { [K in I]: ReturnType<P> };
652
+ }>;
653
+ }): PluginWithExtension<ReturnType<P>>;
654
+ /**
655
+ * Define a plugin without extension capabilities
656
+ * @param options - {@link PluginOptions | plugin options} without extension
657
+ * @returns A defined plugin without extension capabilities.
658
+ */
659
+ declare function plugin(options: {
660
+ id: string;
661
+ name?: string;
662
+ dependencies?: (PluginDependency | string)[];
663
+ setup?: (ctx: Readonly<PluginContext<DefaultGunshiParams>>) => Awaitable<void>;
664
+ }): PluginWithoutExtension<DefaultGunshiParams['extensions']>;
665
+
666
+ //#endregion
667
+ export { ArgSchema, ArgToken, ArgValues, Args, Awaitable, Command, CommandContext, CommandContextCore, CommandDecorator, CommandExamplesFetcher, CommandRunner, DefaultGunshiParams, ExtendContext, ExtractArgs, GunshiParams, GunshiParamsConstraint, LazyCommand, NormalizeToGunshiParams, OnPluginExtension, Plugin, PluginContext, PluginDependency, PluginExtension, PluginFunction, PluginOptions, PluginWithExtension, PluginWithoutExtension, RendererDecorator, ValidationErrorsDecorator, plugin };
package/lib/index.js ADDED
@@ -0,0 +1,45 @@
1
+ //#region ../gunshi/src/plugin/core.ts
2
+ /**
3
+ * Define a plugin
4
+ * @param options - {@link PluginOptions | plugin options}
5
+ * @returns A defined plugin.
6
+ */
7
+ function plugin(options) {
8
+ const { id, name, setup, extension, onExtension, dependencies } = options;
9
+ const pluginFn = async (ctx) => {
10
+ if (setup) await setup(ctx);
11
+ };
12
+ return Object.defineProperties(pluginFn, {
13
+ id: {
14
+ value: id,
15
+ writable: false,
16
+ enumerable: true,
17
+ configurable: true
18
+ },
19
+ ...name && { name: {
20
+ value: name,
21
+ writable: false,
22
+ enumerable: true,
23
+ configurable: true
24
+ } },
25
+ ...dependencies && { dependencies: {
26
+ value: dependencies,
27
+ writable: false,
28
+ enumerable: true,
29
+ configurable: true
30
+ } },
31
+ ...extension && { extension: {
32
+ value: {
33
+ key: Symbol(id),
34
+ factory: extension,
35
+ onFactory: onExtension
36
+ },
37
+ writable: false,
38
+ enumerable: true,
39
+ configurable: true
40
+ } }
41
+ });
42
+ }
43
+
44
+ //#endregion
45
+ export { plugin };
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@gunshi/plugin",
3
+ "description": "utilities for gunshi plugin",
4
+ "version": "0.26.3",
5
+ "author": {
6
+ "name": "kazuya kawaguchi",
7
+ "email": "kawakazu80@gmail.com"
8
+ },
9
+ "license": "MIT",
10
+ "funding": "https://github.com/sponsors/kazupon",
11
+ "bugs": {
12
+ "url": "https://github.com/kazupon/gunshi/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/kazupon/gunshi.git",
17
+ "directory": "packages/plugin"
18
+ },
19
+ "keywords": [
20
+ "gunshi",
21
+ "plugin",
22
+ "cli"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "engines": {
28
+ "node": ">= 20"
29
+ },
30
+ "type": "module",
31
+ "files": [
32
+ "lib"
33
+ ],
34
+ "module": "lib/index.js",
35
+ "exports": {
36
+ ".": {
37
+ "types": "./lib/index.d.ts",
38
+ "import": "./lib/index.js",
39
+ "require": "./lib/index.js",
40
+ "default": "./lib/index.js"
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "types": "lib/index.d.ts",
45
+ "typesVersions": {
46
+ "*": {
47
+ "*": [
48
+ "./lib/*",
49
+ "./*"
50
+ ]
51
+ }
52
+ },
53
+ "devDependencies": {
54
+ "deno": "^2.3.3",
55
+ "jsr": "^0.13.4",
56
+ "jsr-exports-lint": "^0.4.1",
57
+ "publint": "^0.3.12",
58
+ "tsdown": "^0.12.3",
59
+ "gunshi": "0.26.3"
60
+ },
61
+ "scripts": {
62
+ "build": "tsdown",
63
+ "lint:jsr": "jsr publish --dry-run --allow-dirty",
64
+ "typecheck:deno": "deno check --import-map=../../importmap.json ./src"
65
+ }
66
+ }