gunshi 0.20.0 → 0.22.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.
@@ -1,21 +1,14 @@
1
1
  import { COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, create, resolveLazyCommand } from "./utils-STQBqMtf.js";
2
- import { createCommandContext } from "./context-IwpAqLSL.js";
2
+ import { createCommandContext } from "./context-BefYjlE-.js";
3
3
  import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BpwhGYRZ.js";
4
4
  import { parseArgs, resolveArgs } from "args-tokens";
5
5
 
6
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
7
  async function cli(argv, entry, opts = {}) {
15
8
  const tokens = parseArgs(argv);
16
9
  const subCommand = getSubCommand(tokens);
17
10
  const resolvedCommandOptions = resolveCommandOptions(opts, entry);
18
- const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions, true);
11
+ const [name, command, callMode] = await resolveCommand(subCommand, entry, resolvedCommandOptions, true);
19
12
  if (!command) throw new Error(`Command not found: ${name || ""}`);
20
13
  const args = resolveArguments(command.args);
21
14
  const { values, positionals, rest, error } = resolveArgs(args, tokens, {
@@ -31,6 +24,7 @@ async function cli(argv, entry, opts = {}) {
31
24
  argv,
32
25
  tokens,
33
26
  omitted,
27
+ callMode,
34
28
  command,
35
29
  commandOptions: resolvedCommandOptions
36
30
  });
@@ -58,7 +52,10 @@ function resolveArguments(options) {
58
52
  }
59
53
  function resolveCommandOptions(options, entry) {
60
54
  const subCommands = new Map(options.subCommands);
61
- if (typeof entry === "object" && entry.name && options.subCommands) subCommands.set(entry.name, entry);
55
+ if (options.subCommands) {
56
+ if (typeof entry === "function" && "commandName" in entry && entry.commandName) subCommands.set(entry.commandName, entry);
57
+ else if (typeof entry === "object" && entry.name) subCommands.set(entry.name, entry);
58
+ }
62
59
  const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
63
60
  return resolvedOptions;
64
61
  }
@@ -91,16 +88,43 @@ async function showValidationErrors(ctx, error) {
91
88
  const render = ctx.env.renderValidationErrors || renderValidationErrors;
92
89
  ctx.log(await render(ctx, error));
93
90
  }
91
+ const CANNOT_RESOLVE_COMMAND = [
92
+ void 0,
93
+ void 0,
94
+ "unexpected"
95
+ ];
94
96
  async function resolveCommand(sub, entry, options, needRunResolving = false) {
95
97
  const omitted = !sub;
96
- if (typeof entry === "function") return [void 0, { run: entry }];
97
- else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry, "", needRunResolving)] : [void 0, void 0];
98
- else {
99
- if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry, "", needRunResolving)];
100
- const cmd = options.subCommands?.get(sub);
101
- if (cmd == null) return [sub, void 0];
102
- return [sub, await resolveLazyCommand(cmd, sub, needRunResolving)];
98
+ async function doResolveCommand() {
99
+ if (typeof entry === "function") if ("commandName" in entry && entry.commandName) return [
100
+ entry.commandName,
101
+ await resolveLazyCommand(entry, "", needRunResolving),
102
+ "entry"
103
+ ];
104
+ else return [
105
+ void 0,
106
+ { run: entry },
107
+ "entry"
108
+ ];
109
+ else if (typeof entry === "object") return [
110
+ resolveEntryName(entry),
111
+ await resolveLazyCommand(entry, "", needRunResolving),
112
+ "entry"
113
+ ];
114
+ else return CANNOT_RESOLVE_COMMAND;
103
115
  }
116
+ if (omitted || options.subCommands?.size === 0) return doResolveCommand();
117
+ const cmd = options.subCommands?.get(sub);
118
+ if (cmd == null) return [
119
+ sub,
120
+ void 0,
121
+ "unexpected"
122
+ ];
123
+ return [
124
+ sub,
125
+ await resolveLazyCommand(cmd, sub, needRunResolving),
126
+ "subCommand"
127
+ ];
104
128
  }
