@hop-top/kit 0.5.0-alpha.8 → 0.5.0-alpha.9

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.
@@ -20,6 +20,7 @@ import {
20
20
  isRunnable,
21
21
  pflagTypes,
22
22
  splitCSV,
23
+ takesNoValue,
23
24
  walkCommands,
24
25
  type Annotations,
25
26
  } from "./toolspec-internal.js";
@@ -238,11 +239,14 @@ function flagTypeAndDefault(opt: CommanderOption): [string, string] {
238
239
  const def = opt.defaultValue;
239
240
  const declared = pflagTypes.get(opt);
240
241
  if (declared) return [declared, def === undefined ? "" : String(def)];
241
- if (opt.isBoolean()) return ["bool", def === undefined ? "false" : String(def)];
242
242
  if (opt.variadic) {
243
243
  const items = def === undefined ? [] : Array.isArray(def) ? def : [def];
244
244
  return ["stringSlice", `[${items.map(String).join(",")}]`];
245
245
  }
246
+ // A negated --no-x is its own pflag bool, unset (false) by default;
247
+ // commander's default for it belongs to the x attribute, not the flag.
248
+ if (opt.negate) return ["bool", "false"];
249
+ if (takesNoValue(opt)) return ["bool", def === undefined ? "false" : String(def)];
246
250
  return ["string", def === undefined ? "" : String(def)];
247
251
  }
248
252
 
@@ -3,7 +3,9 @@ import * as fs from "fs";
3
3
  import * as os from "os";
4
4
  import * as path from "path";
5
5
  import { Command, Option } from "commander";
6
+ import { completionCommand } from "./completion";
6
7
  import {
8
+ buildManifest,
7
9
  findCommand,
8
10
  loadToolSpec,
9
11
  toolSpecFromCommand,
@@ -214,3 +216,52 @@ describe("loadToolSpec args/short/required", () => {
214
216
  }
215
217
  });
216
218
  });
219
+
220
+ describe("toolSpecFromCommand negatable flags", () => {
221
+ it("types --no-x as a bool flag named no-x, as pflag does", () => {
222
+ const program = new Command("t");
223
+ program.command("run").option("--no-color", "c").action(() => {});
224
+ expect(findCommand(toolSpecFromCommand(program), "run")!.flags).toEqual([
225
+ { name: "no-color", type: "bool", description: "c" },
226
+ ]);
227
+ });
228
+
229
+ it("types both halves of a --x/--no-x pair bool", () => {
230
+ const program = new Command("t");
231
+ program.option("--hints", "on").option("--no-hints", "off");
232
+ expect(toolSpecFromCommand(program).flags).toEqual([
233
+ { name: "hints", type: "bool", description: "on" },
234
+ { name: "no-hints", type: "bool", description: "off" },
235
+ ]);
236
+ });
237
+ });
238
+
239
+ describe("toolSpecFromCommand builtins and hidden (Go WalkCobra)", () => {
240
+ function tree(): Command {
241
+ const program = new Command("t");
242
+ program.command("real").action(() => {});
243
+ program.command("man").action(() => {});
244
+ program.command("__complete").action(() => {});
245
+ program.addCommand(completionCommand(program));
246
+ const grp = program.command("grp", { hidden: true });
247
+ grp.command("leaf").action(() => {});
248
+ return program;
249
+ }
250
+
251
+ it("skips framework builtins: help, completion and its shells, man, __complete", () => {
252
+ const spec = toolSpecFromCommand(tree());
253
+ expect(spec.commands.map((c) => c.name)).toEqual(["real"]);
254
+ expect(findCommand(spec, "bash")).toBeUndefined();
255
+ });
256
+
257
+ it("includeHidden keeps hidden commands, never builtins", () => {
258
+ const spec = toolSpecFromCommand(tree(), { includeHidden: true });
259
+ expect(spec.commands.map((c) => c.name)).toEqual(["real", "grp"]);
260
+ expect(findCommand(spec, "grp")!.children!.map((c) => c.name)).toEqual(["leaf"]);
261
+ });
262
+
263
+ it("buildManifest skips the same builtins", () => {
264
+ const paths = buildManifest(tree(), "1.1").commands.map((c) => c.path.join(" "));
265
+ expect(paths).toEqual(["t grp leaf", "t real"]);
266
+ });
267
+ });
package/src/toolspec.ts CHANGED
@@ -4,6 +4,7 @@ import type {
4
4
  Command as CommanderCommand,
5
5
  Option as CommanderOption,
6
6
  } from "commander";
7
+ import { declaredArgs, isBuiltin, takesNoValue } from "./toolspec-internal.js";
7
8
 
8
9
  // --- Types ---
9
10
 
