@gunshi/shared 0.27.5 → 0.28.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.
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- //#region ../../node_modules/.pnpm/args-tokens@0.23.0/node_modules/args-tokens/lib/parser-C6MbpZjd.d.ts
1
+ //#region ../../node_modules/.pnpm/args-tokens@0.23.1/node_modules/args-tokens/lib/parser.d.ts
2
2
  //#region src/parser.d.ts
3
3
  /**
4
4
  * Entry point of argument parser.
@@ -55,9 +55,8 @@ interface ArgToken {
55
55
  * Parser Options.
56
56
  */
57
57
  //#endregion
58
- //#region ../../node_modules/.pnpm/args-tokens@0.23.0/node_modules/args-tokens/lib/resolver-D64nGlCD.d.ts
58
+ //#region ../../node_modules/.pnpm/args-tokens@0.23.1/node_modules/args-tokens/lib/resolver.d.ts
59
59
  //#region src/resolver.d.ts
60
-
61
60
  /**
62
61
  * An argument schema definition for command-line argument parsing.
63
62
  *
@@ -504,7 +503,7 @@ type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -r
504
503
  *
505
504
  * @internal
506
505
  */
507
- type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K$1 extends keyof ArgSchema> = { [Arg in keyof A as A[Arg][K$1] extends {} ? Arg : never]: V[Arg] };
506
+ type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K extends keyof ArgSchema> = { [Arg in keyof A as A[Arg][K] extends {} ? Arg : never]: V[Arg] };
508
507
  /**
509
508
  * Filters positional arguments from the argument schema.
510
509
  *
@@ -582,7 +581,7 @@ type ExtendContext = Record<string, unknown>;
582
581
  *
583
582
  * @since v0.27.0
584
583
  */
