Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): replace commander and ts-command-line-args with cli-forge #254

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

AgentEnder
Copy link
Contributor

@AgentEnder AgentEnder commented Sep 10, 2024

Discussed briefly with @adam-coster on discord - this PR isn't necessarily meant to be merged, and is definitely not tested, but wanted to demo cli-forge and use this as a chance to spot holes in the new library. Would love feedback, but don't feel bad for closing it out 😄

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 uses commander and ts-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 specify tsconfig.json when running tsc --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 on cli-forge's side, and its not on turborepo's either as the same behavior is exhibited when running tsc --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.

    00:12 $ git commit -m "chore(core): replace commander and ts-command-line-args with cli-forge"

    [cli-forge 487a79c] chore(core): replace commander and ts-command-line-args with cli-forge

    25 files changed, 839 insertions(+), 838 deletions(-)

Stitch Prior Architecture

  • stitch was a CLI built using a combination of commander, and ts-command-line-args for arguments parsing, with inquirer for prompting.
  • stitch used commander's built-in executableDir 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 or ts-command-line-args.

Commander Commands

  • commander uses a method chaining syntax to build commands, and then calls parse 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 the parse 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. In ts-command-line-args the type property takes a function that is used to parse the value, which is a bit more flexible than the type property in cli-forge. In cli-forge, the type property is a string that maps to a built-in parser. For custom types and validation, cli-forge options have validate and coerce properties that can be used.

In cases where the ts-command-line-args type was used to map the type to a custom type, the cli-forge validate and coerce 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 using inquirer 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

  • No CLI based arguments, all are prompt-based.
  • Would like to add automatic prompting to cli-forge itself at some point, but for now, just moving all prompts to the handlers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant