gunshi 0.20.0 → 0.21.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,5 +1,5 @@
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
 
@@ -15,7 +15,7 @@ async function cli(argv, entry, opts = {}) {
15
15
  const tokens = parseArgs(argv);
16
16
  const subCommand = getSubCommand(tokens);
17
17
  const resolvedCommandOptions = resolveCommandOptions(opts, entry);
18
- const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions, true);
18
+ const [name, command, callMode] = await resolveCommand(subCommand, entry, resolvedCommandOptions, true);
19
19
  if (!command) throw new Error(`Command not found: ${name || ""}`);
20
20
  const args = resolveArguments(command.args);
21
21
  const { values, positionals, rest, error } = resolveArgs(args, tokens, {
@@ -31,6 +31,7 @@ async function cli(argv, entry, opts = {}) {
31
31
  argv,
32
32
  tokens,
33
33
  omitted,
34
+ callMode,
34
35
  command,
35
36
  commandOptions: resolvedCommandOptions
36
37
  });
@@ -93,13 +94,37 @@ async function showValidationErrors(ctx, error) {
93
94
  }
94
95
  async function resolveCommand(sub, entry, options, needRunResolving = false) {
95
96
  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];
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
+ ];
98
111
  else {
99
- if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry, "", needRunResolving)];
112
+ if (options.subCommands == null || options.subCommands.size === 0) return [
113
+ resolveEntryName(entry),
114
+ await resolveLazyCommand(entry, "", needRunResolving),
115
+ "entry"
116
+ ];
100
117
  const cmd = options.subCommands?.get(sub);
101
- if (cmd == null) return [sub, void 0];
102
- return [sub, await resolveLazyCommand(cmd, sub, needRunResolving)];
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
+ ];
103
128
  }
104
129
  }
105
130
  function resolveEntryName(entry) {
@@ -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-PK_LzdQr.js";
5
5
 
6
6
  //#region src/generator.ts
7
7
  /**
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
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
@@ -32,4 +32,4 @@ declare class DefaultTranslation implements TranslationAdapter {
32
32
  }
33
33
 
34
34
  //#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 };
35
+ 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-PK_LzdQr.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.21.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"