585
- interface GunshiParams<P$1 extends {
584
+ interface GunshiParams<P extends {
586
585
  args?: Args;
587
586
  extensions?: ExtendContext;
588
587
  } = {
@@ -592,13 +591,13 @@ interface GunshiParams<P$1 extends {
592
591
  /**
593
592
  * Command argument definitions.
594
593
  */
595
- args: P$1 extends {
594
+ args: P extends {
596
595
  args: infer A extends Args;
597
596
  } ? A : Args;
598
597
  /**
599
598
  * Command context extensions.
600
599
  */
601
- extensions: P$1 extends {
600
+ extensions: P extends {
602
601
  extensions: infer E extends ExtendContext;
603
602
  } ? E : {};
604
603
  }
@@ -827,6 +826,15 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
827
826
  * 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.
828
827
  */
829
828
  callMode: CommandCallMode;
829
+ /**
830
+ * The path of nested sub-commands that were resolved to reach the current command.
831
+ *
832
+ * For example, if the user runs `git remote add`, `commandPath` would be `['remote', 'add']`.
833
+ * For the entry command, this is an empty array.
834
+ *
835
+ * @since v0.28.0
836
+ */
837
+ commandPath: string[];
830
838
  /**
831
839
  * Whether to convert the camel-case style argument name to kebab-case.
832
840
  * This context value is set from {@linkcode Command.toKebab} option.
@@ -939,6 +947,15 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
939
947
  * @since v0.27.0
940
948
  */
941
949
  rendering?: RenderingOptions<G>;
950
+ /**
951
+ * Nested sub-commands for this command.
952
+ *
953
+ * Allows building command trees like `git remote add`.
954
+ * Each key is the sub-command name, and the value is a command or lazy command.
955
+ *
956
+ * @since v0.28.0
957
+ */
958
+ subCommands?: Record<string, SubCommandable> | Map<string, SubCommandable>;
942
959
  }
943
960
  /**
944
961
  * Lazy command interface.
@@ -966,6 +983,67 @@ type LazyCommand<G extends GunshiParamsConstraint = DefaultGunshiParams, D exten
966
983
  * @typeParam G - A type extending {@linkcode GunshiParams} to specify the shape of command.
967
984
  */
968
985
  type Commandable<G extends GunshiParamsConstraint = DefaultGunshiParams> = Command<G> | LazyCommand<G, {}>;
986
+ /**
987
+ * Sub-command entry type for use in subCommands.
988
+ *
989
+ * This type uses a loose structural match to bypass TypeScript's contravariance issues
990
+ * with function parameters, allowing any Command or LazyCommand to be used as a sub-command.
991
+ *
992
+ * @since v0.27.1
993
+ */
994
+ interface SubCommandable {
995
+ /**
996
+ * see {@link Command.name}
997
+ */
998
+ name?: string;
999
+ /**
1000
+ * see {@link Command.description}
1001
+ */
1002
+ description?: string;
1003
+ /**
1004
+ * see {@link Command.args}
1005
+ */
1006
+ args?: Args | Record<string, any>;
1007
+ /**
1008
+ * see {@link Command.examples}
1009
+ */
1010
+ examples?: string | ((...args: any[]) => any);
1011
+ /**
1012
+ * see {@link Command.run}
1013
+ */
1014
+ run?: (...args: any[]) => any;
1015
+ /**
1016
+ * see {@link Command.toKebab}
1017
+ */
1018
+ toKebab?: boolean;
1019
+ /**
1020
+ * see {@link Command.internal}
1021
+ */
1022
+ internal?: boolean;
1023
+ /**
1024
+ * see {@link Command.entry}
1025
+ */
1026
+ entry?: boolean;
1027
+ /**
1028
+ * see {@link Command.rendering}
1029
+ */
1030
+ rendering?: any;
1031
+ /**
1032
+ * see {@link LazyCommand.commandName}
1033
+ */
1034
+ commandName?: string;
1035
+ /**
1036
+ * Nested sub-commands for this command.
1037
+ *
1038
+ * @see {@link Command.subCommands}
1039
+ * @since v0.28.0
1040
+ */
1041
+ subCommands?: Record<string, any> | Map<string, any>;
1042
+ /**
1043
+ * Index signature to allow additional properties
1044
+ */
1045
+ [key: string]: any;
1046
+ }
969
1047
  /**
970
1048
  * Command examples fetcher.
971
1049
  *
@@ -985,7 +1063,7 @@ type CommandExamplesFetcher<G extends GunshiParamsConstraint = DefaultGunshiPara
985
1063
  */
986
1064
  type CommandRunner<G extends GunshiParamsConstraint = DefaultGunshiParams> = (ctx: Readonly<CommandContext<G>>) => Awaitable<string | void>;
987
1065
  //#endregion
988
- //#region ../../node_modules/.pnpm/args-tokens@0.23.0/node_modules/args-tokens/lib/utils.d.ts
1066
+ //#region ../../node_modules/.pnpm/args-tokens@0.23.1/node_modules/args-tokens/lib/utils.d.ts
989
1067
  //#region src/utils.d.ts
990
1068
  /**
991
1069
  * Entry point of utils.
@@ -1037,6 +1115,13 @@ declare function create<T>(obj?: object | null): T;
1037
1115
  * @param args - Arguments to log
1038
1116
  */
1039
1117
  declare function log(...args: unknown[]): void;
1118
+ /**
1119
+ * Get the sub-commands of a command as a normalized Map.
1120
+ *
1121
+ * @param cmd - A command or lazy command
1122
+ * @returns A Map of sub-commands, or undefined if the command has no sub-commands.
1123
+ */
1124
+ declare function getCommandSubCommands<G extends GunshiParamsConstraint = DefaultGunshiParams>(cmd: Commandable<G> | Command<G> | LazyCommand<G>): Map<string, Command<G> | LazyCommand<G>> | undefined;
1040
1125
  /**
1041
1126
  * Deep freeze an object, making it immutable.
1042
1127
  *
@@ -1111,9 +1196,9 @@ type CommandBuiltinKeys = GenerateNamespacedKey<BuiltinResourceKeys>;
1111
1196
  * Command i18n option keys.
1112
1197
  * The command i18n option keys are used by the i18n plugin for translation.
1113
1198
  */
1114
- type CommandArgKeys<A extends Args, C = {}, K$1 extends string = GenerateNamespacedKey<Extract<KeyOfArgs<RemovedIndex<A>>, string>, typeof ARG_PREFIX>> = C extends {
1199
+ type CommandArgKeys<A extends Args, C = {}, K extends string = GenerateNamespacedKey<Extract<KeyOfArgs<RemovedIndex<A>>, string>, typeof ARG_PREFIX>> = C extends {
1115
1200
  name: infer N;
1116
- } ? (N extends string ? GenerateNamespacedKey<K$1, N> : K$1) : K$1;
1201
+ } ? (N extends string ? GenerateNamespacedKey<K, N> : K) : K;
1117
1202
  /**
1118
1203
  * Resolve translation keys for command context.
1119
1204
  */
@@ -1131,8 +1216,8 @@ interface Translation<A extends Args, C = {},
1131
1216
  // for CommandContext
1132
1217
  E extends Record<string, string> = {},
1133
1218
  // for extended resources
1134
- K$1 = ResolveTranslationKeys<A, C, E>> {
1135
- (key: K$1, values?: Record<string, unknown>): string;
1219
+ K = ResolveTranslationKeys<A, C, E>> {
1220
+ (key: K, values?: Record<string, unknown>): string;
1136
1221
  }
1137
1222
  //#endregion
1138
1223
  //#region src/localization.d.ts
@@ -1146,7 +1231,7 @@ K$1 = ResolveTranslationKeys<A, C, E>> {
1146
1231
  interface Localization<A extends Args, C = {},
1147
1232
  // for CommandContext
1148
1233
  E extends Record<string, string> = {}> {
1149
- <K$1 = ResolveTranslationKeys<A, C, E>>(key: K$1, values?: Record<string, unknown>): Promise<string>;
1234
+ <K = ResolveTranslationKeys<A, C, E>>(key: K, values?: Record<string, unknown>): Promise<string>;
1150
1235
  }
1151
1236
  /**
1152
1237
  * Create a localizable function for a command.
@@ -1166,7 +1251,7 @@ declare function localizable<A extends Args, C = {},
1166
1251
  // for CommandContext
1167
1252
  E extends Record<string, string> = {},
1168
1253
  // for extended resources
1169
- K$1 = ResolveTranslationKeys<A, C, E>>(ctx: CommandContext, cmd: Command, translate?: Translation<A, C, E, K$1>): Localization<A, C, E>;
1254
+ K = ResolveTranslationKeys<A, C, E>>(ctx: CommandContext, cmd: Command, translate?: Translation<A, C, E, K>): Localization<A, C, E>;
1170
1255
  //#endregion
1171
1256
  //#region ../resources/locales/en-US.d.ts
1172
1257
  declare let COMMAND: string;
@@ -1197,7 +1282,7 @@ declare namespace __json_default_export {
1197
1282
  * @param key - The built-in key to resolve.
1198
1283
  * @returns Prefixed built-in key.
1199
1284
  */
1200
- declare function resolveBuiltInKey<K$1 extends string = CommandBuiltinResourceKeys>(key: K$1): GenerateNamespacedKey<K$1>;
1285
+ declare function resolveBuiltInKey<K extends string = CommandBuiltinResourceKeys>(key: K): GenerateNamespacedKey<K>;
1201
1286
  /**
1202
1287
  * Resolve a namespaced key for argument resources.
1203
1288
  *
@@ -1210,7 +1295,7 @@ declare function resolveBuiltInKey<K$1 extends string = CommandBuiltinResourceKe
1210
1295
  * @param name - The command name.
1211
1296
  * @returns Prefixed argument key.
1212
1297
  */
1213
- declare function resolveArgKey<A extends Args = DefaultGunshiParams['args'], K$1 extends string = KeyOfArgs<RemovedIndex<A>>>(key: K$1, name?: string): string;
1298
+ declare function resolveArgKey<A extends Args = DefaultGunshiParams['args'], K extends string = KeyOfArgs<RemovedIndex<A>>>(key: K, name?: string): string;
1214
1299
  /**
1215
1300
  * Resolve a namespaced key for non-built-in resources.
1216
1301
  *
@@ -1222,7 +1307,7 @@ declare function resolveArgKey<A extends Args = DefaultGunshiParams['args'], K$1
1222
1307
  * @param name - The command name.
1223
1308
  * @returns Prefixed non-built-in key.
1224
1309
  */
1225
- declare function resolveKey<T extends Record<string, string> = {}, K$1 extends string = (keyof T extends string ? keyof T : string)>(key: K$1, name?: string): string;
1310
+ declare function resolveKey<T extends Record<string, string> = {}, K extends string = (keyof T extends string ? keyof T : string)>(key: K, name?: string): string;
1226
1311
  /**
1227
1312
  * Resolve command examples.
1228
1313
  *
@@ -1241,7 +1326,7 @@ declare function resolveExamples<G extends GunshiParamsConstraint = DefaultGunsh
1241
1326
  * @param id - A plugin id to generate a namespaced key.
1242
1327
  * @returns A namespaced key for the plugin.
1243
1328
  */
1244
- declare function namespacedId<K$1 extends string>(id: K$1): GenerateNamespacedKey<K$1, typeof PLUGIN_PREFIX>;
1329
+ declare function namespacedId<K extends string>(id: K): GenerateNamespacedKey<K, typeof PLUGIN_PREFIX>;
1245
1330
  /**
1246
1331
  * Generate a short and long option pair for command arguments.
1247
1332
  *
@@ -1252,4 +1337,4 @@ declare function namespacedId<K$1 extends string>(id: K$1): GenerateNamespacedKe
1252
1337
  */
1253
1338
  declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKebab?: boolean): string;
1254
1339
  //#endregion
1255
- export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, DeepWriteable, __json_default_export as DefaultResource, GenerateNamespacedKey, KeyOfArgs, Localization, PLUGIN_PREFIX, RemovedIndex, ResolveTranslationKeys, Translation, create, deepFreeze, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
1340
+ export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, BuiltinResourceKeys, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, DeepWriteable, __json_default_export as DefaultResource, GenerateNamespacedKey, KeyOfArgs, Localization, PLUGIN_PREFIX, RemovedIndex, ResolveTranslationKeys, Translation, create, deepFreeze, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- //#region ../../node_modules/.pnpm/args-tokens@0.23.0/node_modules/args-tokens/lib/utils-1LQrGCWG.js
1
+ //#region ../../node_modules/.pnpm/args-tokens@0.23.1/node_modules/args-tokens/lib/utils-CxvkckUD.js
2
2
  /**
3
3
  * Entry point of utils.
4
4
  *
@@ -48,7 +48,8 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
48
48
  args: cmd.args,
49
49
  examples: cmd.examples,
50
50
  internal: cmd.internal,
51
- entry: cmd.entry
51
+ entry: cmd.entry,
52
+ subCommands: cmd.subCommands
52
53
  };
53
54
  if ("resource" in cmd && cmd.resource) baseCommand.resource = cmd.resource;
54
55
  command = Object.assign(create(), baseCommand);
@@ -64,6 +65,7 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
64
65
  command.examples = loaded.examples;
65
66
  command.internal = loaded.internal;
66
67
  command.entry = loaded.entry;
68
+ command.subCommands = loaded.subCommands || cmd.subCommands;
67
69
  if ("resource" in loaded && loaded.resource) command.resource = loaded.resource;
68
70
  } else throw new TypeError(`Cannot resolve command: ${cmd.name || name}`);
69
71
  }
@@ -89,6 +91,24 @@ function log(...args) {
89
91
  console.log(...args);
90
92
  }
91
93
  /**
94
+ * Get the sub-commands of a command as a normalized Map.
95
+ *
96
+ * @param cmd - A command or lazy command
97
+ * @returns A Map of sub-commands, or undefined if the command has no sub-commands.
98
+ */
99
+ function getCommandSubCommands(cmd) {
100
+ const subCommands = isLazyCommand(cmd) ? cmd.subCommands : typeof cmd === "object" ? cmd.subCommands : void 0;
101
+ if (!subCommands) return;
102
+ if (subCommands instanceof Map) return subCommands.size > 0 ? subCommands : void 0;
103
+ if (typeof subCommands === "object") {
104
+ const entries = Object.entries(subCommands);
105
+ if (entries.length === 0) return;
106
+ const map = /* @__PURE__ */ new Map();
107
+ for (const [name, cmd] of entries) map.set(name, cmd);
108
+ return map;
109
+ }
110
+ }
111
+ /**
92
112
  * Deep freeze an object, making it immutable.
93
113
  *
94
114
  * @param obj - The object to freeze
@@ -292,4 +312,4 @@ function localizable(ctx, cmd, translate) {
292
312
  }
293
313
 
294
314
  //#endregion
295
- export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, en_US_default as DefaultResource, PLUGIN_PREFIX, create, deepFreeze, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
315
+ export { ARG_NEGATABLE_PREFIX, ARG_PREFIX, ARG_PREFIX_AND_KEY_SEPARATOR, BUILD_IN_PREFIX_AND_KEY_SEPARATOR, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMON_ARGS, en_US_default as DefaultResource, PLUGIN_PREFIX, create, deepFreeze, getCommandSubCommands, isLazyCommand, kebabnize, localizable, log, makeShortLongOptionPair, namespacedId, resolveArgKey, resolveBuiltInKey, resolveExamples, resolveKey, resolveLazyCommand };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/shared",
3
3
  "description": "shared utils for gunshi",
4
- "version": "0.27.5",
4
+ "version": "0.28.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -55,8 +55,8 @@
55
55
  "jsr-exports-lint": "^0.4.1",
56
56
  "publint": "^0.3.16",
57
57
  "tsdown": "0.15.12",
58
- "@gunshi/resources": "0.27.5",
59
- "gunshi": "0.27.5"
58
+ "@gunshi/resources": "0.28.0",
59
+ "gunshi": "0.28.0"
60
60
  },
61
61
  "scripts": {
62
62
  "build": "tsdown",