gunshi 0.13.0 → 0.14.3

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,4 +1,4 @@
1
- import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand } from "./renderer-DAUAIZxV.js";
1
+ import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey } from "./renderer-B2JVfepJ.js";
2
2
  import { parseArgs, resolveArgs } from "args-tokens";
3
3
 
4
4
  //#region src/locales/en-US.json
@@ -60,7 +60,7 @@ var DefaultTranslation = class {
60
60
  //#endregion
61
61
  //#region src/context.ts
62
62
  const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
63
- async function createCommandContext({ options, values, positionals, args, command, commandOptions, omitted = false }) {
63
+ async function createCommandContext({ options, values, positionals, args, tokens, command, commandOptions, omitted = false }) {
64
64
  /**
65
65
  * normailize the options schema and values, to avoid prototype pollution
66
66
  */
@@ -122,6 +122,7 @@ async function createCommandContext({ options, values, positionals, args, comman
122
122
  values,
123
123
  positionals,
124
124
  _: args,
125
+ tokens,
125
126
  log: commandOptions.usageSilent ? NOOP : log,
126
127
  loadCommands,
127
128
  translate
@@ -134,7 +135,7 @@ async function createCommandContext({ options, values, positionals, args, comman
134
135
  return [key, description];
135
136
  });
136
137
  const defaultCommandResource = loadedOptionsResources.reduce((res, [key, value]) => {
137
- res[key] = value;
138
+ res[resolveOptionKey(key)] = value;
138
139
  return res;
139
140
  }, create());
140
141
  defaultCommandResource.description = command.description || "";
@@ -181,6 +182,7 @@ async function cli(args, entry, opts = {}) {
181
182
  values,
182
183
  positionals,
183
184
  args,
185
+ tokens,
184
186
  omitted,
185
187
  command,
186
188
  commandOptions: resolvedCommandOptions
@@ -244,14 +246,17 @@ async function showValidationErrors(ctx, error) {
244
246
  async function resolveCommand(sub, entry, options) {
245
247
  const omitted = !sub;
246
248
  if (typeof entry === "function") return [void 0, { run: entry }];
247
- else if (omitted) return typeof entry === "object" ? [entry.name, await resolveLazyCommand(entry)] : [void 0, void 0];
249
+ else if (omitted) return typeof entry === "object" ? [resolveEntryName(entry), await resolveLazyCommand(entry)] : [void 0, void 0];
248
250
  else {
249
- if (options.subCommands == null) return [sub, void 0];
251
+ if (options.subCommands == null || options.subCommands.size === 0) return [resolveEntryName(entry), await resolveLazyCommand(entry)];
250
252
  const cmd = options.subCommands?.get(sub);
251
253
  if (cmd == null) return [sub, void 0];
252
254
  return [sub, await resolveLazyCommand(cmd, sub)];
253
255
  }
254
256
  }
257
+ function resolveEntryName(entry) {
258
+ return entry.name || "(anonymous)";
259
+ }
255
260
 
256
261
  //#endregion
257
262
  export { DefaultTranslation, cli };
@@ -0,0 +1,8 @@
1
+
2
+ //#region src/definition.ts
3
+ function define(definition) {
4
+ return definition;
5
+ }
6
+
7
+ //#endregion
8
+ export { define };
@@ -0,0 +1,21 @@
1
+ import { ArgOptions } from 'args-tokens';
2
+ export { ArgOptionSchema, ArgOptions, ArgValues } from 'args-tokens';
3
+ import { C as Command } from './types.d-Dw5e-IT1.js';
4
+
5
+ /**
6
+ * The entry for command deifinition.
7
+ * @example
8
+ * ```js
9
+ * import { define } from 'gunshi/definition'
10
+ * ```
11
+ * @module
12
+ */
13
+
14
+ /**
15
+ * Define a {@link Command | command} with type inference
16
+ * @param definition A {@link Command | command} definition
17
+ * @returns A {@link Command | command} definition with type inference
18
+ */
19
+ declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>;
20
+
21
+ export { define };
@@ -0,0 +1,3 @@
1
+ import { define } from "./definition-by5EpPkZ.js";
2
+
3
+ export { define };
@@ -1,5 +1,5 @@
1
1
  import { ArgOptions } from 'args-tokens';
2
- import { C as Command, a as CommandOptions } from './types.d-DqtZy3HN.js';
2
+ import { C as Command, a as CommandOptions } from './types.d-Dw5e-IT1.js';
3
3
 
4
4
  /**
5
5
  * Generate the command usage.
package/lib/generator.js CHANGED
@@ -1,5 +1,5 @@
1
- import { create } from "./renderer-DAUAIZxV.js";
2
- import { cli } from "./cli-D_4MqIMP.js";
1
+ import { create } from "./renderer-B2JVfepJ.js";
2
+ import { cli } from "./cli-BlZyQHmk.js";
3
3
 
4
4
  //#region src/generator.ts
5
5
  async function generate(command, entry, opts = {}) {
package/lib/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { ArgOptions } from 'args-tokens';
2
2
  export { ArgOptionSchema, ArgOptions, ArgValues, parseArgs, resolveArgs } from 'args-tokens';
3
- import { C as Command, b as CommandRunner, a as CommandOptions, T as TranslationAdapter, c as TranslationAdapterFactoryOptions } from './types.d-DqtZy3HN.js';
4
- export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, h as CommandContext, g as CommandEnvironment, i as CommandResource, j as CommandResourceFetcher, l as Commandable, D as DEFAULT_LOCALE, G as GenerateNamespacedKey, L as LazyCommand, k as TranslationAdapterFactory } from './types.d-DqtZy3HN.js';
3
+ import { C as Command, b as CommandRunner, a as CommandOptions, T as TranslationAdapter, c as TranslationAdapterFactoryOptions } from './types.d-Dw5e-IT1.js';
4
+ export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBuiltinResourceKeys, i as CommandContext, h as CommandEnvironment, g as CommandOptionKeys, j as CommandResource, k as CommandResourceFetcher, m as Commandable, D as DEFAULT_LOCALE, G as GenerateNamespacedKey, L as LazyCommand, R as RemovedIndex, l as TranslationAdapterFactory } from './types.d-Dw5e-IT1.js';
5
+ export { define } from './definition.js';
5
6
 
6
7
  /**
7
8
  * Run the command.
package/lib/index.js CHANGED
@@ -1,5 +1,6 @@
1
- import { DEFAULT_LOCALE } from "./renderer-DAUAIZxV.js";
2
- import { DefaultTranslation, cli } from "./cli-D_4MqIMP.js";
1
+ import { define } from "./definition-by5EpPkZ.js";
2
+ import { DEFAULT_LOCALE } from "./renderer-B2JVfepJ.js";
3
+ import { DefaultTranslation, cli } from "./cli-BlZyQHmk.js";
3
4
  import { parseArgs, resolveArgs } from "args-tokens";
4
5
 
5
- export { DEFAULT_LOCALE, DefaultTranslation, cli, parseArgs, resolveArgs };
6
+ export { DEFAULT_LOCALE, DefaultTranslation, cli, define, parseArgs, resolveArgs };
@@ -2,6 +2,7 @@
2
2
  //#region src/constants.ts
3
3
  const DEFAULT_LOCALE = "en-US";
4
4
  const BUILT_IN_PREFIX = "_";
5
+ const OPTION_PREFIX = "Option";
5
6
  const BUILT_IN_KEY_SEPARATOR = ":";
6
7
  const NOOP = () => {};
7
8
  const COMMON_OPTIONS = {
@@ -42,6 +43,9 @@ async function resolveLazyCommand(cmd, name) {
42
43
  function resolveBuiltInKey(key) {
43
44
  return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
44
45
  }
46
+ function resolveOptionKey(key) {
47
+ return `${OPTION_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
48
+ }
45
49
  function mapResourceWithBuiltinKey(resource) {
46
50
  return Object.entries(resource).reduce((acc, [key, value]) => {
47
51
  acc[resolveBuiltInKey(key)] = value;
@@ -244,7 +248,7 @@ async function generateOptionsUsage(ctx, optionsPairs) {
244
248
  const optionsMaxLength = Math.max(...Object.entries(optionsPairs).map(([_, value]) => value.length));
245
249
  const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key, _]) => ctx.options[key].type.length)) : 0;
246
250
  const usages = await Promise.all(Object.entries(optionsPairs).map(([key, value]) => {
247
- const rawDesc = ctx.translate(key);
251
+ const rawDesc = ctx.translate(resolveOptionKey(key));
248
252
  const optionsSchema = ctx.env.usageOptionType ? `[${ctx.options[key].type}] ` : "";
249
253
  const desc = `${optionsSchema ? optionsSchema.padEnd(optionSchemaMaxLength + 3) : ""}${rawDesc}`;
250
254
  const option = `${value.padEnd(optionsMaxLength + ctx.env.middleMargin)}${desc}`;
@@ -262,4 +266,4 @@ function renderValidationErrors(_ctx, error) {
262
266
  }
263
267
 
264
268
  //#endregion
265
- export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand };
269
+ export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, renderHeader, renderUsage, renderValidationErrors, resolveLazyCommand, resolveOptionKey };
@@ -1,5 +1,5 @@
1
1
  import { ArgOptions } from 'args-tokens';
2
- import { h as CommandContext } from '../types.d-DqtZy3HN.js';
2
+ import { i as CommandContext } from './types.d-Dw5e-IT1.js';
3
3
 
4
4
  /**
5
5
  * Render the header.
@@ -0,0 +1,3 @@
1
+ import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-B2JVfepJ.js";
2
+
3
+ export { renderHeader, renderUsage, renderValidationErrors };
@@ -1,10 +1,11 @@
1
- import { ArgOptions, ArgValues } from 'args-tokens';
1
+ import { ArgOptions, ArgValues, ArgToken } from 'args-tokens';
2
2
 
3
3
  /**
4
4
  * The default locale string, which format is BCP 47 language tag.
5
5
  */
6
6
  declare const DEFAULT_LOCALE = "en-US";
7
7
  declare const BUILT_IN_PREFIX = "_";
8
+ declare const OPTION_PREFIX = "Option";
8
9
  declare const BUILT_IN_KEY_SEPARATOR = ":";
9
10
  declare const NOOP: () => void;
10
11
  type CommonOptionType = {
@@ -30,6 +31,7 @@ declare const __constants_ts_COMMAND_OPTIONS_DEFAULT: typeof COMMAND_OPTIONS_DEF
30
31
  declare const __constants_ts_COMMON_OPTIONS: typeof COMMON_OPTIONS;
31
32
  declare const __constants_ts_DEFAULT_LOCALE: typeof DEFAULT_LOCALE;
32
33
  declare const __constants_ts_NOOP: typeof NOOP;
34
+ declare const __constants_ts_OPTION_PREFIX: typeof OPTION_PREFIX;
33
35
  declare namespace __constants_ts {
34
36
  export {
35
37
  __constants_ts_BUILT_IN_KEY_SEPARATOR as BUILT_IN_KEY_SEPARATOR,
@@ -39,13 +41,19 @@ declare namespace __constants_ts {
39
41
  __constants_ts_COMMON_OPTIONS as COMMON_OPTIONS,
40
42
  __constants_ts_DEFAULT_LOCALE as DEFAULT_LOCALE,
41
43
  __constants_ts_NOOP as NOOP,
44
+ __constants_ts_OPTION_PREFIX as OPTION_PREFIX,
42
45
  };
43
46
  }
44
47
 
48
+ type Awaitable<T> = T | Promise<T>;
49
+ type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K] : T[K] };
45
50
  /**
46
- * Define a promise type that can be await from T.
51
+ * Remove index signature from object or record type.
52
+ */
53
+ type RemovedIndex<T> = RemoveIndexSignature<{ [K in keyof T] : T[K] }>;
54
+ /**
55
+ * Generate a namespaced key.
47
56
  */
48
- type Awaitable<T> = T | Promise<T>;
49
57
  type GenerateNamespacedKey<
50
58
  Key extends string,
51
59
  Prefixed extends string = typeof BUILT_IN_PREFIX
@@ -64,6 +72,11 @@ type CommandBuiltinResourceKeys = (typeof __constants_ts)["COMMAND_BUILTIN_RESOU
64
72
  */
65
73
  type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinOptionsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | "description" | "examples";
66
74
  /**
75
+ * Command i18n option keys.
76
+ * The command i18n option keys are used to {@link CommandContext.translate | translate} function.
77
+ */
78
+ type CommandOptionKeys<Options extends ArgOptions> = GenerateNamespacedKey<keyof RemovedIndex<Options>, typeof OPTION_PREFIX>;
79
+ /**
67
80
  * Command environment.
68
81
  */
69
82
  interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
@@ -240,6 +253,10 @@ interface CommandContext<
240
253
  */
241
254
  _: string[];
242
255
  /**
256
+ * Argument tokens, that is parsed by `parseArgs` function.
257
+ */
258
+ tokens: ArgToken[];
259
+ /**
243
260
  * Whether the currently executing command has been executed with the sub-command name omitted.
244
261
  */
245
262
  omitted: boolean;
@@ -264,7 +281,8 @@ interface CommandContext<
264
281
  */
265
282
  translate: <
266
283
  T extends string = CommandBuiltinKeys,
267
- Key = CommandBuiltinKeys | keyof Options | T
284
+ O = CommandOptionKeys<Options>,
285
+ Key = CommandBuiltinKeys | O | T
268
286
  >(key: Key, values?: Record<string, unknown>) => string;
269
287
  }
270
288
  /**
@@ -312,7 +330,7 @@ type CommandResource<Options extends ArgOptions = ArgOptions> = {
312
330
  * Examples usage.
313
331
  */
314
332
  examples: string
315
- } & { [Option in keyof Options] : string } & {
333
+ } & { [Option in GenerateNamespacedKey<keyof RemovedIndex<Options>, typeof OPTION_PREFIX>] : string } & {
316
334
  [key: string]: string
317
335
  };
318
336
  /**
@@ -391,4 +409,4 @@ type LazyCommand<Options extends ArgOptions = ArgOptions> = () => Awaitable<Comm
391
409
  type Commandable<Options extends ArgOptions> = Command<Options> | LazyCommand<Options>;
392
410
 
393
411
  export { DEFAULT_LOCALE as D };
394
- export type { Command as C, GenerateNamespacedKey as G, LazyCommand as L, TranslationAdapter as T, CommandOptions as a, CommandRunner as b, TranslationAdapterFactoryOptions as c, CommandBuiltinOptionsKeys as d, CommandBuiltinResourceKeys as e, CommandBuiltinKeys as f, CommandEnvironment as g, CommandContext as h, CommandResource as i, CommandResourceFetcher as j, TranslationAdapterFactory as k, Commandable as l };
412
+ export type { Command as C, GenerateNamespacedKey as G, LazyCommand as L, RemovedIndex as R, TranslationAdapter as T, CommandOptions as a, CommandRunner as b, TranslationAdapterFactoryOptions as c, CommandBuiltinOptionsKeys as d, CommandBuiltinResourceKeys as e, CommandBuiltinKeys as f, CommandOptionKeys as g, CommandEnvironment as h, CommandContext as i, CommandResource as j, CommandResourceFetcher as k, TranslationAdapterFactory as l, Commandable as m };
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.13.0",
4
+ "version": "0.14.3",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -41,11 +41,17 @@
41
41
  "require": "./lib/index.js",
42
42
  "default": "./lib/index.js"
43
43
  },
44
+ "./definition": {
45
+ "types": "./lib/definition.d.ts",
46
+ "import": "./lib/definition.js",
47
+ "require": "./lib/definition.js",
48
+ "default": "./lib/definition.js"
49
+ },
44
50
  "./renderer": {
45
- "types": "./lib/renderer/index.d.ts",
46
- "import": "./lib/renderer/index.js",
47
- "require": "./lib/renderer/index.js",
48
- "default": "./lib/renderer/index.js"
51
+ "types": "./lib/renderer.d.ts",
52
+ "import": "./lib/renderer.js",
53
+ "require": "./lib/renderer.js",
54
+ "default": "./lib/renderer.js"
49
55
  },
50
56
  "./generator": {
51
57
  "types": "./lib/generator.d.ts",
@@ -53,8 +59,7 @@
53
59
  "require": "./lib/generator.js",
54
60
  "default": "./lib/generator.js"
55
61
  },
56
- "./package.json": "./package.json",
57
- "./*": "./*"
62
+ "./package.json": "./package.json"
58
63
  },
59
64
  "types": "lib/index.d.ts",
60
65
  "typesVersions": {
@@ -71,18 +76,18 @@
71
76
  "devDependencies": {
72
77
  "@eslint/markdown": "^6.3.0",
73
78
  "@intlify/core": "next",
74
- "@kazupon/eslint-config": "^0.26.1",
79
+ "@kazupon/eslint-config": "^0.27.0",
75
80
  "@kazupon/prettier-config": "^0.1.1",
76
- "@types/node": "^22.13.14",
77
- "@vitest/eslint-plugin": "^1.1.38",
81
+ "@types/node": "^22.14.0",
82
+ "@vitest/eslint-plugin": "^1.1.42",
78
83
  "bumpp": "^10.1.0",
79
- "deno": "^2.2.6",
80
- "eslint": "^9.23.0",
81
- "eslint-config-prettier": "^10.1.1",
82
- "eslint-import-resolver-typescript": "^4.2.7",
84
+ "deno": "^2.2.8",
85
+ "eslint": "^9.24.0",
86
+ "eslint-config-prettier": "^10.1.2",
87
+ "eslint-import-resolver-typescript": "^4.3.2",
83
88
  "eslint-plugin-import": "^2.31.0",
84
89
  "eslint-plugin-jsonc": "^2.20.0",
85
- "eslint-plugin-module-interop": "^0.3.0",
90
+ "eslint-plugin-module-interop": "^0.3.1",
86
91
  "eslint-plugin-promise": "^7.2.1",
87
92
  "eslint-plugin-regexp": "^2.7.0",
88
93
  "eslint-plugin-unicorn": "^58.0.0",
@@ -92,21 +97,22 @@
92
97
  "eslint-plugin-yml": "^1.17.0",
93
98
  "gh-changelogen": "^0.2.8",
94
99
  "jsr": "^0.13.4",
95
- "knip": "^5.46.3",
100
+ "knip": "^5.50.2",
96
101
  "lint-staged": "^15.5.0",
97
102
  "messageformat": "4.0.0-10",
98
103
  "pkg-pr-new": "^0.0.42",
99
104
  "prettier": "^3.5.3",
100
- "tsdown": "^0.6.10",
101
- "typedoc": "^0.28.1",
102
- "typedoc-plugin-markdown": "^4.6.0",
105
+ "publint": "^0.3.11",
106
+ "tsdown": "0.6.10",
107
+ "typedoc": "^0.28.2",
108
+ "typedoc-plugin-markdown": "^4.6.2",
103
109
  "typedoc-vitepress-theme": "^1.1.2",
104
- "typescript": "^5.8.2",
105
- "typescript-eslint": "^8.28.0",
110
+ "typescript": "^5.8.3",
111
+ "typescript-eslint": "^8.29.1",
106
112
  "vitepress": "^1.6.3",
107
- "vitepress-plugin-group-icons": "^1.3.8",
113
+ "vitepress-plugin-group-icons": "^1.4.1",
108
114
  "vitepress-plugin-llms": "^0.0.22",
109
- "vitest": "^3.0.9",
115
+ "vitest": "^3.1.1",
110
116
  "vue": "^3.5.13"
111
117
  },
112
118
  "prettier": "@kazupon/prettier-config",
@@ -1,3 +0,0 @@
1
- import { renderHeader, renderUsage, renderValidationErrors } from "../renderer-DAUAIZxV.js";
2
-
3
- export { renderHeader, renderUsage, renderValidationErrors };