gunshi 0.15.0 → 0.16.0

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.
@@ -0,0 +1,109 @@
1
+ import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, create, resolveLazyCommand } from "./utils-BYPzZy9X.js";
2
+ import { createCommandContext } from "./context-BROXRnNP.js";
3
+ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BNorS8VG.js";
4
+ import { parseArgs, resolveArgs } from "args-tokens";
5
+
6
+ //#region src/cli.ts
7
+ /**
8
+ * Run the command.
9
+ * @param args Command line arguments
10
+ * @param entry A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
11
+ * @param opts A {@link CommandOptions | command options}
12
+ * @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
13
+ */
14
+ async function cli(args, entry, opts = {}) {
15
+ const tokens = parseArgs(args);
16
+ const subCommand = getSubCommand(tokens);
17
+ const resolvedCommandOptions = resolveCommandOptions(opts, entry);
18
+ const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
19
+ if (!command) throw new Error(`Command not found: ${name || ""}`);
20
+ const options = resolveArgOptions(command.options);
21
+ const { values, positionals, rest, error } = resolveArgs(options, tokens, {
22
+ optionGrouping: true,
23
+ allowNegative: true
24
+ });
25
+ const omitted = !subCommand;
26
+ const ctx = await createCommandContext({
27
+ options,
28
+ values,
29
+ positionals,
30
+ rest,
31
+ args,
32
+ tokens,
33
+ omitted,
34
+ command,
35
+ commandOptions: resolvedCommandOptions
36
+ });
37
+ if (values.version) {
38
+ showVersion(ctx);
39
+ return;
40
+ }
41
+ const usageBuffer = [];
42
+ const header = await showHeader(ctx);
43
+ if (header) usageBuffer.push(header);
44
+ if (values.help) {
45
+ const usage = await showUsage(ctx);
46
+ if (usage) usageBuffer.push(usage);
47
+ return usageBuffer.join("\n");
48
+ }
49
+ if (error) {
50
+ await showValidationErrors(ctx, error);
51
+ return;
52
+ }
53
+ await command.run(ctx);
54
+ }
55
+ function resolveArgOptions(options) {
56
+ return Object.assign(create(), options, COMMON_OPTIONS);
57
+ }
58
+ function resolveCommandOptions(options, entry) {
59
+ const subCommands = new Map(options.subCommands);
60
+ if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
61
+ const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
62
+ return resolvedOptions;
63
+ }
64
+ function getSubCommand(tokens) {
65
+ const firstToken = tokens[0];
66
+ return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
67
+ }
68
+ async function showUsage(ctx) {
69
+ if (ctx.env.renderUsage === null) return;
70
+ const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
71
+ if (usage) {
72
+ ctx.log(usage);
73
+ return usage;
74
+ }
75
+ }
76
+ function showVersion(ctx) {
77
+ ctx.log(ctx.env.version);
78
+ }
79
+ async function showHeader(ctx) {
80
+ if (ctx.env.renderHeader === null) return;
81
+ const header = await (ctx.env.renderHeader || renderHeader)(ctx);
82
+ if (header) {
83
+ ctx.log(header);
84
+ ctx.log();
85
+ return header;
86
+ }
87
+ }
88
+ async function showValidationErrors(ctx, error) {
89
+ if (ctx.env.renderValidationErrors === null) return;
90
+ const render = ctx.env.renderValidationErrors || renderValidationErrors;
91
+ ctx.log(await render(ctx, error));
92
+ }
93
+ async function resolveCommand(sub, entry, options) {
94
+ const omitted = !sub;
95
+ if (typeof entry === "function") return [void 0, { run: entry }];
96
+ else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry)] : [void 0, void 0];
97
+ else {
98
+ if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
99
+ const cmd = options.subCommands?.get(sub);
100
+ if (cmd == null) return [sub, void 0];
101
+ return [sub, await resolveLazyCommand(cmd, sub)];
102
+ }
103
+ }
104
+ function resolveEntryName(entry) {
105
+ return entry.name || "(anonymous)";
106
+ }
107
+
108
+ //#endregion
109
+ export { cli };
@@ -1,5 +1,4 @@
1
- import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey } from "./renderer-BeuJazdk.js";
2
- import { parseArgs, resolveArgs } from "args-tokens";
1
+ import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveLazyCommand, resolveOptionKey } from "./utils-BYPzZy9X.js";
3
2
 
4
3
  //#region src/locales/en-US.json