105
129
  function resolveEntryName(entry) {
106
130
  return entry.name || "(anonymous)";
@@ -72,7 +72,7 @@ const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
72
72
  * @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
73
73
  * @returns A {@link CommandContext | command context}, which is readonly
74
74
  */
75
- async function createCommandContext({ args, values, positionals, rest, argv, tokens, command, commandOptions, omitted = false }) {
75
+ async function createCommandContext({ args, values, positionals, rest, argv, tokens, command, commandOptions, callMode = "entry", omitted = false }) {
76
76
  /**
77
77
  * normailize the options schema and values, to avoid prototype pollution
78
78
  */
@@ -128,6 +128,7 @@ async function createCommandContext({ args, values, positionals, rest, argv, tok
128
128
  name: command.name,
129
129
  description: command.description,
130
130
  omitted,
131
+ callMode,
131
132
  locale,
132
133
  env,
133
134
  args: _args,
package/lib/context.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Command, CommandContext, CommandOptions } from "./types-iztBdPY6.js";
1
+ import { Command, CommandCallMode, CommandContext, CommandOptions } from "./types-BbgfNBFM.js";
2
2
  import { ArgToken, ArgValues, Args } from "args-tokens";
3
3
 
4
4
  //#region src/context.d.ts
@@ -38,6 +38,10 @@ interface CommandContextParams<A extends Args, V> {
38
38
  * Whether the command is omitted
39
39
  */
40
40
  omitted: boolean;
41
+ /**
42
+ * Command call mode.
43
+ */
44
+ callMode: CommandCallMode;
41
45
  /**
42
46
  * A target {@link Command | command}
43
47
  */
@@ -61,6 +65,7 @@ declare function createCommandContext<A extends Args = Args, V extends ArgValues
61
65
  tokens,
62
66
  command,
63
67
  commandOptions,
68
+ callMode,
64
69
  omitted
65
70
  }: CommandContextParams<A, V>): Promise<Readonly<CommandContext<A, V>>>; //#endregion
66
71
  export { createCommandContext };
package/lib/context.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import "./utils-STQBqMtf.js";
2
- import { createCommandContext } from "./context-IwpAqLSL.js";
2
+ import { createCommandContext } from "./context-BefYjlE-.js";
3
3
 
4
4
  export { createCommandContext };
@@ -1,4 +1,4 @@
1
- import { Command, CommandLoader, LazyCommand } from "./types-iztBdPY6.js";
1
+ import { Command, CommandLoader, LazyCommand } from "./types-BbgfNBFM.js";
2
2
  import { ArgSchema, ArgValues as ArgValues$1, Args, Args as Args$1 } from "args-tokens";
3
3
 
4
4
  //#region src/definition.d.ts
@@ -1,3 +1,3 @@
1
- import "./types-iztBdPY6.js";
2
- import { ArgSchema, ArgValues, Args, define$1 as define, lazy$1 as lazy } from "./definition-BrCD_SuG.js";
1
+ import "./types-BbgfNBFM.js";
2
+ import { ArgSchema, ArgValues, Args, define$1 as define, lazy$1 as lazy } from "./definition-DUNcKGVg.js";
3
3
  export { ArgSchema, ArgValues, Args, define, lazy };
@@ -1,4 +1,4 @@
1
- import { Command, CommandOptions } from "./types-iztBdPY6.js";
1
+ import { Command, CommandOptions } from "./types-BbgfNBFM.js";
2
2
  import { Args } from "args-tokens";
3
3
 
4
4
  //#region src/generator.d.ts
package/lib/generator.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { create } from "./utils-STQBqMtf.js";
2
- import "./context-IwpAqLSL.js";
2
+ import "./context-BefYjlE-.js";
3
3
  import "./renderer-BpwhGYRZ.js";
4
- import { cli } from "./cli-rwwoCHjK.js";
4
+ import { cli } from "./cli-DEuAxEk9.js";
5
5
 
6
6
  //#region src/generator.ts
7
7
  /**
package/lib/index.d.ts CHANGED
@@ -1,24 +1,10 @@
1
- import { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types-iztBdPY6.js";
2
- import { define$1 as define, lazy$1 as lazy } from "./definition-BrCD_SuG.js";
1
+ import { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types-BbgfNBFM.js";
2
+ import { define$1 as define, lazy$1 as lazy } from "./definition-DUNcKGVg.js";
3
3
  import { ArgSchema, ArgValues, Args, Args as Args$1, parseArgs, resolveArgs } from "args-tokens";
4
4
 
5
5
  //#region src/cli.d.ts
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
- */
13
6
 
14
- /**
15
- * Run the command.
16
- * @param args Command line arguments
17
- * @param entry A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
18
- * @param opts A {@link CommandOptions | command options}
19
- * @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
20
- */
21
- declare function cli<A extends Args$1 = Args$1>(argv: string[], entry: Command<A> | CommandRunner<A>, opts?: CommandOptions<A>): Promise<string | undefined>;
7
+ declare function cli<A extends Args$1 = Args$1>(argv: string[], entry: Command<A> | CommandRunner<A> | LazyCommand<A>, opts?: CommandOptions<A>): Promise<string | undefined>;
22
8
 
23
9
  //#endregion
24
10
  //#region src/translation.d.ts
@@ -32,4 +18,4 @@ declare class DefaultTranslation implements TranslationAdapter {
32
18
  }
33
19
 
34
20
  //#endregion
35
- export { ArgSchema, ArgValues, Args, Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions, cli, define, lazy, parseArgs, resolveArgs };
21
+ export { ArgSchema, ArgValues, Args, Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions, cli, define, lazy, parseArgs, resolveArgs };
package/lib/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-STQBqMtf.js";
2
- import { DefaultTranslation } from "./context-IwpAqLSL.js";
2
+ import { DefaultTranslation } from "./context-BefYjlE-.js";
3
3
  import { define, lazy } from "./definition-DyVBFqB7.js";
4
4
  import "./renderer-BpwhGYRZ.js";
5
- import { cli } from "./cli-rwwoCHjK.js";
5
+ import { cli } from "./cli-DEuAxEk9.js";
6
6
  import { parseArgs, resolveArgs } from "args-tokens";
7
7
 
8
8
  export { DEFAULT_LOCALE, DefaultTranslation, cli, define, lazy, parseArgs, resolveArgs };
package/lib/renderer.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CommandContext } from "./types-iztBdPY6.js";
1
+ import { CommandContext } from "./types-BbgfNBFM.js";
2
2
  import { Args } from "args-tokens";
3
3
 
4
4
  //#region src/renderer/header.d.ts
@@ -205,6 +205,10 @@ interface CommandOptions<A extends Args = Args> {
205
205
  */
206
206
  translationAdapterFactory?: TranslationAdapterFactory;
207
207
  }
208
+ /**
209
+ * Command call mode.
210
+ */
211
+ type CommandCallMode = 'entry' | 'subCommand' | 'unexpected';
208
212
  /**
209
213
  * Command context.
210
214
  * Command context is the context of the command execution.
@@ -261,6 +265,11 @@ interface CommandContext<A extends Args = Args, V = ArgValues<A>> {
261
265
  * Whether the currently executing command has been executed with the sub-command name omitted.
262
266
  */
263
267
  omitted: boolean;
268
+ /**
269
+ * Command call mode.
270
+ * 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.
271
+ */
272
+ callMode: CommandCallMode;
264
273
  /**
265
274
  * Output a message.
266
275
  * If {@link CommandEnvironment.usageSilent} is true, the message is not output.
@@ -417,4 +426,4 @@ type LazyCommand<A extends Args = Args> = {
417
426
  * Define a command type.
418
427
  */
419
428
  type Commandable<A extends Args> = Command<A> | LazyCommand<A>; //#endregion
420
- export { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
429
+ export { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandCallMode, CommandContext, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
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.20.0",
4
+ "version": "0.22.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -103,12 +103,12 @@
103
103
  "eslint-plugin-vue-composable": "^1.0.0",
104
104
  "eslint-plugin-yml": "^1.18.0",
105
105
  "gh-changelogen": "^0.2.8",
106
- "gunshi019": "npm:gunshi@0.19.0",
106
+ "gunshi019": "npm:gunshi@0.21.0",
107
107
  "jsr": "^0.13.4",
108
108
  "jsr-exports-lint": "^0.2.0",
109
109
  "knip": "^5.53.0",
110
110
  "lint-staged": "^15.5.1",
111
- "messageformat": "4.0.0-10",
111
+ "messageformat": "4.0.0-11",
112
112
  "mitata": "^1.0.34",
113
113
  "pkg-pr-new": "^0.0.43",
114
114
  "prettier": "^3.5.3",