gunshi 0.2.1 → 0.2.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.
@@ -95,7 +95,8 @@ async function createCommandContext({ options, values, positionals, command, com
95
95
  localeResources.set(locale.toString(), builtInLoadedResources);
96
96
  } catch {}
97
97
  /**
98
- * define the translation function
98
+ * define the translation function, which is used to {@link CommandContext.translation}.
99
+ *
99
100
  */
100
101
  function translation(key) {
101
102
  if (COMMAND_I18N_RESOURCE_KEYS.includes(key)) {
package/lib/context.d.ts CHANGED
@@ -1,17 +1,50 @@
1
1
  import { ArgOptions, ArgValues } from 'args-tokens';
2
- import { C as Command, a as CommandOptions, b as CommandContext } from './types.d-B3YGxDV6.js';
2
+ import { C as Command, a as CommandOptions, b as CommandContext } from './types.d-00BVt8hZ.js';
3
3
 
4
+ /**
5
+ * The default locale string, which format is BCP 47 language tag
6
+ */
4
7
  declare const DEFAULT_LOCALE = "en-US";
8
+ /**
9
+ * Parameters of {@link createCommandContext}
10
+ */
11
+ interface CommandContextParams<
12
+ Options extends ArgOptions,
13
+ Values
14
+ > {
15
+ /**
16
+ * An options of target command
17
+ */
18
+ options: Options | undefined;
19
+ /**
20
+ * A values of target command
21
+ */
22
+ values: Values;
23
+ /**
24
+ * A positionals arguments, which passed to the target command
25
+ */
26
+ positionals: string[];
27
+ /**
28
+ * Whether the command is omitted
29
+ */
30
+ omitted: boolean;
31
+ /**
32
+ * A target {@link Command | command}
33
+ */
34
+ command: Command<Options>;
35
+ /**
36
+ * A command options, which is spicialized from `cli` function
37
+ */
38
+ commandOptions: CommandOptions<Options>;
39
+ }
40
+ /**
41
+ * Create a {@link CommandContext | command context}
42
+ * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
43
+ * @returns A {@link CommandContext | command context}, which is readonly
44
+ */
5
45
  declare function createCommandContext<
6
46
  Options extends ArgOptions,
7
47
  Values = ArgValues<Options>
8
- >({ options, values, positionals, command, commandOptions, omitted }: {
9
- options: Options | undefined
10
- values: Values
11
- positionals: string[]
12
- omitted: boolean
13
- command: Command<Options>
14
- commandOptions: CommandOptions<Options>
15
- }): Promise<Readonly<CommandContext<Options, Values>>>;
48
+ >({ options, values, positionals, command, commandOptions, omitted }: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>;
16
49
 
17
50
  export { DEFAULT_LOCALE, createCommandContext };
package/lib/context.js CHANGED
@@ -1,4 +1,4 @@
1
- import { DEFAULT_LOCALE, createCommandContext } from "./context-BQKZW5bg.js";
1
+ import { DEFAULT_LOCALE, createCommandContext } from "./context-DmZAeiph.js";
2
2
  import "./utils-NHs5DuHk.js";
3
3
 
4
4
  export { DEFAULT_LOCALE, createCommandContext };
package/lib/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { ArgOptions } from 'args-tokens';
2
2
  export { ArgOptionSchema, ArgOptions, ArgValues } from 'args-tokens';
3
- import { C as Command, c as CommandRunner, a as CommandOptions } from './types.d-B3YGxDV6.js';
4
- export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, b as CommandContext, g as CommandEnvironment, i as CommandResource, j as CommandResourceFetcher, h as CommandUsageRender, L as LazyCommand } from './types.d-B3YGxDV6.js';
3
+ import { C as Command, c as CommandRunner, a as CommandOptions } from './types.d-00BVt8hZ.js';
4
+ export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, b as CommandContext, g as CommandEnvironment, h as CommandResource, i as CommandResourceFetcher, L as LazyCommand } from './types.d-00BVt8hZ.js';
5
5
 
6
6
  /**
7
7
  * Run the command
8
8
  * @param args - command line arguments
9
- * @param entry - a {@link Command | entry command} or an {@link CommandRunner | inline command runner}
10
- * @param opts - a {@link CommandOptions | command options}
9
+ * @param entry - A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
10
+ * @param opts - A {@link CommandOptions | command options}
11
11
  */
12
12
  declare function cli<Options extends ArgOptions>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<void>;
13
13
 
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, createCommandContext } from "./context-BQKZW5bg.js";
1
+ import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, createCommandContext } from "./context-DmZAeiph.js";
2
2
  import { create, log, resolveLazyCommand } from "./utils-NHs5DuHk.js";
3
3
  import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-Bo0DibAK.js";
4
4
  import { parseArgs, resolveArgs } from "args-tokens";
