Repository navigation
Conversation
…pec §4) Adds serveSchemaMetaCommand, which serves the reserved __schema meta-command by intercepting argv before Commander parsing instead of registering a hidden subcommand: - no --help footprint and no command-tree mutation; - single-command (default-action) programs keep their invocation shape, which subcommand registration would change; - the emitted document always lists __schema in reservedMetaCommands, merged with caller-declared entries. Also adds writeSchemaSidecar for the <binary>.cli-schema.json sidecar mechanism, and documents both in the package README.
Mpdreamz
left a comment
There was a problem hiding this comment.
Nice work — clean design, great test coverage, and the argv-interception rationale is well-articulated. A few small thoughts, none blocking:
-
Return type clarity:
serveSchemaMetaCommandreturnsboolean, but with the realprocess.exitthereturn trueis never reached. Might be worth a brief JSDoc note that the return value is only meaningful when a customexitis injected (callers in the wild will just doserveSchemaMetaCommand(…); program.parse()linearly anyway). -
Sidecar directory assumption:
writeSchemaSidecarwill throw if the parent directory doesn't exist — totally reasonable, just might be nice to mention in the JSDoc that the caller is responsible for ensuring the path is writable. -
Trailing args after
__schema: the current behaviour (ignore them, serve the schema regardless) seems right to me — spec §4.1 doesn't mandate erroring on extras. A one-line comment in the code noting "trailing arguments are intentionally ignored" would make that decision visible to future readers. -
reservedMetaCommandsordering: the Set-based merge naturally gives caller entries first, then__schema— works well. Just flagging that the test asserts on that ordering, so if it ever needs to be sorted for spec reasons the test will catch it. 👍
Really like the injectable write/exit/argv pattern — makes the tests a joy to read.
I reviewed this with the help of AI.
- serveSchemaMetaCommand: JSDoc note that the boolean return is only observable when a custom `exit` is injected (real process.exit never returns). - serveSchemaMetaCommand: inline comment that trailing args after `__schema` are intentionally ignored. - writeSchemaSidecar: JSDoc note that the caller must ensure the parent directory exists and is writable.
|
@Mpdreamz thanks for the thorough review — I've pushed 390b096 addressing the three doc nits:
I left the One small ask: the two CI runs on this PR are in |
What
@cli-schema/commanderbuilds a valid CLI Schema document from a live command tree, but until now the CLI itself couldn't be discovered — spec §4 defines two mechanisms and the package implemented neither at runtime:mytool __schemaprints the document to stdout, exit 0<binary>.cli-schema.jsonshipped next to the binaryThis PR adds both:
serveSchemaMetaCommand(root, options)— serves__schemaby intercepting argv before Commander parsing, and guarantees__schemaappears in the emitted document'sreservedMetaCommands(merged with caller-declared entries).writeSchemaSidecar(schema, binaryPath)— writes<binary>.cli-schema.json, intended for install/release scripts.Why argv interception instead of a hidden subcommand
The README currently documents
program.command('__schema', { hidden: true })as the wiring recipe. That works for multi-command programs but has three sharp edges, all avoided by interceptingargv[2] === '__schema'beforeprogram.parse():.action()only) starts treating bare arguments as unknown-command errors once any subcommand exists — registering__schemachanges the CLI's invocation shape. (Same issue surfaced in the Python/Typer integration — see Proposal: official Python Typer/Click exporter SDK cli-schema#6.)--help/ tree footprint by construction — the meta-command never enters the command tree, rather than entering it and being hidden after the fact. Spec §4.1: implementations SHOULD NOT treat__schemaas a subcommand visible in help.reservedMetaCommandscan't drift — the helper injects__schemainto the emitted document automatically; the manual recipe relies on the caller remembering to declare it.The hidden-subcommand pattern is still mentioned in the README as a helper-free alternative, with the caveats noted.
Usage
argv,write, andexitare injectable for tests; defaults areprocess.argv,process.stdout.write,process.exit.Verification
npm run build,npm test(47 pass / 0 fail across workspaces),npm run lint— all cleanpackages/commander/test/discovery.test.tscover: spec-valid output (validated against the meta-schema via@cli-schema/spec), exit code 0, no-op on normal invocations, untouched command tree / absent from--help, default-action program shape preserved,reservedMetaCommandsmerging, and sidecar file contents.