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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

refactor: use yargs for the cli #791

wants to merge 9 commits into from

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: a160fb5

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
Copy link

pkg-pr-new bot commented Jul 16, 2025

Open in StackBlitz

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

commit: a160fb5

@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?

import { createOpenNextConfigIfNotExistent } from "../build/utils/create-config-files.js";
import { setupCLI, withWranglerOptions, withWranglerPassthroughArgs } from "./setup-cli.js";

async function buildCommand(args: {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to add some JSDoc here.

i.e. does wranglerArgs contain config and env ?

Something like:

/**
 * Implementation of the `opennextjs- clouflare command`
 *
  * @param ...
  * @returns 
*/

An explicit return type would be nice too

return buildImpl(options, config, { ...args, minify: !args.noMinify, sourceDir: baseDir }, wranglerConfig);
}

export function addBuildCommand<T extends yargs.Argv>(y: T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export function addBuildCommand<T extends yargs.Argv>(y: T) {
/**
* Add the `build` command to yargs configuration
*
* Consumes 1 positional parameter
*/
export function addBuildCommand<T extends yargs.Argv>(y: T) {


async function buildCommand(args: {
wranglerArgs: string[];
config: string | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: rename this to configPath (we also use config for the values in i.e. populateR2IncrementalCache)

const nextAppDir = process.cwd();

/**
* Setup the CLI, print necessary messages, and retrieve various options and configs.
Copy link
Contributor

Choose a reason for hiding this comment

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

setupCompiledAppCLI has the same comment.

What's the difference, when to use which ?

Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC the callback is to do some work in the middle of this function.

Another way could be do move this to the caller:

ShowHeader();
CompileConfig(); // for build
LoadConfig(); // for other commands
ExecuteCommand();

Not sure if this would make the code easier to follow?

});
}

export function withWranglerOptions<T extends yargs.Argv>(args: T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Some JSDoc would help for those 3 functions


async function runCommand(args: Arguments) {
printHeader(`Cloudflare ${args.command}`);
y = addBuildCommand(y);
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe this can return y for chaining?
Up to you

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

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