5
4
  var COMMAND = "COMMAND";
@@ -62,6 +61,11 @@ var DefaultTranslation = class {
62
61
  //#endregion
63
62
  //#region src/context.ts
64
63
  const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
64
+ /**
65
+ * Create a {@link CommandContext | command context}
66
+ * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
67
+ * @returns A {@link CommandContext | command context}, which is readonly
68
+ */
65
69
  async function createCommandContext({ options, values, positionals, rest, args, tokens, command, commandOptions, omitted = false }) {
66
70
  /**
67
71
  * normailize the options schema and values, to avoid prototype pollution
@@ -170,100 +174,4 @@ async function loadCommandResource(ctx, command) {
170
174
  }
171
175
 
172
176
  //#endregion
173
- //#region src/cli.ts
174
- async function cli(args, entry, opts = {}) {
175
- const tokens = parseArgs(args);
176
- const subCommand = getSubCommand(tokens);
177
- const resolvedCommandOptions = resolveCommandOptions(opts, entry);
178
- const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions);
179
- if (!command) throw new Error(`Command not found: ${name || ""}`);
180
- const options = resolveArgOptions(command.options);
181
- const { values, positionals, rest, error } = resolveArgs(options, tokens, {
182
- optionGrouping: true,
183
- allowNegative: true
184
- });
185
- const omitted = !subCommand;
186
- const ctx = await createCommandContext({
187
- options,
188
- values,
189
- positionals,
190
- rest,
191
- args,
192
- tokens,
193
- omitted,
194
- command,
195
- commandOptions: resolvedCommandOptions
196
- });
197
- if (values.version) {
198
- showVersion(ctx);
199
- return;
200
- }
201
- const usageBuffer = [];
202
- const header = await showHeader(ctx);
203
- if (header) usageBuffer.push(header);
204
- if (values.help) {
205
- const usage = await showUsage(ctx);
206
- if (usage) usageBuffer.push(usage);
207
- return usageBuffer.join("\n");
208
- }
209
- if (error) {
210
- await showValidationErrors(ctx, error);
211
- return;
212
- }
213
- await command.run(ctx);
214
- }
215
- function resolveArgOptions(options) {
216
- return Object.assign(create(), options, COMMON_OPTIONS);
217
- }
218
- function resolveCommandOptions(options, entry) {
219
- const subCommands = new Map(options.subCommands);
220
- if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
221
- const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
222
- return resolvedOptions;
223
- }
224
- function getSubCommand(tokens) {
225
- const firstToken = tokens[0];
226
- return firstToken && firstToken.kind === "positional" && firstToken.index === 0 && firstToken.value ? firstToken.value : "";
227
- }
228
- async function showUsage(ctx) {
229
- if (ctx.env.renderUsage === null) return;
230
- const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
231
- if (usage) {
232
- ctx.log(usage);
233
- return usage;
234
- }
235
- }
236
- function showVersion(ctx) {
237
- ctx.log(ctx.env.version);
238
- }
239
- async function showHeader(ctx) {
240
- if (ctx.env.renderHeader === null) return;
241
- const header = await (ctx.env.renderHeader || renderHeader)(ctx);
242
- if (header) {
243
- ctx.log(header);
244
- ctx.log();
245
- return header;
246
- }
247
- }
248
- async function showValidationErrors(ctx, error) {
249
- if (ctx.env.renderValidationErrors === null) return;
250
- const render = ctx.env.renderValidationErrors || renderValidationErrors;
251
- ctx.log(await render(ctx, error));
252
- }
253
- async function resolveCommand(sub, entry, options) {
254
- const omitted = !sub;
255
- if (typeof entry === "function") return [void 0, { run: entry }];
256
- else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry)] : [void 0, void 0];
257
- else {
258
- if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
259
- const cmd = options.subCommands?.get(sub);
260
- if (cmd == null) return [sub, void 0];
261
- return [sub, await resolveLazyCommand(cmd, sub)];
262
- }
263
- }
264
- function resolveEntryName(entry) {
265
- return entry.name || "(anonymous)";
266
- }
267
-
268
- //#endregion
269
- export { DefaultTranslation, cli };
177
+ export { DefaultTranslation, createCommandContext };
@@ -0,0 +1,54 @@
1
+ import { Command, CommandContext, CommandOptions } from "./types.d-aDzUZTqM.js";
2
+ import { ArgOptions, ArgToken, ArgValues } from "args-tokens";
3
+
4
+ //#region src/context.d.ts
5
+ /**
6
+ * Parameters of {@link createCommandContext}
7
+ */
8
+ interface CommandContextParams<Options extends ArgOptions, Values> {
9
+ /**
10
+ * An options of target command
11
+ */
12
+ options: Options;
13
+ /**
14
+ * A values of target command
15
+ */
16
+ values: Values;
17
+ /**
18
+ * A positionals arguments, which passed to the target command
19
+ */
20
+ positionals: string[];
21
+ /**
22
+ * A rest arguments, which passed to the target command
23
+ */
24
+ rest: string[];
25
+ /**
26
+ * Original command line arguments
27
+ */
28
+ args: string[];
29
+ /**
30
+ * Argument tokens that are parsed by the `parseArgs` function
31
+ */
32
+ tokens: ArgToken[];
33
+ /**
34
+ * Whether the command is omitted
35
+ */
36
+ omitted: boolean;
37
+ /**
38
+ * A target {@link Command | command}
39
+ */
40
+ command: Command<Options>;
41
+ /**
42
+ * A command options, which is spicialized from `cli` function
43
+ */
44
+ commandOptions: CommandOptions<Options>;
45
+ }
46
+ /**
47
+ * Create a {@link CommandContext | command context}
48
+ * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
49
+ * @returns A {@link CommandContext | command context}, which is readonly
50
+ */
51
+ declare function createCommandContext<Options extends ArgOptions = ArgOptions, Values extends ArgValues<Options> = ArgValues<Options>>({ options, values, positionals, rest, args, tokens, command, commandOptions, omitted }: CommandContextParams<Options, Values>): Promise<Readonly<CommandContext<Options, Values>>>;
52
+
53
+ //#endregion
54
+ export { createCommandContext };
package/lib/context.js ADDED
@@ -0,0 +1,4 @@
1
+ import "./utils-BYPzZy9X.js";
2
+ import { createCommandContext } from "./context-BROXRnNP.js";
3
+
4
+ export { createCommandContext };
@@ -0,0 +1,12 @@
1
+ //#region src/definition.ts
2
+ /**
3
+ * Define a {@link Command | command} with type inference
4
+ * @param definition A {@link Command | command} definition
5
+ * @returns A {@link Command | command} definition with type inference
6
+ */
7
+ function define(definition) {
8
+ return definition;
9
+ }
10
+
11
+ //#endregion
12
+ export { define };
@@ -1,12 +1,12 @@
1
- import { Command } from "./types.d-zAq1WIpD.js";
1
+ import { Command } from "./types.d-aDzUZTqM.js";
2
2
  import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues as ArgValues$1 } from "args-tokens";
3
3
 
4
4
  //#region src/definition.d.ts
5
5
  /**
6
- * Define a {@link Command | command} with type inference
7
- * @param definition A {@link Command | command} definition
8
- * @returns A {@link Command | command} definition with type inference
9
- */
6
+ * Define a {@link Command | command} with type inference
7
+ * @param definition A {@link Command | command} definition
8
+ * @returns A {@link Command | command} definition with type inference
9
+ */
10
10
  declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>;
11
11
 
12
12
  //#endregion
@@ -1,4 +1,4 @@
1
- import "./types.d-zAq1WIpD.js";
2
- import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-DD_GraLw.js";
1
+ import "./types.d-aDzUZTqM.js";
2
+ import { ArgOptionSchema, ArgOptions, ArgValues, define$1 as define } from "./definition.d-DllW_uD3.js";
3
3
 
4
4
  export { ArgOptionSchema, ArgOptions, ArgValues, define };
package/lib/definition.js CHANGED
@@ -1,3 +1,3 @@
1
- import { define } from "./definition-BAm6f1St.js";
1
+ import { define } from "./definition-VzcnM0si.js";
2
2
 
3
3
  export { define };
@@ -1,14 +1,14 @@
1
- import { Command, CommandOptions } from "./types.d-zAq1WIpD.js";
1
+ import { Command, CommandOptions } from "./types.d-aDzUZTqM.js";
2
2
  import { ArgOptions } from "args-tokens";
3
3
 
4
4
  //#region src/generator.d.ts
5
5
  /**
6
- * Generate the command usage.
7
- * @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
8
- * @param entry - A {@link Command | entry command}
9
- * @param opts - A {@link CommandOptions | command options}
10
- * @returns A rendered usage.
11
- */
6
+ * Generate the command usage.
7
+ * @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
8
+ * @param entry - A {@link Command | entry command}
9
+ * @param opts - A {@link CommandOptions | command options}
10
+ * @returns A rendered usage.
11
+ */
12
12
  declare function generate<Options extends ArgOptions = ArgOptions>(command: string | null, entry: Command<Options>, opts?: CommandOptions<Options>): Promise<string>;
13
13
 
14
14
  //#endregion
package/lib/generator.js CHANGED
@@ -1,7 +1,16 @@
1
- import { create } from "./renderer-BeuJazdk.js";
2
- import { cli } from "./cli--6qYDY8U.js";
1
+ import { create } from "./utils-BYPzZy9X.js";
2
+ import "./context-BROXRnNP.js";
3
+ import "./renderer-BNorS8VG.js";
4
+ import { cli } from "./cli-B7eqtqBF.js";
3
5
 
4
6
  //#region src/generator.ts
7
+ /**
8
+ * Generate the command usage.
9
+ * @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
10
+ * @param entry - A {@link Command | entry command}
11
+ * @param opts - A {@link CommandOptions | command options}
12
+ * @returns A rendered usage.
13
+ */
5
14
  async function generate(command, entry, opts = {}) {
6
15
  const args = ["-h"];
7
16
  if (command != null) args.unshift(command);
package/lib/index.d.ts CHANGED
@@ -1,15 +1,15 @@
1
- import { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types.d-zAq1WIpD.js";
2
- import { define$1 as define } from "./definition.d-DD_GraLw.js";
1
+ import { Command, CommandBuiltinKeys, CommandBuiltinOptionsKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandOptionKeys, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgOptions, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types.d-aDzUZTqM.js";
2
+ import { define$1 as define } from "./definition.d-DllW_uD3.js";
3
3
  import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues, parseArgs, resolveArgs } from "args-tokens";
4
4
 
5
5
  //#region src/cli.d.ts
6
6
  /**
7
- * Run the command.
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}
11
- * @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
12
- */
7
+ * Run the command.
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}
11
+ * @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
12
+ */
13
13
  declare function cli<Options extends ArgOptions$1 = ArgOptions$1>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
14
14
 
15
15
  //#endregion
package/lib/index.js CHANGED
@@ -1,6 +1,8 @@
1
- import { define } from "./definition-BAm6f1St.js";
2
- import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./renderer-BeuJazdk.js";
3
- import { DefaultTranslation, cli } from "./cli--6qYDY8U.js";
1
+ import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-BYPzZy9X.js";
2
+ import { DefaultTranslation } from "./context-BROXRnNP.js";
3
+ import { define } from "./definition-VzcnM0si.js";
4
+ import "./renderer-BNorS8VG.js";
5
+ import { cli } from "./cli-B7eqtqBF.js";
4
6
  import { parseArgs, resolveArgs } from "args-tokens";
5
7
 
6
8
  export { DEFAULT_LOCALE, DefaultTranslation, cli, define, parseArgs, resolveArgs };
@@ -1,74 +1,11 @@
1
+ import { create, resolveBuiltInKey, resolveOptionKey } from "./utils-BYPzZy9X.js";
1
2
 
2
- //#region src/constants.ts
3
- const DEFAULT_LOCALE = "en-US";
4
- const BUILT_IN_PREFIX = "_";
5
- const OPTION_PREFIX = "Option";
6
- const BUILT_IN_KEY_SEPARATOR = ":";
7
- const NOOP = () => {};
8
- const COMMON_OPTIONS = {
9
- help: {
10
- type: "boolean",
11
- short: "h",
12
- description: "Display this help message"
13
- },
14
- version: {
15
- type: "boolean",
16
- short: "v",
17
- description: "Display this version"
18
- }
19
- };
20
- const COMMAND_OPTIONS_DEFAULT = {
21
- name: void 0,
22
- description: void 0,
23
- version: void 0,
24
- cwd: void 0,
25
- usageSilent: false,
26
- subCommands: void 0,
27
- leftMargin: 2,
28
- middleMargin: 10,
29
- usageOptionType: false,
30
- renderHeader: void 0,
31
- renderUsage: void 0,
32
- renderValidationErrors: void 0,
33
- translationAdapterFactory: void 0
34
- };
35
-
36
- //#endregion
37
- //#region src/utils.ts
38
- async function resolveLazyCommand(cmd, name) {
39
- const resolved = Object.assign(create(), typeof cmd == "function" ? await cmd() : cmd);
40
- if (resolved.name == null && name) resolved.name = name;
41
- return deepFreeze(resolved);
42
- }
43
- function resolveBuiltInKey(key) {
44
- return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
45
- }
46
- function resolveOptionKey(key) {
47
- return `${OPTION_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
48
- }
49
- function mapResourceWithBuiltinKey(resource) {
50
- return Object.entries(resource).reduce((acc, [key, value]) => {
51
- acc[resolveBuiltInKey(key)] = value;
52
- return acc;
53
- }, create());
54
- }
55
- function create(obj = null) {
56
- return Object.create(obj);
57
- }
58
- function log(...args) {
59
- console.log(...args);
60
- }
61
- function deepFreeze(obj) {
62
- if (obj === null || typeof obj !== "object") return obj;
63
- for (const key of Object.keys(obj)) {
64
- const value = obj[key];
65
- if (typeof value === "object" && value !== null) deepFreeze(value);
66
- }
67
- return Object.freeze(obj);
68
- }
69
-
70
- //#endregion
71
3
  //#region src/renderer/header.ts
4
+ /**
5
+ * Render the header.
6
+ * @param ctx A {@link CommandContext | command context}
7
+ * @returns A rendered header.
8
+ */
72
9
  function renderHeader(ctx) {
73
10
  const title = ctx.env.description || ctx.env.name || "";
74
11
  return Promise.resolve(title ? `${title} (${ctx.env.name || ""}${ctx.env.version ? ` v${ctx.env.version}` : ""})` : title);
@@ -76,6 +13,11 @@ function renderHeader(ctx) {
76
13
 
77
14
  //#endregion
78
15
  //#region src/renderer/usage.ts
16
+ /**
17
+ * Render the usage.
18
+ * @param ctx A {@link CommandContext | command context}
19
+ * @returns A rendered usage.
20
+ */
79
21
  async function renderUsage(ctx) {
80
22
  const messages = [];
81
23
  if (!ctx.omitted) {
@@ -274,6 +216,12 @@ async function generateOptionsUsage(ctx, optionsPairs) {
274
216
 
275
217
  //#endregion
276
218
  //#region src/renderer/validation.ts
219
+ /**
220
+ * Render the validation errors.
221
+ * @param ctx A {@link CommandContext | command context}
222
+ * @param error An {@link AggregateError} of option in `args-token` validation
223
+ * @returns A rendered validation error.
224
+ */
277
225
  function renderValidationErrors(_ctx, error) {
278
226
  const messages = [];
279
227
  for (const err of error.errors) messages.push(err.message);
@@ -281,4 +229,4 @@ function renderValidationErrors(_ctx, error) {
281
229
  }
282
230
 
283
231
  //#endregion
284
- export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE as DEFAULT_LOCALE$1, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey };
232
+ export { renderHeader, renderUsage, renderValidationErrors };
package/lib/renderer.d.ts CHANGED
@@ -1,31 +1,31 @@
1
- import { CommandContext } from "./types.d-zAq1WIpD.js";
1
+ import { CommandContext } from "./types.d-aDzUZTqM.js";
2
2
  import { ArgOptions } from "args-tokens";
3
3
 
4
4
  //#region src/renderer/header.d.ts
5
5
  /**
6
- * Render the header.
7
- * @param ctx A {@link CommandContext | command context}
8
- * @returns A rendered header.
9
- */
6
+ * Render the header.
7
+ * @param ctx A {@link CommandContext | command context}
8
+ * @returns A rendered header.
9
+ */
10
10
  declare function renderHeader<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
11
11
 
12
12
  //#endregion
13
13
  //#region src/renderer/usage.d.ts
14
14
  /**
15
- * Render the usage.
16
- * @param ctx A {@link CommandContext | command context}
17
- * @returns A rendered usage.
18
- */
15
+ * Render the usage.
16
+ * @param ctx A {@link CommandContext | command context}
17
+ * @returns A rendered usage.
18
+ */
19
19
  declare function renderUsage<Options extends ArgOptions = ArgOptions>(ctx: Readonly<CommandContext<Options>>): Promise<string>;
20
20
 
21
21
  //#endregion
22
22
  //#region src/renderer/validation.d.ts
23
23
  /**
24
- * Render the validation errors.
25
- * @param ctx A {@link CommandContext | command context}
26
- * @param error An {@link AggregateError} of option in `args-token` validation
27
- * @returns A rendered validation error.
28
- */
24
+ * Render the validation errors.
25
+ * @param ctx A {@link CommandContext | command context}
26
+ * @param error An {@link AggregateError} of option in `args-token` validation
27
+ * @returns A rendered validation error.
28
+ */
29
29
  declare function renderValidationErrors<Options extends ArgOptions = ArgOptions>(_ctx: CommandContext<Options>, error: AggregateError): Promise<string>;
30
30
 
31
31
  //#endregion
package/lib/renderer.js CHANGED
@@ -1,3 +1,4 @@
1
- import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BeuJazdk.js";
1
+ import "./utils-BYPzZy9X.js";
2
+ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BNorS8VG.js";
2
3
 
3
4
  export { renderHeader, renderUsage, renderValidationErrors };
@@ -11,8 +11,8 @@ declare namespace constants_d_exports {
11
11
  export { BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, OPTION_PREFIX, }
12
12
  }
13
13
  /**
14
- * The default locale string, which format is BCP 47 language tag.
15
- */
14
+ * The default locale string, which format is BCP 47 language tag.
15
+ */
16
16
  declare const DEFAULT_LOCALE = "en-US";
17
17
  declare const BUILT_IN_PREFIX = "_";
18
18
  declare const OPTION_PREFIX = "Option";
@@ -41,8 +41,8 @@ type RemoveIndexSignature<T> = {
41
41
  [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
42
42
  };
43
43
  /**
44
- * Remove index signature from object or record type.
45
- */
44
+ * Remove index signature from object or record type.
45
+ */
46
46
  type RemovedIndex<T> = RemoveIndexSignature<{
47
47
  [K in keyof T]: T[K];
48
48
  }>;
@@ -50,30 +50,30 @@ type KeyOfArgOptions<Options extends ArgOptions> = keyof Options | {
50
50
  [K in keyof Options]: Options[K]['type'] extends 'boolean' ? `no-${Extract<K, string>}` : never;
51
51
  }[keyof Options];
52
52
  /**
53
- * Generate a namespaced key.
54
- */
53
+ * Generate a namespaced key.
54
+ */
55
55
  type GenerateNamespacedKey<Key extends string, Prefixed extends string = typeof BUILT_IN_PREFIX> = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
56
56
  /**
57
- * Command i18n built-in options keys.
58
- */
57
+ * Command i18n built-in options keys.
58
+ */
59
59
  type CommandBuiltinOptionsKeys = keyof (typeof constants_d_exports)['COMMON_OPTIONS'];
60
60
  /**
61
- * Command i18n built-in resource keys.
62
- */
61
+ * Command i18n built-in resource keys.
62
+ */
63
63
  type CommandBuiltinResourceKeys = (typeof constants_d_exports)['COMMAND_BUILTIN_RESOURCE_KEYS'][number];
64
64
  /**
65
- * Command i18n built-in keys.
66
- * The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
67
- */
65
+ * Command i18n built-in keys.
66
+ * The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
67
+ */
68
68
  type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinOptionsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | 'description' | 'examples';
69
69
  /**
70
- * Command i18n option keys.
71
- * The command i18n option keys are used to {@link CommandContext.translate | translate} function.
72
- */
70
+ * Command i18n option keys.
71
+ * The command i18n option keys are used to {@link CommandContext.translate | translate} function.
72
+ */
73
73
  type CommandOptionKeys<Options extends ArgOptions> = GenerateNamespacedKey<KeyOfArgOptions<RemovedIndex<Options>>, typeof OPTION_PREFIX>;
74
74
  /**
75
- * Command environment.
76
- */
75
+ * Command environment.
76
+ */
77
77
  interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
78
78
  /**
79
79
  * Current working directory.
@@ -139,8 +139,8 @@ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
139
139
  renderValidationErrors: ((ctx: CommandContext<Options>, error: AggregateError) => Promise<string>) | null | undefined;
140
140
  }
141
141
  /**
142
- * Command options.
143
- */
142
+ * Command options.
143
+ */
144
144
  interface CommandOptions<Options extends ArgOptions = ArgOptions> {
145
145
  /**
146
146
  * Current working directory.
@@ -201,9 +201,9 @@ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
201
201
  translationAdapterFactory?: TranslationAdapterFactory;
202
202
  }
203
203
  /**
204
- * Command context.
205
- * Command context is the context of the command execution.
206
- */
204
+ * Command context.
205
+ * Command context is the context of the command execution.
206
+ */
207
207
  interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> {
208
208
  /**
209
209
  * Command name, that is the command that is executed.
@@ -278,8 +278,8 @@ interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgVa
278
278
  translate: <T extends string = CommandBuiltinKeys, O = CommandOptionKeys<Options>, Key = CommandBuiltinKeys | O | T>(key: Key, values?: Record<string, unknown>) => string;
279
279
  }
280
280
  /**
281
- * Command interface.
282
- */
281
+ * Command interface.
282
+ */
283
283
  interface Command<Options extends ArgOptions = ArgOptions> {
284
284
  /**
285
285
  * Command name.
@@ -311,8 +311,8 @@ interface Command<Options extends ArgOptions = ArgOptions> {
311
311
  resource?: CommandResourceFetcher<Options>;
312
312
  }
313
313
  /**
314
- * Command resource.
315
- */
314
+ * Command resource.
315
+ */
316
316
  type CommandResource<Options extends ArgOptions = ArgOptions> = {
317
317
  /**
318
318
  * Command description.
@@ -328,18 +328,18 @@ type CommandResource<Options extends ArgOptions = ArgOptions> = {
328
328
  [key: string]: string;
329
329
  };
330
330
  /**
331
- * Command resource fetcher.
332
- * @param ctx A {@link CommandContext | command context}
333
- * @returns A fetched {@link CommandResource | command resource}.
334
- */
331
+ * Command resource fetcher.
332
+ * @param ctx A {@link CommandContext | command context}
333
+ * @returns A fetched {@link CommandResource | command resource}.
334
+ */
335
335
  type CommandResourceFetcher<Options extends ArgOptions = ArgOptions, Values = ArgValues<Options>> = (ctx: Readonly<CommandContext<Options, Values>>) => Promise<CommandResource<Options>>;
336
336
  /**
337
- * Translation adapter factory.
338
- */
337
+ * Translation adapter factory.
338
+ */
339
339
  type TranslationAdapterFactory = (options: TranslationAdapterFactoryOptions) => TranslationAdapter;
340
340
  /**
341
- * Translation adapter factory options.
342
- */
341
+ * Translation adapter factory options.
342
+ */
343
343
  interface TranslationAdapterFactoryOptions {
344
344
  /**
345
345
  * A locale.
@@ -351,10 +351,10 @@ interface TranslationAdapterFactoryOptions {
351
351
  fallbackLocale: string;
352
352
  }
353
353
  /**
354
- * Translation adapter.
355
- * This adapter is used to custom message formatter like {@link https://github.com/intlify/vue-i18n/blob/master/spec/syntax.ebnf | Intlify message format}, {@link https://github.com/tc39/proposal-intl-messageformat | `Intl.MessageFormat` (MF2)}, and etc.
356
- * This adapter will support localization with your preferred message format.
357
- */
354
+ * Translation adapter.
355
+ * This adapter is used to custom message formatter like {@link https://github.com/intlify/vue-i18n/blob/master/spec/syntax.ebnf | Intlify message format}, {@link https://github.com/tc39/proposal-intl-messageformat | `Intl.MessageFormat` (MF2)}, and etc.
356
+ * This adapter will support localization with your preferred message format.
357
+ */
358
358
  interface TranslationAdapter<MessageResource = string> {
359
359
  /**
360
360
  * Get a resource of locale.
@@ -385,18 +385,18 @@ interface TranslationAdapter<MessageResource = string> {
385
385
  translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
386
386
  }
387
387
  /**
388
- * Command runner.
389
- * @param ctx A {@link CommandContext | command context}
390
- */
388
+ * Command runner.
389
+ * @param ctx A {@link CommandContext | command context}
390
+ */
391
391
  type CommandRunner<Options extends ArgOptions = ArgOptions> = (ctx: Readonly<CommandContext<Options>>) => Awaitable<void>;
392
392
  /**
393
- * Lazy command interface.
394
- * Lazy command that's not loaded until it is executed.
395
- */
393
+ * Lazy command interface.
394
+ * Lazy command that's not loaded until it is executed.
395
+ */
396
396
  type LazyCommand<Options extends ArgOptions = ArgOptions> = () => Awaitable<Command<Options>>;
397
397
  /**
398
- * Define a command type.
399
- */
398
+ * Define a command type.
399
+ */
400
400
  type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>;
401
401
 
402
402
  //#endregion
@@ -0,0 +1,73 @@
1
+ //#region src/constants.ts
2
+ /**
3
+ * The default locale string, which format is BCP 47 language tag.
4
+ */
5
+ const DEFAULT_LOCALE = "en-US";
6
+ const BUILT_IN_PREFIX = "_";
7
+ const OPTION_PREFIX = "Option";
8
+ const BUILT_IN_KEY_SEPARATOR = ":";
9
+ const NOOP = () => {};
10
+ const COMMON_OPTIONS = {
11
+ help: {
12
+ type: "boolean",
13
+ short: "h",
14
+ description: "Display this help message"
15
+ },
16
+ version: {
17
+ type: "boolean",
18
+ short: "v",
19
+ description: "Display this version"
20
+ }
21
+ };
22
+ const COMMAND_OPTIONS_DEFAULT = {
23
+ name: void 0,
24
+ description: void 0,
25
+ version: void 0,
26
+ cwd: void 0,
27
+ usageSilent: false,
28
+ subCommands: void 0,
29
+ leftMargin: 2,
30
+ middleMargin: 10,
31
+ usageOptionType: false,
32
+ renderHeader: void 0,
33
+ renderUsage: void 0,
34
+ renderValidationErrors: void 0,
35
+ translationAdapterFactory: void 0
36
+ };
37
+
38
+ //#endregion
39
+ //#region src/utils.ts
40
+ async function resolveLazyCommand(cmd, name) {
41
+ const resolved = Object.assign(create(), typeof cmd == "function" ? await cmd() : cmd);
42
+ if (resolved.name == null && name) resolved.name = name;
43
+ return deepFreeze(resolved);
44
+ }
45
+ function resolveBuiltInKey(key) {
46
+ return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
47
+ }
48
+ function resolveOptionKey(key) {
49
+ return `${OPTION_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
50
+ }
51
+ function mapResourceWithBuiltinKey(resource) {
52
+ return Object.entries(resource).reduce((acc, [key, value]) => {
53
+ acc[resolveBuiltInKey(key)] = value;
54
+ return acc;
55
+ }, create());
56
+ }
57
+ function create(obj = null) {
58
+ return Object.create(obj);
59
+ }
60
+ function log(...args) {
61
+ console.log(...args);
62
+ }
63
+ function deepFreeze(obj) {
64
+ if (obj === null || typeof obj !== "object") return obj;
65
+ for (const key of Object.keys(obj)) {
66
+ const value = obj[key];
67
+ if (typeof value === "object" && value !== null) deepFreeze(value);
68
+ }
69
+ return Object.freeze(obj);
70
+ }
71
+
72
+ //#endregion
73
+ export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE as DEFAULT_LOCALE$1, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveBuiltInKey, resolveLazyCommand, resolveOptionKey };
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.15.0",
4
+ "version": "0.16.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -47,6 +47,12 @@
47
47
  "require": "./lib/definition.js",
48
48
  "default": "./lib/definition.js"
49
49
  },
50
+ "./context": {
51
+ "types": "./lib/context.d.ts",
52
+ "import": "./lib/context.js",
53
+ "require": "./lib/context.js",
54
+ "default": "./lib/context.js"
55
+ },
50
56
  "./renderer": {
51
57
  "types": "./lib/renderer.d.ts",
52
58
  "import": "./lib/renderer.js",
@@ -97,14 +103,14 @@
97
103
  "eslint-plugin-yml": "^1.17.0",
98
104
  "gh-changelogen": "^0.2.8",
99
105
  "jsr": "^0.13.4",
106
+ "jsr-exports-lint": "^0.2.0",
100
107
  "knip": "^5.50.2",
101
108
  "lint-staged": "^15.5.0",
102
109
  "messageformat": "4.0.0-10",
103
110
  "pkg-pr-new": "^0.0.43",
104
111
  "prettier": "^3.5.3",
105
112
  "publint": "^0.3.11",
106
- "tsdown": "^0.9.1",
107
- "tsdown-jsr-exports-lint": "^0.1.4",
113
+ "tsdown": "^0.9.6",
108
114
  "typedoc": "^0.28.2",
109
115
  "typedoc-plugin-markdown": "^4.6.2",
110
116
  "typedoc-vitepress-theme": "^1.1.2",
@@ -1,8 +0,0 @@
1
-
2
- //#region src/definition.ts
3
- function define(definition) {
4
- return definition;
5
- }
6
-
7
- //#endregion
8
- export { define };