@@ -331,47 +332,62 @@ export interface FromCommandOptions {
331
332
  annotations?: Record<string, CommandAnnotation>;
332
333
  /** Defaults to "1", the version the YAML toolspecs declare. */
333
334
  schemaVersion?: string;
335
+ /** Keep hidden commands (Go WithIncludeHidden). Hidden options stay omitted. */
336
+ includeHidden?: boolean;
334
337
  }
335
338
 
336
339
  /**
337
340
  * Build a ToolSpec from a constructed commander tree, so CLIs with
338
341
  * dynamically registered subcommands need no hand-synced YAML.
339
- * Mirrors Go's WalkCobra projection: hidden commands and options are
340
- * omitted, flag names are undashed, root options become spec.flags.
342
+ * Mirrors Go's WalkCobra projection: hidden commands (unless
343
+ * includeHidden) and hidden options are omitted, framework builtins
344
+ * (help, completion and its shells, man, __complete,
345
+ * __completeNoDesc) and kit/spec-command commands are skipped, flag
346
+ * names are undashed, root options become spec.flags.
341
347
  */
342
348
  export function toolSpecFromCommand(
343
349
  program: CommanderCommand,
344
350
  opts: FromCommandOptions = {},
345
351
  ): ToolSpec {
346
- const annotations = opts.annotations ?? {};
352
+ const ctx: WalkContext = {
353
+ annotations: opts.annotations ?? {},
354
+ includeHidden: opts.includeHidden ?? false,
355
+ };
347
356
  const spec: ToolSpec = {
348
357
  name: program.name(),
349
358
  schemaVersion: opts.schemaVersion ?? "1",
350
- commands: projectChildren(program, [], annotations),
359
+ commands: projectChildren(program, [], ctx),
351
360
  };
352
361
  const flags = projectFlags(program);
353
362
  if (flags) spec.flags = flags;
354
363
  return spec;
355
364
  }
356
365
 
366
+ interface WalkContext {
367
+ annotations: Record<string, CommandAnnotation>;
368
+ includeHidden: boolean;
369
+ }
370
+
357
371
  function projectChildren(
358
372
  cmd: CommanderCommand,
359
373
  path: string[],
360
- annotations: Record<string, CommandAnnotation>,
374
+ ctx: WalkContext,
361
375
  ): Command[] {
362
376
  // visibleCommands drops hidden subcommands but appends commander's
363
377
  // implicit help placeholder, which is not in cmd.commands.
364
378
  const visible = cmd.createHelp().visibleCommands(cmd);
365
- return visible
366
- .filter((c) => cmd.commands.includes(c))
379
+ return cmd.commands
380
+ .filter((c) => c.name() !== "")
381
+ .filter((c) => ctx.includeHidden || visible.includes(c))
382
+ .filter((c) => !isBuiltin(c, cmd))
367
383
  .filter((c) => kitAnnotations(c)["kit/spec-command"] !== "true")
368
- .map((c) => projectCommand(c, [...path, c.name()], annotations));
384
+ .map((c) => projectCommand(c, [...path, c.name()], ctx));
369
385
  }
370
386
 
371
387
  function projectCommand(
372
388
  cmd: CommanderCommand,
373
389
  path: string[],
374
- annotations: Record<string, CommandAnnotation>,
390
+ ctx: WalkContext,
375
391
  ): Command {
376
392
  const out: Command = { name: cmd.name() };
377
393
  const short = cmd.summary() || cmd.description();
@@ -380,16 +396,14 @@ function projectCommand(
380
396
  if (aliases.length > 0) out.aliases = [...aliases];
381
397
  const flags = projectFlags(cmd);
382
398
  if (flags) out.flags = flags;
383
- const args = cmd.registeredArguments.map((a): Arg =>
384
- a.required ? { name: a.name(), required: true } : { name: a.name() },
385
- );
399
+ const args: Arg[] = declaredArgs(cmd);
386
400
  if (args.length > 0) out.args = args;
387
401
  const exitCodes = splitCSV(kitAnnotations(cmd)["kit/exit-codes"]);
388
402
  if (exitCodes) out.exitCodes = exitCodes;
389
- const children = projectChildren(cmd, path, annotations);
403
+ const children = projectChildren(cmd, path, ctx);
390
404
  if (children.length > 0) out.children = children;
391
405
 
392
- const ann = annotations[path.join(" ")];
406
+ const ann = ctx.annotations[path.join(" ")];
393
407
  if (ann) {
394
408
  for (const [k, v] of Object.entries(ann)) {
395
409
  if (v !== undefined) (out as any)[k] = v;
@@ -432,7 +446,7 @@ function projectFlags(cmd: CommanderCommand): Flag[] | undefined {
432
446
 
433
447
  /** pflag-style type names, matching the Go walker's output. */
434
448
  function flagType(opt: CommanderOption): string {
435
- if (opt.isBoolean()) return "bool";
436
449
  if (opt.variadic) return "stringSlice";
450
+ if (takesNoValue(opt)) return "bool";
437
451
  return "string";
438
452
  }