@gunshi/shared 0.27.6 → 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 +87 -1
- package/lib/index.js +22 -2
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
|
@@ -826,6 +826,15 @@ interface CommandContext<G extends GunshiParamsConstraint = DefaultGunshiParams>
|
|
|
826
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.
|
|
827
827
|
*/
|
|
828
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[];
|
|
829
838
|
/**
|
|
830
839
|
* Whether to convert the camel-case style argument name to kebab-case.
|
|
831
840
|
* This context value is set from {@linkcode Command.toKebab} option.
|
|
@@ -938,6 +947,15 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
|
|
|
938
947
|
* @since v0.27.0
|
|
939
948
|
*/
|
|
940
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>;
|
|
941
959
|
}
|
|
942
960
|
/**
|
|
943
961
|
* Lazy command interface.
|
|
@@ -965,6 +983,67 @@ type LazyCommand<G extends GunshiParamsConstraint = DefaultGunshiParams, D exten
|
|
|
965
983
|
* @typeParam G - A type extending {@linkcode GunshiParams} to specify the shape of command.
|
|
966
984
|
*/
|
|
967
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
|
+
}
|
|
968
1047
|
/**
|
|
969
1048
|
* Command examples fetcher.
|
|
970
1049
|
*
|
|
@@ -1036,6 +1115,13 @@ declare function create<T>(obj?: object | null): T;
|
|
|
1036
1115
|
* @param args - Arguments to log
|
|
1037
1116
|
*/
|
|
1038
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;
|
|
1039
1125
|
/**
|
|
1040
1126
|
* Deep freeze an object, making it immutable.
|
|
1041
1127
|
*
|
|
@@ -1251,4 +1337,4 @@ declare function namespacedId<K extends string>(id: K): GenerateNamespacedKey<K,
|
|
|
1251
1337
|
*/
|
|
1252
1338
|
declare function makeShortLongOptionPair(schema: ArgSchema, name: string, toKebab?: boolean): string;
|
|
1253
1339
|
//#endregion
|
|
1254
|
-
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
|
@@ -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.
|
|
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.
|
|
59
|
-
"gunshi": "0.
|
|
58
|
+
"@gunshi/resources": "0.28.0",
|
|
59
|
+
"gunshi": "0.28.0"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"build": "tsdown",
|