chore(core): replace commander and ts-command-line-args with cli-forge #254
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Notes
cli-forgeis a new typescript library for parsing CLI arguments that I'm working on. A main focal point for the library is nailing the typescript types and keeping good dev ergonomics. Docs can be found here: cli-forge.In this repo
stitchcurrently usescommanderandts-command-line-args, which requires a bit of casting around. This PR shows how the types improve without the need for casting.Oddities
I needed to change the
buildscript to manually specifytsconfig.jsonwhen runningtsc --build. I'm not quite sure why this would have been necessary, but without it the build would fail on a Zod error in addition to some stuff with the new CLI typings. I don't think this is oncli-forge's side, and its not on turborepo's either as the same behavior is exhibited when runningtsc --buildon the command line.Somehow the transition to
cli-forgeonly changed the overall line count by 1. I'm not sure how this is possible, but I'm not going to complain.Stitch Prior Architecture
stitchwas a CLI built using a combination ofcommander, andts-command-line-argsfor arguments parsing, withinquirerfor prompting.stitchusedcommander's built-inexecutableDirto split commands into separate files that were discovered and loaded at runtime.Transition to CLI-Forge
At a per-command level, the transition was relatively straightforward but differed based on if the command was using
commanderorts-command-line-args.Commander Commands
commanderuses a method chaining syntax to build commands, and then callsparseto execute the command.cli-forgeuses a similar method chaining syntax within its command builders, but it is slightly different.The options were translated losslessly, and command use should not change.
TS-Command-Line-Args Commands
ts-command-line-argsuses a more declarative syntax to define commands and options. It consists of an object structure that is passed to theparsefunction.cli-forgedoes not support this syntax, so the rewrite was more involved.There were a few things that looked a bit nicer in the
ts-command-line-argssyntax, but the new syntax is still quite clean. Ints-command-line-argsthetypeproperty takes a function that is used to parse the value, which is a bit more flexible than thetypeproperty incli-forge. Incli-forge, thetypeproperty is a string that maps to a built-in parser. For custom types and validation,cli-forgeoptions havevalidateandcoerceproperties that can be used.In cases where the
ts-command-line-argstype was used to map the type to a custom type, thecli-forgevalidateandcoerceproperties were used to achieve the same result.Prompting
There were a few points where a command did not define any cli level options and relied on prompting the user for input. In
stitch, this was done usinginquirerand was handled in the command file.cli-forgedoesn't currently have any built in affordances for prompting, so all prompting was moved to the handler and left as is.Issues Subcommands