@gunshi/bone 1.0.0-rc.2 → 1.0.0-rc.4

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
@@ -1034,7 +1034,8 @@ interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiPar
1034
1034
  * The entry command of the CLI, marked with `entry: true`.
1035
1035
  *
1036
1036
  * It is available whether or not the CLI has sub-commands, while {@linkcode CommandEnvironment.subCommands}
1037
- * includes the entry command only when the sub-commands are given with {@linkcode CliOptions.subCommands}.
1037
+ * includes the entry command only when the CLI has sub-commands, given with {@linkcode CliOptions.subCommands}
1038
+ * or with {@linkcode Command.subCommands} of the entry command.
1038
1039
  * It is not frozen, like the commands in {@linkcode CommandEnvironment.subCommands}.
1039
1040
  */
1040
1041
  entryCommand?: Command<any> | LazyCommand<any>;
@@ -1109,8 +1110,11 @@ interface CliOptions<G extends GunshiParamsConstraint = DefaultGunshiParams> {
1109
1110
  version?: string;
1110
1111
  /**
1111
1112
  * Sub commands.
1113
+ *
1114
+ * An object or a Map keyed by the sub-command names, or an array of commands, which are keyed by their names.
1115
+ * When this is not given, the {@linkcode Command.subCommands} of the entry command are the sub-commands.
1112
1116
  */
1113
- subCommands?: Record<string, SubCommandable> | Map<string, SubCommandable>;
1117
+ subCommands?: Record<string, SubCommandable> | Map<string, SubCommandable> | readonly SubCommandable[];
1114
1118
  /**
1115
1119
  * Left margin of the command output.
1116
1120
  */
@@ -1442,10 +1446,13 @@ interface Command<G extends GunshiParamsConstraint = DefaultGunshiParams> {
1442
1446
  *
1443
1447
  * Allows building command trees like `git remote add`.
1444
1448
  * Each key is the sub-command name, and the value is a command or lazy command.
1449
+ * An array of commands is keyed by the names of the commands.
1450
+ *
1451
+ * On the entry command, these are the top-level sub-commands when {@linkcode CliOptions.subCommands} is not given.
1445
1452
  *
1446
1453
  * @since v0.28.0
1447
1454
  */
1448
- subCommands?: Record<string, SubCommandable> | Map<string, SubCommandable>;
1455
+ subCommands?: Record<string, SubCommandable> | Map<string, SubCommandable> | readonly SubCommandable[];
1449
1456
  }
1450
1457
  /**
1451
1458
  * Lazy command interface.
@@ -1528,7 +1535,7 @@ interface SubCommandable {
1528
1535
  * @see {@link Command.subCommands}
1529
1536
  * @since v0.28.0
1530
1537
  */
1531
- subCommands?: Record<string, any> | Map<string, any>;
1538
+ subCommands?: Record<string, any> | Map<string, any> | readonly any[];
1532
1539
  /**
1533
1540
  * Index signature to allow additional properties
1534
1541
  */
@@ -1608,7 +1615,8 @@ type ValidationErrorsDecorator<G extends GunshiParamsConstraint = DefaultGunshiP
1608
1615
  //#endregion
1609
1616
  //#region ../gunshi/src/cli/bone.d.ts
1610
1617
  /**
1611
- * Run the command.
1618
+ * Run the command, without the built-in plugins: `--help` and `--version` print nothing, unless you add the plugins.
1619
+ * To have them, use `cli` from `gunshi`.
1612
1620
  *
1613
1621
  * @typeParam A - The type of {@linkcode Args | arguments} defined in the command and cli options.
1614
1622
  *
@@ -1622,7 +1630,8 @@ declare function cli<A extends Args = Args, G extends GunshiParams = {
1622
1630
  extensions: {};
1623
1631
  }>(args: string[], entry: Command<G> | CommandRunner<G> | LazyCommand<G>, options?: CliOptions<G>): Promise<string | undefined>;
1624
1632
  /**
1625
- * Run the command.
1633
+ * Run the command, without the built-in plugins: `--help` and `--version` print nothing, unless you add the plugins.
1634
+ * To have them, use `cli` from `gunshi`.
1626
1635
  *
1627
1636
  * @typeParam E - An {@linkcode ExtendContext} type to specify the shape of command and cli options.
1628
1637
  *
@@ -1636,7 +1645,8 @@ declare function cli<E extends ExtendContext = ExtendContext, G extends GunshiPa
1636
1645
  extensions: E;
1637
1646
  }>(args: string[], entry: Command<G> | CommandRunner<G> | LazyCommand<G>, options?: CliOptions<G>): Promise<string | undefined>;
1638
1647
  /**
1639
- * Run the command.
1648
+ * Run the command, without the built-in plugins: `--help` and `--version` print nothing, unless you add the plugins.
1649
+ * To have them, use `cli` from `gunshi`.
1640
1650
  *
1641
1651
  * @typeParam G - A type extending {@linkcode GunshiParams} to specify the shape of command and cli options.
1642
1652
  *
package/lib/index.js CHANGED
@@ -1264,13 +1264,27 @@ function log(...args) {
1264
1264
  /**
1265
1265
  * Get the sub-commands of a command as a normalized Map.
1266
1266
  *
1267
+ * An array of sub-commands is keyed by the names of the commands (`commandName` for a lazy command).
1268
+ *
1267
1269
  * @param cmd - A command or lazy command
1268
1270
  * @returns A Map of sub-commands, or undefined if the command has no sub-commands.
1271
+ * @throws {TypeError} If a command in an array has no name, or two commands in an array have the same name.
1269
1272
  */
1270
1273
  function getCommandSubCommands(cmd) {
1271
1274
  const subCommands = isLazyCommand(cmd) ? cmd.subCommands : typeof cmd === "object" ? cmd.subCommands : void 0;
1272
1275
  if (!subCommands) return;
1273
1276
  if (subCommands instanceof Map) return subCommands.size > 0 ? subCommands : void 0;
1277
+ if (Array.isArray(subCommands)) {
1278
+ if (subCommands.length === 0) return;
1279
+ const map = /* @__PURE__ */ new Map();
1280
+ for (const sub of subCommands) {
1281
+ const name = isLazyCommand(sub) ? sub.commandName : sub?.name;
1282
+ if (typeof name !== "string" || name === "") throw new TypeError(`Sub-command name must be a non-empty string in an array of 'subCommands'`);
1283
+ if (map.has(name)) throw new TypeError(`Sub-command '${name}' is given twice in an array of 'subCommands'`);
1284
+ map.set(name, sub);
1285
+ }
1286
+ return map;
1287
+ }
1274
1288
  if (typeof subCommands === "object") {
1275
1289
  const entries = Object.entries(subCommands);
1276
1290
  if (entries.length === 0) return;
@@ -2076,7 +2090,6 @@ function mergeValidationErrors(error, additionalErrors) {
2076
2090
  if (additionalErrors.length === 0) return error;
2077
2091
  return new AggregateError(error ? [...error.errors, ...additionalErrors] : additionalErrors, error?.message || additionalErrors[0]?.message);
2078
2092
  }
2079
- const isObject = (val) => val !== null && typeof val === "object";
2080
2093
  /**
2081
2094
  * Create the entry command that sub-commands and plugins can see.
2082
2095
  * The user-provided entry is copied, so that marking it with `entry: true` does not mutate it.
@@ -2094,10 +2107,13 @@ function createEntryCommand(entryCmd) {
2094
2107
  };
2095
2108
  }
2096
2109
  function createInitialSubCommands(options, entryCommand) {
2097
- const hasSubCommands = options.subCommands ? options.subCommands instanceof Map ? options.subCommands.size > 0 : isObject(options.subCommands) && Object.keys(options.subCommands).length > 0 : false;
2098
- const subCommands = new Map(options.subCommands instanceof Map ? options.subCommands : []);
2099
- if (!(options.subCommands instanceof Map) && isObject(options.subCommands)) for (const [name, cmd] of Object.entries(options.subCommands)) subCommands.set(name, cmd);
2100
- if (hasSubCommands && entryCommand) subCommands.set(resolveEntryName(entryCommand), entryCommand);
2110
+ const subCommands = new Map(getCommandSubCommands({ subCommands: options.subCommands }) ?? []);
2111
+ /**
2112
+ * NOTE(kazupon): `subCommands` works for any command definition, the entry included.
2113
+ * Without sub-commands in the options, the sub-commands of the entry are the top-level ones.
2114
+ */
2115
+ if (!(subCommands.size > 0) && entryCommand) for (const [name, cmd] of getCommandSubCommands(entryCommand) ?? []) subCommands.set(name, cmd);
2116
+ if (subCommands.size > 0 && entryCommand) subCommands.set(resolveEntryName(entryCommand), entryCommand);
2101
2117
  return subCommands;
2102
2118
  }
2103
2119
  function normalizeCliOptions(options, decorators, pluginContext, entryCommand) {
@@ -2532,7 +2548,8 @@ async function executeCommand(cmd, ctx, decorators) {
2532
2548
  * @license MIT
2533
2549
  */
2534
2550
  /**
2535
- * Run the command.
2551
+ * Run the command, without the built-in plugins: `--help` and `--version` print nothing, unless you add the plugins.
2552
+ * To have them, use `cli` from `gunshi`.
2536
2553
  *
2537
2554
  * @typeParam G - A type extending {@linkcode GunshiParams} to specify the shape of command and cli options.
2538
2555
  *
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/bone",
3
3
  "description": "gunshi minimum",
4
- "version": "1.0.0-rc.2",
4
+ "version": "1.0.0-rc.4",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -51,11 +51,11 @@
51
51
  }
52
52
  },
53
53
  "devDependencies": {
54
- "@gunshi/definition": "1.0.0-rc.2",
55
- "@gunshi/plugin-global": "1.0.0-rc.2",
56
- "@gunshi/plugin-renderer": "1.0.0-rc.2",
54
+ "@gunshi/definition": "1.0.0-rc.4",
55
+ "@gunshi/plugin-global": "1.0.0-rc.4",
56
+ "@gunshi/plugin-renderer": "1.0.0-rc.4",
57
57
  "deno": "^2.7.14",
58
- "gunshi": "1.0.0-rc.2",
58
+ "gunshi": "1.0.0-rc.4",
59
59
  "jsr": "^0.14.3",
60
60
  "jsr-exports-lint": "^0.4.2",
61
61
  "publint": "^0.3.20",