@@ -33,7 +33,7 @@ async function cli(args, entry, opts = {}) {
33
33
  }
34
34
  if (error) {
35
35
  await showValidationErrors(ctx, error);
36
- throw error;
36
+ return;
37
37
  }
38
38
  await command.run(ctx);
39
39
  }
package/lib/renderer.d.ts CHANGED
@@ -1,8 +1,24 @@
1
1
  import { ArgOptions } from 'args-tokens';
2
- import { b as CommandContext } from './types.d-B3YGxDV6.js';
2
+ import { b as CommandContext } from './types.d-00BVt8hZ.js';
3
3
 
4
+ /**
5
+ * Render the header
6
+ * @param ctx A {@link CommandContext | command context}
7
+ * @returns A rendered header
8
+ */
4
9
  declare function renderHeader<Options extends ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
10
+ /**
11
+ * Render the usage
12
+ * @param ctx A {@link CommandContext | command context}
13
+ * @returns A rendered usage
14
+ */
5
15
  declare function renderUsage<Options extends ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
16
+ /**
17
+ * Render the validation errors
18
+ * @param ctx A {@link CommandContext | command context}
19
+ * @param error An {@link AggregateError} of option in `args-token` validation
20
+ * @returns A rendered validation error
21
+ */
6
22
  declare function renderValidationErrors<Options extends ArgOptions>(_ctx: CommandContext<Options>, error: AggregateError): Promise<string>;
7
23
 
8
24
  export { renderHeader, renderUsage, renderValidationErrors };
@@ -25,54 +25,54 @@ declare namespace __constants {
25
25
  */
26
26
  type Awaitable<T> = T | Promise<T>;
27
27
  /**
28
- * The command i18n built-in options keys
28
+ * Command i18n built-in options keys
29
29
  * @experimental
30
30
  */
31
31
  type CommandBuiltinOptionsKeys = keyof (typeof __constants)["COMMON_OPTIONS"];
32
32
  /**
33
- * The command i18n built-in resource keys
33
+ * Command i18n built-in resource keys
34
34
  * @experimental
35
35
  */
36
36
  type CommandBuiltinResourceKeys = (typeof __constants)["COMMAND_I18N_RESOURCE_KEYS"][number];
37
37
  /**
38
- * The command i18n built-in keys
38
+ * Command i18n built-in keys
39
39
  * @description The command i18n built-in keys are used to {@link CommandContext.translation | translate} function
40
40
  * @experimental
41
41
  */
42
42
  type CommandBuiltinKeys = CommandBuiltinOptionsKeys | CommandBuiltinResourceKeys | "description" | "examples";
43
43
  /**
44
- * The command environment
44
+ * Command environment
45
45
  */
46
46
  interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
47
47
  /**
48
- * The current working directory
48
+ * Current working directory
49
49
  * @see {@link CommandOptions.cwd}
50
50
  */
51
51
  cwd: string | undefined;
52
52
  /**
53
- * The command name
53
+ * Command name
54
54
  * @see {@link CommandOptions.name}
55
55
  */
56
56
  name: string | undefined;
57
57
  /**
58
- * The command description
58
+ * Command description
59
59
  * @see {@link CommandOptions.description}
60
60
  *
61
61
  */
62
62
  description: string | undefined;
63
63
  /**
64
- * The command version
64
+ * Command version
65
65
  * @see {@link CommandOptions.version}
66
66
  */
67
67
  version: string | undefined;
68
68
  /**
69
- * The left margin of the command output
69
+ * Left margin of the command output
70
70
  * @default 2
71
71
  * @see {@link CommandOptions.leftMargin}
72
72
  */
73
73
  leftMargin: number;
74
74
  /**
75
- * The middle margin of the command output
75
+ * Middle margin of the command output
76
76
  * @default 10
77
77
  * @see {@link CommandOptions.middleMargin}
78
78
  */
@@ -84,7 +84,7 @@ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
84
84
  */
85
85
  usageOptionType: boolean;
86
86
  /**
87
- * The sub commands
87
+ * Sub commands
88
88
  * @see {@link CommandOptions.subCommands}
89
89
  */
90
90
  subCommands: Map<string, Command<Options> | LazyCommand<Options>> | undefined;
@@ -102,45 +102,40 @@ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
102
102
  renderValidationErrors: ((ctx: CommandContext<Options>, error: AggregateError) => Promise<string>) | null | undefined;
103
103
  }
104
104
  /**
105
- * The command options
105
+ * Command options
106
106
  */
