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-forge
is 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
stitch
currently usescommander
andts-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
build
script to manually specifytsconfig.json
when 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 --build
on the command line.Somehow the transition to
cli-forge
only 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
stitch
was a CLI built using a combination ofcommander
, andts-command-line-args
for arguments parsing, withinquirer
for prompting.stitch
usedcommander
's built-inexecutableDir
to 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
commander
orts-command-line-args
.Commander Commands
commander
uses a method chaining syntax to build commands, and then callsparse
to execute the command.cli-forge
uses 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-args
uses a more declarative syntax to define commands and options. It consists of an object structure that is passed to theparse
function.cli-forge
does 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-args
syntax, but the new syntax is still quite clean. Ints-command-line-args
thetype
property takes a function that is used to parse the value, which is a bit more flexible than thetype
property incli-forge
. Incli-forge
, thetype
property is a string that maps to a built-in parser. For custom types and validation,cli-forge
options havevalidate
andcoerce
properties that can be used.In cases where the
ts-command-line-args
type was used to map the type to a custom type, thecli-forge
validate
andcoerce
properties 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 usinginquirer
and was handled in the command file.cli-forge
doesn't currently have any built in affordances for prompting, so all prompting was moved to the handler and left as is.Issues Subcommands