convoker 0.2.0 → 0.3.2

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/dist/command.d.ts CHANGED
@@ -1,535 +1,218 @@
1
- /**
2
- * Command action function.
3
- */
4
- export declare type ActionFn<T extends Input> = (input: InferInput<T>) => void | Promise<void>;
5
-
6
- /**
7
- * A basic input type.
8
- */
9
- declare type BasicKind = "boolean" | "string" | "number" | "bigint";
10
-
11
- /**
12
- * Builder for commands.
13
- */
14
- export declare type Builder = (c: Command<any>) => Command<any> | void;
15
-
16
- /**
17
- * A command.
18
- */
19
- export declare class Command<T extends Input = Input> {
20
- /**
21
- * The names (aliases) of this command.
22
- */
23
- $names: string[];
24
- /**
25
- * The description of this command.
26
- */
27
- $description: string | undefined;
28
- /**
29
- * The theme of this command
30
- */
31
- $theme: Theme | undefined;
32
- /**
33
- * The version of this command.
34
- */
35
- $version: string | undefined;
36
- /**
37
- * The children of this command.
38
- */
39
- $children: Map<string, CommandAlias>;
40
- /**
41
- * The parent of this command.
42
- */
43
- $parent: Command<any> | undefined;
44
- /**
45
- * If this command allows unknown options.
46
- */
47
- $allowUnknownOptions: boolean;
48
- /**
49
- * If you should be able to surpass the amount of positional arguments defined in the input.
50
- */
51
- $allowSurpassArgLimit: boolean;
52
- /**
53
- * The input this command takes.
54
- */
55
- $input: T;
56
- /**
57
- * The action function of this command.
58
- */
59
- $fn: ActionFn<T> | undefined;
60
- /**
61
- * The error handler of this command.
62
- */
63
- $errorFn: ErrorFn<T> | undefined;
64
- /**
65
- * Creates a new command.
66
- * @param names The names (aliases).
67
- * @param desc The description.
68
- * @param version The version.
69
- */
70
- constructor(names: string | string[], desc?: string, version?: string);
71
- /**
72
- * Adds a set of aliases to this command.
73
- * @param aliases The aliases to add.
74
- * @returns this
75
- */
76
- alias(...aliases: string[]): this;
77
- /**
78
- * Adds a description to this command.
79
- * @param desc The description.
80
- * @returns this
81
- */
82
- description(desc: string): this;
83
- /**
84
- * Adds a version to this command.
85
- * @param version The version.
86
- * @returns this
87
- */
88
- version(version: string): this;
89
- /**
90
- * Sets the input for this command.
91
- * @param version The input.
92
- * @returns this
93
- */
94
- input<TInput extends Input>(input: TInput): Command<TInput>;
95
- /**
96
- * Sets the action function for this command.
97
- * @param fn The action.
98
- * @returns this
99
- */
100
- action(fn: ActionFn<T>): this;
101
- /**
102
- * Sets the error function for this command.
103
- * @param fn The error handler.
104
- * @returns this
105
- */
106
- error(fn: ErrorFn<T>): this;
107
- /**
108
- * Adds an existing command to this.
109
- * @param command The command.
110
- * @returns this
111
- */
112
- add(command: Command<any>): this;
113
- /**
114
- * Creates a new subcommand and adds it.
115
- * @param names The aliases of the subcommand.
116
- * @param builder A builder to create the command.
117
- */
118
- subCommand(names: string | string[], builder: Builder): this;
119
- /**
120
- * Creates a new subcommand and adds it.
121
- * @param names The aliases of the subcommand.
122
- * @param desc The description of the subcommand.
123
- * @param version The version of the subcommand.
124
- */
125
- subCommand(names: string | string[], desc?: string, version?: string): Command<any>;
126
- /**
127
- * Allows unknown options.
128
- * @returns this
129
- */
130
- allowUnknownOptions(): this;
131
- /**
132
- * Parses a set of command-line arguments.
133
- * @param argv The arguments to parse.
134
- * @returns A parse result.
135
- */
136
- parse(argv: string[]): Promise<ParseResult<T>>;
137
- private buildInputMap;
138
- /**
139
- * Gets the full command path (name including parents).
140
- * @returns The full command path.
141
- */
142
- fullCommandPath(): string;
143
- /**
144
- * The default error screen.
145
- * @param errors The errors.
146
- */
147
- defaultErrorScreen(errors: Error[]): void;
148
- /**
149
- * Handles a set of errors.
150
- * @param errors The errors to handle.
151
- * @param input The parsed input, if possible.
152
- * @returns this
153
- */
154
- handleErrors(errors: Error[], input?: Partial<InferInput<T>>): Promise<this>;
155
- /**
156
- * Runs a command.
157
- * @param argv The arguments to run the command with. Defaults to your runtime's `argv` equivalent.
158
- * @returns this
159
- */
160
- run(argv?: string[]): Promise<this>;
161
- }
162
-
163
- /**
164
- * What the command is an alias for.
165
- */
166
- export declare interface CommandAlias<T extends Input = Input> {
167
- /**
168
- * A pointer to the command.
169
- */
170
- command: Command<T>;
171
- /**
172
- * The name of the command this is an alias for.
173
- */
174
- alias?: string;
175
- }
176
-
177
- /**
178
- * A Convoker-related error. These are usually handled by default.
179
- */
180
- declare class ConvokerError extends Error {
181
- /**
182
- * The command this error happened on.
183
- */
184
- command: Command<any>;
185
- /**
186
- * Creates a new Convoker error.
187
- * @param message The message.
188
- * @param command The command.
189
- */
190
- constructor(message: string, command: Command<any>);
191
- /**
192
- * Prints the error's message.
193
- */
194
- print(): void;
195
- }
196
-
197
- /**
198
- * Command error handler.
199
- */
200
- export declare type ErrorFn<T extends Input> = (command: Command<T>, errors: Error[], input: Partial<InferInput<T>>) => void | Promise<void>;
201
-
202
- /**
203
- * Infers a TypeScript type from an option or positional.
204
- */
205
- declare type InferEntry<T> = T extends {
206
- $kind: infer TKind extends Kind;
207
- $required: infer Required;
208
- $list: infer List;
209
- } ? List extends true ? Required extends true ? TypeOf<TKind>[] : TypeOf<TKind>[] | undefined : Required extends true ? TypeOf<TKind> : TypeOf<TKind> | undefined : never;
210
-
211
- /**
212
- * Infers TypeScript types from an input object.
213
- */
214
- declare type InferInput<T extends Input> = {
215
- [K in keyof T]: InferEntry<T[K]>;
216
- };
217
-
218
- /**
219
- * An input object.
220
- */
221
- declare interface Input {
222
- [x: string]: Option_2<any, any, any> | Positional<any, any, any>;
223
- }
224
-
225
- /**
226
- * An input type.
227
- */
228
- declare type Kind = BasicKind | StandardSchemaV1<any, any>;
229
-
230
- /**
231
- * An option.
232
- */
233
- declare class Option_2<TKind extends Kind, TRequired extends boolean = true, TList extends boolean = false> {
234
- /**
235
- * The kind of this option.
236
- */
237
- $kind: TKind;
238
- /**
239
- * The aliases of this option.
240
- */
241
- $names: string[];
242
- /**
243
- * The description of this option.
244
- */
245
- $description: string | undefined;
246
- /**
247
- * The default value of this option.
248
- */
249
- $default: TypeOf<TKind> | undefined;
250
- /**
251
- * If this option is required.
252
- */
253
- $required: TRequired;
254
- /**
255
- * If this option is a list.
256
- */
257
- $list: TList;
258
- /**
259
- * A separator if this option is a list.
260
- */
261
- $separator: string | undefined;
262
- /**
263
- * Creates a new option.
264
- * @param kind The type of this option.
265
- * @param names The names of this option.
266
- */
267
- constructor(kind: TKind, names: string[]);
268
- /**
269
- * Makes this option a list.
270
- * @returns this
271
- */
272
- list(separator?: string): Option_2<TKind, TRequired, true>;
273
- /**
274
- * Makes this option required.
275
- * @returns this
276
- */
277
- required(): Option_2<TKind, true, TList>;
278
- /**
279
- * Makes this option optional.
280
- * @returns this
281
- */
282
- optional(): Option_2<TKind, false, TList>;
283
- /**
284
- * Sets a default value.
285
- * @param value The default value.
286
- * @returns this
287
- */
288
- default(value: TypeOf<TKind>): this;
289
- /**
290
- * Sets a description.
291
- * @param desc The description.
292
- * @returns this
293
- */
294
- description(desc: string): this;
295
- }
296
-
297
- /**
298
- * The result of the `Command.parse` function.
299
- */
300
- export declare interface ParseResult<T extends Input> {
301
- /**
302
- * A pointer to the command to run.
303
- */
304
- command: Command<T>;
305
- /**
306
- * The input to pass into the command.
307
- */
308
- input: InferInput<T>;
309
- /**
310
- * Errors collected during parsing.
311
- */
312
- errors: ConvokerError[];
313
- /**
314
- * If this should result in displaying the version of the command.
315
- */
316
- isVersion: boolean;
317
- /**
318
- * If this should result in displaying a help screen.
319
- */
320
- isHelp: boolean;
321
- }
322
-
323
- /**
324
- * A positional argument.
325
- */
326
- declare class Positional<TKind extends Kind, TRequired extends boolean = true, TList extends boolean = false> {
327
- /**
328
- * The type of this argument.
329
- */
330
- $kind: TKind;
331
- /**
332
- * The default value of this argument.
333
- */
334
- $default: TypeOf<TKind> | undefined;
335
- /**
336
- * The description of this argument.
337
- */
338
- $description: string | undefined;
339
- /**
340
- * If this argument is required.
341
- */
342
- $required: TRequired;
343
- /**
344
- * If this argument is a list.
345
- */
346
- $list: TList;
347
- /**
348
- * Creates a new positional argument.
349
- * @param kind The positional argument.
350
- */
351
- constructor(kind: TKind);
352
- /**
353
- * Makes this argument a list.
354
- * @returns this
355
- */
356
- list(): Positional<TKind, TRequired, true>;
357
- /**
358
- * Makes this argument required.
359
- * @returns this
360
- */
361
- required(): Positional<TKind, true, TList>;
362
- /**
363
- * Makes this argument optional.
364
- * @returns this
365
- */
366
- optional(): Positional<TKind, false, TList>;
367
- /**
368
- * Sets a default value.
369
- * @param value The default value.
370
- * @returns this
371
- */
372
- default(value: TypeOf<TKind>): this;
373
- /**
374
- * Sets a description.
375
- * @param desc The description.
376
- * @returns this
377
- */
378
- description(desc: string): this;
379
- }
380
-
381
- /** The Standard Schema interface. */
382
- declare interface StandardSchemaV1<Input = unknown, Output = Input> {
383
- /** The Standard Schema properties. */
384
- readonly "~standard": StandardSchemaV1.Props<Input, Output>;
385
- }
386
-
387
- declare namespace StandardSchemaV1 {
388
- /** The Standard Schema properties interface. */
389
- interface Props<Input = unknown, Output = Input> {
390
- /** The version number of the standard. */
391
- readonly version: 1;
392
- /** The vendor name of the schema library. */
393
- readonly vendor: string;
394
- /** Validates unknown input values. */
395
- readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
396
- /** Inferred types associated with the schema. */
397
- readonly types?: Types<Input, Output> | undefined;
398
- }
399
- /** The result interface of the validate function. */
400
- type Result<Output> = SuccessResult<Output> | FailureResult;
401
- /** The result interface if validation succeeds. */
402
- interface SuccessResult<Output> {
403
- /** The typed output value. */
404
- readonly value: Output;
405
- /** The non-existent issues. */
406
- readonly issues?: undefined;
407
- }
408
- /** The result interface if validation fails. */
409
- interface FailureResult {
410
- /** The issues of failed validation. */
411
- readonly issues: ReadonlyArray<Issue>;
412
- }
413
- /** The issue interface of the failure output. */
414
- interface Issue {
415
- /** The error message of the issue. */
416
- readonly message: string;
417
- /** The path of the issue, if any. */
418
- readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
419
- }
420
- /** The path segment interface of the issue. */
421
- interface PathSegment {
422
- /** The key representing a path segment. */
423
- readonly key: PropertyKey;
424
- }
425
- /** The Standard Schema types interface. */
426
- interface Types<Input = unknown, Output = Input> {
427
- /** The input type of the schema. */
428
- readonly input: Input;
429
- /** The output type of the schema. */
430
- readonly output: Output;
431
- }
432
- /** Infers the input type of a Standard Schema. */
433
- type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
434
- /** Infers the output type of a Standard Schema. */
435
- type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
436
- }
437
-
438
- /**
439
- * A theme.
440
- */
441
- declare interface Theme {
442
- /**
443
- * Wraps a string in a background ANSI code.
444
- * @param a The string to wrap.
445
- */
446
- background?(a: string): string;
447
- /**
448
- * Wraps a string in a foreground ANSI code.
449
- * @param a The string to wrap.
450
- */
451
- foreground?(a: string): string;
452
- /**
453
- * Wraps a string in a primary ANSI code.
454
- * @param a The string to wrap.
455
- */
456
- primary(a: string): string;
457
- /**
458
- * Wraps a string in a secondary ANSI code.
459
- * @param a The string to wrap.
460
- */
461
- secondary(a: string): string;
462
- /**
463
- * Wraps a string in a accent ANSI code.
464
- * @param a The string to wrap.
465
- */
466
- accent?(a: string): string;
467
- /**
468
- * Wraps a string in a success ANSI code.
469
- * @param a The string to wrap.
470
- */
471
- success(a: string): string;
472
- /**
473
- * Wraps a string in a warning ANSI code.
474
- * @param a The string to wrap.
475
- */
476
- warning(a: string): string;
477
- /**
478
- * Wraps a string in a error ANSI code.
479
- * @param a The string to wrap.
480
- */
481
- error(a: string): string;
482
- /**
483
- * Wraps a string in a info ANSI code.
484
- * @param a The string to wrap.
485
- */
486
- info?(a: string): string;
487
- /**
488
- * Set of symbols for logging.
489
- */
490
- symbols?: {
491
- /**
492
- * Success message symbol.
493
- */
494
- success: string;
495
- /**
496
- * Error message symbol.
497
- */
498
- error: string;
499
- /**
500
- * Warning message symbol.
501
- */
502
- warning: string;
503
- /**
504
- * Information message symbol.
505
- */
506
- info?: string;
507
- };
508
- /**
509
- * Optional styles.
510
- */
511
- styles?: {
512
- /**
513
- * Wraps a string in a bold ANSI code.
514
- * @param a The string to wrap.
515
- */
516
- bold?(a: string): string;
517
- /**
518
- * Wraps a string in an italic ANSI code.
519
- * @param a The string to wrap.
520
- */
521
- italic?(a: string): string;
522
- /**
523
- * Wraps a string in an underline ANSI code.
524
- * @param a The string to wrap.
525
- */
526
- underline?(a: string): string;
527
- };
528
- }
529
-
530
- /**
531
- * Converts a Kind to a TypeScript type.
532
- */
533
- declare type TypeOf<T extends Kind> = T extends StandardSchemaV1<any, infer Out> ? Out : T extends "boolean" ? boolean : T extends "string" ? string : T extends "number" ? number : T extends "bigint" ? bigint : never;
534
-
535
- export { }
1
+ import { type Theme } from "./color";
2
+ import { ConvokerError } from "./error";
3
+ import { type InferInput, type Input } from "./input";
4
+ /**
5
+ * What the command is an alias for.
6
+ */
7
+ export interface CommandAlias<T extends Input = Input> {
8
+ /**
9
+ * A pointer to the command.
10
+ */
11
+ command: Command<T>;
12
+ /**
13
+ * The name of the command this is an alias for.
14
+ */
15
+ alias?: string;
16
+ }
17
+ /**
18
+ * The result of the `Command.parse` function.
19
+ */
20
+ export interface ParseResult<T extends Input> {
21
+ /**
22
+ * A pointer to the command to run.
23
+ */
24
+ command: Command<T>;
25
+ /**
26
+ * The input to pass into the command.
27
+ */
28
+ input: InferInput<T>;
29
+ /**
30
+ * Errors collected during parsing.
31
+ */
32
+ errors: ConvokerError[];
33
+ /**
34
+ * If this should result in displaying the version of the command.
35
+ */
36
+ isVersion: boolean;
37
+ /**
38
+ * If this should result in displaying a help screen.
39
+ */
40
+ isHelp: boolean;
41
+ }
42
+ /**
43
+ * Command action function.
44
+ */
45
+ export type ActionFn<T extends Input> = (input: InferInput<T>) => any | Promise<any>;
46
+ /**
47
+ * Command middleware function.
48
+ */
49
+ export type MiddlewareFn<T extends Input = Input> = (input: InferInput<T>, next: () => Promise<any>) => any | Promise<any>;
50
+ /**
51
+ * Command error handler.
52
+ */
53
+ export type ErrorFn<T extends Input> = (command: Command<T>, errors: Error[], input: Partial<InferInput<T>>) => void | Promise<void>;
54
+ /**
55
+ * Builder for commands.
56
+ */
57
+ export type Builder = (c: Command<any>) => Command<any> | void;
58
+ /**
59
+ * A command.
60
+ */
61
+ export declare class Command<T extends Input = Input> {
62
+ /**
63
+ * The names (aliases) of this command.
64
+ */
65
+ $names: string[];
66
+ /**
67
+ * The description of this command.
68
+ */
69
+ $description: string | undefined;
70
+ /**
71
+ * The theme of this command
72
+ */
73
+ $theme: Theme | undefined;
74
+ /**
75
+ * The version of this command.
76
+ */
77
+ $version: string | undefined;
78
+ /**
79
+ * The children of this command.
80
+ */
81
+ $children: Map<string, CommandAlias>;
82
+ /**
83
+ * The parent of this command.
84
+ */
85
+ $parent: Command<any> | undefined;
86
+ /**
87
+ * If this command allows unknown options.
88
+ */
89
+ $allowUnknownOptions: boolean;
90
+ /**
91
+ * If you should be able to surpass the amount of positional arguments defined in the input.
92
+ */
93
+ $allowSurpassArgLimit: boolean;
94
+ /**
95
+ * The input this command takes.
96
+ */
97
+ $input: T;
98
+ /**
99
+ * The action function of this command.
100
+ */
101
+ $fn: ActionFn<T> | undefined;
102
+ /**
103
+ * The middlewares associated with this command.
104
+ */
105
+ $middlewares: MiddlewareFn<T>[];
106
+ /**
107
+ * The error handler of this command.
108
+ */
109
+ $errorFn: ErrorFn<T> | undefined;
110
+ /**
111
+ * Creates a new command.
112
+ * @param names The names (aliases).
113
+ * @param desc The description.
114
+ * @param version The version.
115
+ */
116
+ constructor(names: string | string[], desc?: string, version?: string);
117
+ /**
118
+ * Adds a set of aliases to this command.
119
+ * @param aliases The aliases to add.
120
+ * @returns this
121
+ */
122
+ alias(...aliases: string[]): this;
123
+ /**
124
+ * Adds a description to this command.
125
+ * @param desc The description.
126
+ * @returns this
127
+ */
128
+ description(desc: string): this;
129
+ /**
130
+ * Adds a version to this command.
131
+ * @param version The version.
132
+ * @returns this
133
+ */
134
+ version(version: string): this;
135
+ /**
136
+ * Sets the input for this command.
137
+ * @param version The input.
138
+ * @returns this
139
+ */
140
+ input<TInput extends Input>(input: TInput): Command<TInput>;
141
+ /**
142
+ * Adds a chain of middlewares.
143
+ * @param fns The middlewares to use.
144
+ * @returns this
145
+ */
146
+ use(...fns: MiddlewareFn<T>[]): this;
147
+ /**
148
+ * Sets the action function for this command.
149
+ * @param fn The action.
150
+ * @returns this
151
+ */
152
+ action(fn: ActionFn<T>): this;
153
+ /**
154
+ * Sets the error function for this command.
155
+ * @param fn The error handler.
156
+ * @returns this
157
+ */
158
+ error(fn: ErrorFn<T>): this;
159
+ /**
160
+ * Adds an existing command to this.
161
+ * @param command The command.
162
+ * @returns this
163
+ */
164
+ add(command: Command<any>): this;
165
+ /**
166
+ * Creates a new subcommand and adds it.
167
+ * @param names The aliases of the subcommand.
168
+ * @param builder A builder to create the command.
169
+ */
170
+ subCommand(names: string | string[], builder: Builder): this;
171
+ /**
172
+ * Creates a new subcommand and adds it.
173
+ * @param names The aliases of the subcommand.
174
+ * @param desc The description of the subcommand.
175
+ * @param version The version of the subcommand.
176
+ */
177
+ subCommand(names: string | string[], desc?: string, version?: string): Command<any>;
178
+ /**
179
+ * Allows unknown options.
180
+ * @returns this
181
+ */
182
+ allowUnknownOptions(): this;
183
+ /**
184
+ * Parses a set of command-line arguments.
185
+ * @param argv The arguments to parse.
186
+ * @returns A parse result.
187
+ */
188
+ parse(argv: string[]): Promise<ParseResult<T>>;
189
+ private buildInputMap;
190
+ /**
191
+ * Allows surpassing the amount of arguments specified.
192
+ * @returns this
193
+ */
194
+ allowSurpassArgLimit(): this;
195
+ /**
196
+ * Gets the full command path (name including parents).
197
+ * @returns The full command path.
198
+ */
199
+ fullCommandPath(): string;
200
+ /**
201
+ * The default error screen.
202
+ * @param errors The errors.
203
+ */
204
+ defaultErrorScreen(errors: Error[]): void;
205
+ /**
206
+ * Handles a set of errors.
207
+ * @param errors The errors to handle.
208
+ * @param input The parsed input, if possible.
209
+ * @returns this
210
+ */
211
+ handleErrors(errors: Error[], input?: Partial<InferInput<T>>): Promise<this>;
212
+ /**
213
+ * Runs a command.
214
+ * @param argv The arguments to run the command with. Defaults to your runtime's `argv` equivalent.
215
+ * @returns this
216
+ */
217
+ run(argv?: string[]): Promise<this>;
218
+ }