107
107
  interface CommandOptions<Options extends ArgOptions> {
108
108
  /**
109
- * The current working directory
110
- * @description This is the current working directory path passed in the context of the run command. This is useful if you need your command about the current execution directory.
109
+ * Current working directory
111
110
  */
112
111
  cwd?: string;
113
112
  /**
114
- * The command name
115
- * @description Please specify the name of the command that was executed. If you would specify it, gunshi will be displayed in the usage.
113
+ * Command program name
116
114
  */
117
115
  name?: string;
118
116
  /**
119
- * The command description
120
- * @description Please specify the description (summary) of the command that was executed. If you would specify it, gunshi will be displayed in the usage.
117
+ * Command program description
121
118
  *
122
119
  */
123
120
  description?: string;
124
121
  /**
125
- * The command version
126
- * @description Please specify the version of the command that was executed. If you would specify it, gunshi will be displayed in the usage.
122
+ * Command program version
127
123
  */
128
124
  version?: string;
129
125
  /**
130
- * The locale of the command
131
- * @description The locale of the command that was executed. If you would specify it, gunshi command usage will be localized.
126
+ * Command program locale
132
127
  */
133
128
  locale?: string | Intl.Locale;
134
129
  /**
135
- * The sub commands
130
+ * Sub commands
136
131
  */
137
132
  subCommands?: Map<string, Command<Options> | LazyCommand<Options>>;
138
133
  /**
139
- * The left margin of the command output
134
+ * Left margin of the command output
140
135
  */
141
136
  leftMargin?: number;
142
137
  /**
143
- * The middle margin of the command output
138
+ * Middle margin of the command output
144
139
  */
145
140
  middleMargin?: number;
146
141
  /**
@@ -161,44 +156,44 @@ interface CommandOptions<Options extends ArgOptions> {
161
156
  renderValidationErrors?: ((ctx: Readonly<CommandContext<Options>>, error: AggregateError) => Promise<string>) | null;
162
157
  }
163
158
  /**
164
- * The command context
165
- * @description The command context is the context of the command execution
159
+ * Command context
160
+ * @description Command context is the context of the command execution
166
161
  */
167
162
  interface CommandContext<
168
163
  Options extends ArgOptions,
169
164
  Values = ArgValues<Options>
170
165
  > {
171
166
  /**
172
- * The command name, that is the command that is executed
167
+ * Command name, that is the command that is executed
173
168
  * @description The command name is same {@link CommandEnvironment.name}
174
169
  */
175
170
  name: string | undefined;
176
171
  /**
177
- * The command description, that is the description of the command that is executed
172
+ * Command description, that is the description of the command that is executed
178
173
  * @description The command description is same {@link CommandEnvironment.description}
179
174
  */
180
175
  description: string | undefined;
181
176
  /**
182
- * The command locale, that is the locale of the command that is executed
177
+ * Command locale, that is the locale of the command that is executed
183
178
  */
184
179
  locale: Intl.Locale;
185
180
  /**
186
- * The command environment, that is the environment of the command that is executed
181
+ * Command environment, that is the environment of the command that is executed
187
182
  * @description The command environment is same {@link CommandEnvironment}
188
183
  */
189
184
  env: CommandEnvironment<Options>;
190
185
  /**
191
- * The command options, that is the options of the command that is executed
186
+ * Command options, that is the options of the command that is executed
192
187
  * @description The command options is same {@link Command.options}
193
188
  */
194
189
  options: Options | undefined;
195
190
  /**
196
- * The command values, that is the values of the command that is executed
191
+ * Command values, that is the values of the command that is executed
197
192
  * @description Resolve values with `resolveArgs` from command arguments and {@link Command.options}
198
193
  */
199
194
  values: Values;
200
195
  /**
201
- * The command positionals, that is the positionals of the command that is executed
196
+ * Command positionals arguments, that is the positionals of the command that is executed
202
197
  * @description Resolve positionals with `resolveArgs` from command arguments
203
198
  */
204
199
  positionals: string[];
@@ -207,20 +202,20 @@ interface CommandContext<
207
202
  */
208
203
  omitted: boolean;
209
204
  /**
210
- * The usage of the command
211
- * @description The usage of the command is same {@link Command.usage}, and more has `--help` and `--version` options
205
+ * Command usage
206
+ * @description Usage of the command is same {@link Command.usage}, and more has `--help` and `--version` options
212
207
  */
213
208
  usage: CommandUsage<Options>;
214
209
  /**
215
- * Load the sub-commands
210
+ * Load sub-commands
216
211
  * @description The loaded commands are cached and returned when called again
217
212
  * @returns loaded commands
218
213
  */
219
214
  loadCommands: () => Promise<Command<Options>[]>;
220
215
  /**
221
- * The translation function
222
- * @param key {CommandBuiltinKeys | T} - The key to be translated
223
- * @returns The translated string, if the key is not found, the key itself is returned
216
+ * Translation function
217
+ * @param key the key to be translated
218
+ * @returns A translated string
224
219
  * @experimental
225
220
  */
226
221
  translation: <
@@ -229,37 +224,32 @@ interface CommandContext<
229
224
  >(key: Key) => string;
230
225
  }
231
226
  /**
232
- * The command usage render
233
- * @description if the render function is async, it should return a promise
234
- */
235
- type CommandUsageRender<Options extends ArgOptions> = ((ctx: Readonly<CommandContext<Options>>) => Promise<string>) | string;
236
- /**
237
- * The command usage
227
+ * Command usage
238
228
  */
239
229
  interface CommandUsage<Options extends ArgOptions> {
240
230
  /**
241
- * The options usage
231
+ * Options usage
242
232
  */
243
233
  options?: { [Option in keyof Options] : string };
244
234
  /**
245
- * The examples usage
235
+ * Examples usage
246
236
  */
247
237
  examples?: string;
248
238
  }
249
239
  /**
250
- * The command interface
240
+ * Command interface
251
241
  */
252
242
  interface Command<Options extends ArgOptions> {
253
243
  /**
254
- * The command name
244
+ * Command name
255
245
  * @description
256
- * The command name is used to find command line arguments to execute from sub commands, so it's recommended to specify.
246
+ * Command name is used to find command line arguments to execute from sub commands, so it's recommended to specify.
257
247
  */
258
248
  name?: string;
259
249
  /**
260
- * The command description
250
+ * Command description
261
251
  * @description
262
- * The command description is used to describe the command in usage, so it's recommended to specify.
252
+ * Command description is used to describe the command in usage, so it's recommended to specify.
263
253
  */
264
254
  description?: string;
265
255
  /**
@@ -268,57 +258,59 @@ interface Command<Options extends ArgOptions> {
268
258
  */
269
259
  default?: boolean;
270
260
  /**
271
- * The command options
261
+ * Command options
272
262
  */
273
263
  options?: Options;
274
264
  /**
275
- * The command usage
265
+ * Command usage
276
266
  * @description
277
- * The command usage is used to describe the command in usage, so it's recommended to specify.
267
+ * Command usage is used to describe the command in usage, so it's recommended to specify.
278
268
  */
279
269
  usage?: CommandUsage<Options>;
280
270
  /**
281
- * The command runner, that's the command to be executed
271
+ * Command runner, that's the command to be executed
282
272
  */
283
273
  run: CommandRunner<Options>;
284
274
  /**
285
- * The command resource fetcher
275
+ * Command resource fetcher
286
276
  * @experimental
287
277
  */
288
278
  resource?: CommandResourceFetcher<Options>;
289
279
  }
290
280
  /**
291
- * The command resource
281
+ * Command resource
292
282
  * @experimental
293
283
  */
294
284
  interface CommandResource<Options extends ArgOptions> {
295
285
  /**
296
- * The command description resource
286
+ * Command description
297
287
  */
298
288
  description: string;
299
289
  /**
300
- * The options usage resources
290
+ * Options usage
301
291
  */
302
292
  options: { [Option in keyof Options] : string };
303
293
  /**
304
- * The examples usage resources
294
+ * Examples usage
305
295
  */
306
296
  examples: string;
307
297
  }
308
298
  /**
309
- * The command resource fetcher
299
+ * Command resource fetcher
300
+ * @param ctx A {@link CommandContext | command context}
301
+ * @returns A fetched {@link CommandResource | command resource}
310
302
  * @experimental
311
303
  */
312
304
  type CommandResourceFetcher<Options extends ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Promise<CommandResource<Options>>;
313
305
  /**
314
- * The command runner interface
315
- * @param ctx - The {@link CommandContext | command context}
306
+ * Command runner
307
+ * @param ctx A {@link CommandContext | command context}
316
308
  */
317
309
  type CommandRunner<Options extends ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>;
318
310
  /**
319
- * The lazy command interface
320
- * @description The lazy command that's not loaded until it is executed
311
+ * Lazy command interface
312
+ * @description lazy command that's not loaded until it is executed
321
313
  */
322
314
  type LazyCommand<Options extends ArgOptions> = () => Awaitable<Command<Options>>;
323
315
 
324
- export type { Command as C, LazyCommand as L, CommandOptions as a, CommandContext as b, CommandRunner as c, CommandBuiltinOptionsKeys as d, CommandBuiltinResourceKeys as e, CommandBuiltinKeys as f, CommandEnvironment as g, CommandUsageRender as h, CommandResource as i, CommandResourceFetcher as j };
316
+ export type { Command as C, LazyCommand as L, CommandOptions as a, CommandContext as b, CommandRunner as c, CommandBuiltinOptionsKeys as d, CommandBuiltinResourceKeys as e, CommandBuiltinKeys as f, CommandEnvironment as g, CommandResource as h, CommandResourceFetcher as i };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gunshi",
3
3
  "description": "Modern javascript command-line library",
4
- "version": "0.2.1",
4
+ "version": "0.2.2",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"