@gunshi/shared 1.0.0-rc.1 → 1.0.0-rc.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.
package/lib/index.d.ts CHANGED
@@ -871,7 +871,8 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
871
871
  * The entry command of the CLI, marked with `entry: true`.
872
872
  *
873
873
  * It is available whether or not the CLI has sub-commands, while {@linkcode CommandEnvironment.subCommands}
874
- * includes the entry command only when the sub-commands are given with {@linkcode CliOptions.subCommands}.
874
+ * includes the entry command only when the CLI has sub-commands, given with {@linkcode CliOptions.subCommands}
875
+ * or with {@linkcode Command.subCommands} of the entry command.
875
876
  * It is not frozen, like the commands in {@linkcode CommandEnvironment.subCommands}.
876
877
  */
877
878
  entryCommand?: Command<any> | LazyCommand<any>;
@@ -1056,9 +1057,11 @@ interface RenderingOptions<G extends GunshiParamsConstraint = DefaultGunshiParam
1056
1057
  usage?: ((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null;
1057
1058
  /**
1058
1059
  * Validation errors rendering configuration
1059
- * - `null`: Disable rendering
1060
+ * - `null`: Disable rendering. `cli()` rejects with the validation errors, for the caller to report them
1060
1061
  * - `function`: Use custom renderer
1061
1062
  * - `undefined` (when omitted): Use default renderer
1063
+ *
1064
+ * See {@linkcode CliOptions.renderValidationErrors} for where the rendered message goes.
1062
1065
  */
1063
1066
  validationErrors?: ((ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>) | null;
1064
1067
  }
@@ -1123,10 +1126,13 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
1123
1126
  *
1124
1127
  * Allows building command trees like `git remote add`.
1125
1128
  * Each key is the sub-command name, and the value is a command or lazy command.
1129
+ * An array of commands is keyed by the names of the commands.
1130
+ *
1131
+ * On the entry command, these are the top-level sub-commands when {@linkcode CliOptions.subCommands} is not given.
1126
1132
  *
1127
1133
  * @since v0.28.0
1128
1134
  */
1129
- subCommands?: Record<string, SubCommandable> | Map<string, SubCommandable>;
1135
+ subCommands?: Record<string, SubCommandable> | Map<string, SubCommandable> | readonly SubCommandable[];
1130
1136
  }
1131
1137
  /**
1132
1138
  * Lazy command interface.
@@ -1209,7 +1215,7 @@ interface SubCommandable {
1209
1215
  * @see {@link Command.subCommands}
1210
1216
  * @since v0.28.0
1211
1217
  */
1212
- subCommands?: Record<string, any> | Map<string, any>;
1218
+ subCommands?: Record<string, any> | Map<string, any> | readonly any[];
1213
1219
  /**
1214
1220
  * Index signature to allow additional properties
1215
1221
  */
@@ -1294,8 +1300,11 @@ declare function log(...args: unknown[]): void;
1294
1300
  /**
1295
1301
  * Get the sub-commands of a command as a normalized Map.
1296
1302
  *
1303
+ * An array of sub-commands is keyed by the names of the commands (`commandName` for a lazy command).
1304
+ *
1297
1305
  * @param cmd - A command or lazy command
1298
1306
  * @returns A Map of sub-commands, or undefined if the command has no sub-commands.
1307
+ * @throws {TypeError} If a command in an array has no name, or two commands in an array have the same name.
1299
1308
  */
1300
1309
  declare function getCommandSubCommands<G extends GunshiParamsConstraint = DefaultGunshiParams>(cmd: Commandable<G> | Command<G> | LazyCommand<G>): Map<string, Command<G> | LazyCommand<G>> | undefined;
1301
1310
  /**
package/lib/index.js CHANGED
@@ -111,13 +111,27 @@ function log(...args) {
111
111
  /**
112
112
  * Get the sub-commands of a command as a normalized Map.
113
113
  *
114
+ * An array of sub-commands is keyed by the names of the commands (`commandName` for a lazy command).
115
+ *
114
116
  * @param cmd - A command or lazy command
115
117
  * @returns A Map of sub-commands, or undefined if the command has no sub-commands.
118
+ * @throws {TypeError} If a command in an array has no name, or two commands in an array have the same name.
116
119
  */
117
120
  function getCommandSubCommands(cmd) {
118
121
  const subCommands = isLazyCommand(cmd) ? cmd.subCommands : typeof cmd === "object" ? cmd.subCommands : void 0;
119
122
  if (!subCommands) return;
120
123
  if (subCommands instanceof Map) return subCommands.size > 0 ? subCommands : void 0;
124
+ if (Array.isArray(subCommands)) {
125
+ if (subCommands.length === 0) return;
126
+ const map = /* @__PURE__ */ new Map();
127
+ for (const sub of subCommands) {
128
+ const name = isLazyCommand(sub) ? sub.commandName : sub?.name;
129
+ if (typeof name !== "string" || name === "") throw new TypeError(`Sub-command name must be a non-empty string in an array of 'subCommands'`);
130
+ if (map.has(name)) throw new TypeError(`Sub-command '${name}' is given twice in an array of 'subCommands'`);
131
+ map.set(name, sub);
132
+ }
133
+ return map;
134
+ }
121
135
  if (typeof subCommands === "object") {
122
136
  const entries = Object.entries(subCommands);
123
137
  if (entries.length === 0) return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/shared",
3
3
  "description": "shared utils for gunshi",
4
- "version": "1.0.0-rc.1",
4
+ "version": "1.0.0-rc.3",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -50,9 +50,9 @@
50
50
  }
51
51
  },
52
52
  "devDependencies": {
53
- "@gunshi/resources": "1.0.0-rc.1",
53
+ "@gunshi/resources": "1.0.0-rc.3",
54
54
  "deno": "^2.7.14",
55
- "gunshi": "1.0.0-rc.1",
55
+ "gunshi": "1.0.0-rc.3",
56
56
  "jsr": "^0.14.3",
57
57
  "jsr-exports-lint": "^0.4.2",
58
58
  "publint": "^0.3.20",