Skip to content

refactor: use yargs for the cli #791

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

Merged
merged 19 commits into from
Jul 23, 2025
Merged

refactor: use yargs for the cli #791

merged 19 commits into from
Jul 23, 2025

Conversation

james-elicx
Copy link
Collaborator

@james-elicx james-elicx commented Jul 16, 2025

Changes

  • Replaces our args parsing with yargs (out of the box commands, optionals, help menus, etc.)
  • Removes output arg as it was never used.
  • Moves the setup part of the CLI to a reusable function called in each command, and commands now just accept args as their param.
  • Moves wrangler config retrieval to the setup of the CLI.
  • Reads the compiled OpenNext config in non-build commands.
  • The flags --env and --config become proper first class citizens.

Replaces #790

Commands Tested

  • build
  • preview
  • deploy
  • upload
  • populateCache

Copy link

changeset-bot bot commented Jul 16, 2025

🦋 Changeset detected

Latest commit: d02a59e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@james-elicx james-elicx linked an issue Jul 16, 2025 that may be closed by this pull request
Closed
Copy link

pkg-pr-new bot commented Jul 16, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@791

commit: 50dd17e

@james-elicx james-elicx marked this pull request as ready for review July 16, 2025 07:57
@james-elicx james-elicx requested a review from vicb July 16, 2025 07:57
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for the PR.

Does it still work as expected when there is a --?

I'll take one more look tomorrow

@james-elicx
Copy link
Collaborator Author

Does it still work as expected when there is a --?

Unknown args and args after -- will be added to the array of args to pass to wrangler.

If --env or --config are supplied in the --, they won't be picked up by OpenNext but will be picked up by the Wrangler CLI. Because I'm changing them to be first class citizens, we're no longer doing the searching through argv for them at any position, so it wouldn't extract them if they're after --.

@vicb
Copy link
Contributor

vicb commented Jul 20, 2025

If --env or --config are supplied in the --, they won't be picked up by OpenNext but will be picked up by the Wrangler CLI. Because I'm changing them to be first class citizens, we're no longer doing the searching through argv for them at any position, so it wouldn't extract them if they're after --.

Is it what we want?
IIUC opennextjs-cloudflare deploy -- --env=... would populate i.e. populate the cache and run wrangler deploy for different envs?

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
I think more comments might help maintaining this code in the future

@james-elicx
Copy link
Collaborator Author

If --env or --config are supplied in the --, they won't be picked up by OpenNext but will be picked up by the Wrangler CLI. Because I'm changing them to be first class citizens, we're no longer doing the searching through argv for them at any position, so it wouldn't extract them if they're after --.

Is it what we want? IIUC opennextjs-cloudflare deploy -- --env=... would populate i.e. populate the cache and run wrangler deploy for different envs?

Yep, that's true, although people were complaining when we wanted people to use -- instead of args being first-class citizens...

What would you think of us throwing an error when people pass --env or --config after the --? I really wanted to make the flag a first-class citizen in the CLI because we use it like one for things like cache population. It's not normal for CLIs to parse flags after the -- and use them internally, which is why I was trying to get away from doing that.

@james-elicx james-elicx requested a review from vicb July 21, 2025 19:14

const nextAppDir = process.cwd();
export function runCommand() {
let y = yargs(process.argv.slice(2))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we filter out -- here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything after -- are considered non-option arguments and put into args._ by yargs

@vicb
Copy link
Contributor

vicb commented Jul 22, 2025

I took a deeper look and have some comments:

First I really like the changes, I fell like the CLI is much nice now, thanks for working on this.

I do have a few comments:

  1. --

We use to recommend opennextjs-cloudflare deploy -- --env=prod

IIUC this command will now (maybe before also?) use a different env for populating the cache than for deploying.

  1. yargs

I see

image image

Would be nice to ba able to solve this

  1. Open Next config / setupCLI

IIUC setupCLI is here to factor some code. It can retrieve or compile the config.

I wouldn't mind more duplication in each of the command to make the code easier to follow/extend

i.e. each command would:

printHeader();

compileConfig() or retieveCompiledConfig();

readWranglerConfig();

Each of this function could be helpers (i.e. printHeader(); could print the helper and show the windows warning, ...)

Maybe one thing we could do then is to move compileConfig() back to build.ts because it really belongs there.

Thinking forward if we add new commands, we might have to add options to setupCLI() and it might makes code less readable.

@james-elicx
Copy link
Collaborator Author

I took a deeper look and have some comments:

First I really like the changes, I fell like the CLI is much nice now, thanks for working on this.

I do have a few comments:

  1. --

We use to recommend opennextjs-cloudflare deploy -- --env=prod

IIUC this command will now (maybe before also?) use a different env for populating the cache than for deploying.

So would you like to preserve the old behaviour, or throw an error if it's used after the --?

  1. yargs

I see

image image
Would be nice to ba able to solve this

I don't get this - that looks like the dependencies aren't installed.

  1. Open Next config / setupCLI

IIUC setupCLI is here to factor some code. It can retrieve or compile the config.

I wouldn't mind more duplication in each of the command to make the code easier to follow/extend

i.e. each command would:

printHeader();

compileConfig() or retieveCompiledConfig();

readWranglerConfig();

Each of this function could be helpers (i.e. printHeader(); could print the helper and show the windows warning, ...)

Maybe one thing we could do then is to move compileConfig() back to build.ts because it really belongs there.

Thinking forward if we add new commands, we might have to add options to setupCLI() and it might makes code less readable.

Sure, I'll change it.

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for this PR!

Great code and nice to have some help on the CLI now!

One nit about an error message when the config is not found.

@james-elicx james-elicx merged commit b158e8b into main Jul 23, 2025
7 checks passed
@james-elicx james-elicx deleted the james/yargs branch July 23, 2025 14:04
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.

CLI
2 participants