gunshi 0.21.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.
@@ -4,13 +4,6 @@ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-Bp
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);
@@ -59,7 +52,10 @@ function resolveArguments(options) {
59
52
  }
60
53
  function resolveCommandOptions(options, entry) {
61
54
  const subCommands = new Map(options.subCommands);
62
- 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
+ }
63
59
  const resolvedOptions = Object.assign(create(), COMMAND_OPTIONS_DEFAULT, options, { subCommands });
64
60
  return resolvedOptions;
65
61
  }
@@ -92,40 +88,43 @@ async function showValidationErrors(ctx, error) {
92
88
  const render = ctx.env.renderValidationErrors || renderValidationErrors;
93
89
  ctx.log(await render(ctx, error));
94
90
  }
91
+ const CANNOT_RESOLVE_COMMAND = [
92
+ void 0,
93
+ void 0,
94
+ "unexpected"
95
+ ];
95
96
  async function resolveCommand(sub, entry, options, needRunResolving = false) {
96
97
  const omitted = !sub;
97
- if (typeof entry === "function") return [
98
- void 0,
99
- { run: entry },
100
- "entry"
101
- ];
102
- else if (omitted) return typeof entry === "object" ? [
103
- resolveEntryName(entry),
104
- await resolveLazyCommand(entry, "", needRunResolving),
105
- "entry"
106
- ] : [
107
- void 0,
108
- void 0,
109
- "unexpected"
110
- ];
111
- else {
112
- if (options.subCommands == null || options.subCommands.size === 0) return [
113
- resolveEntryName(entry),
98
+ async function doResolveCommand() {
99
+ if (typeof entry === "function") if ("commandName" in entry && entry.commandName) return [
100
+ entry.commandName,
114
101
  await resolveLazyCommand(entry, "", needRunResolving),
115
102
  "entry"
116
103
  ];
117
- const cmd = options.subCommands?.get(sub);
118
- if (cmd == null) return [
119
- sub,
104
+ else return [
120
105
  void 0,
121
- "unexpected"
106
+ { run: entry },
107
+ "entry"
122
108
  ];
123
- return [
124
- sub,
125
- await resolveLazyCommand(cmd, sub, needRunResolving),
126
- "subCommand"
109
+ else if (typeof entry === "object") return [
110
+ resolveEntryName(entry),
111
+ await resolveLazyCommand(entry, "", needRunResolving),
112
+ "entry"
127
113
  ];
114
+ else return CANNOT_RESOLVE_COMMAND;
128
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
+ ];
129
128
  }
130
129
  function resolveEntryName(entry) {
131
130
  return entry.name || "(anonymous)";
package/lib/generator.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { create } from "./utils-STQBqMtf.js";
2
2
  import "./context-BefYjlE-.js";
3
3
  import "./renderer-BpwhGYRZ.js";
4
- import { cli } from "./cli-PK_LzdQr.js";
4
+ import { cli } from "./cli-DEuAxEk9.js";
5
5
 
6
6
  //#region src/generator.ts
7
7
  /**
package/lib/index.d.ts CHANGED
@@ -3,22 +3,8 @@ 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
package/lib/index.js CHANGED
@@ -2,7 +2,7 @@ import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-STQBqMtf.js";
2
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-PK_LzdQr.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gunshi",
3
3
  "description": "Modern javascript command-line library",
4
- "version": "0.21.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",