Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/yellow-pears-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@opennextjs/cloudflare": patch
---

Add `--help` and `--version` to the `opennextjs-cloudflare` CLI

Improve the `opennextjs-cloudflare` CLI by:

- ensuring that unknown commands (e.g., `opennextjs-cloudflare foo`) display a clear and helpful error message
- adding a `-h`|`--help` flag to display the CLI's help message
- adding a `-v`|`--version` flag to display the package's version
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function buildCommand(
*/
export function addBuildCommand<T extends yargs.Argv>(y: T) {
return y.command(
"build",
"build [args..]",
"Build an OpenNext Cloudflare worker",
(c) =>
withWranglerOptions(c)
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu
*/
export function addDeployCommand<T extends yargs.Argv>(y: T) {
return y.command(
"deploy",
"deploy [args..]",
"Deploy a built OpenNext app to Cloudflare Workers",
(c) => withPopulateCacheOptions(c),
(args) => deployCommand(withWranglerPassthroughArgs(args))
Expand Down
4 changes: 2 additions & 2 deletions packages/cloudflare/src/cli/commands/populate-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,13 @@ export function addPopulateCacheCommand<T extends yargs.Argv>(y: T) {
return y.command("populateCache", "Populate the cache for a built Next.js app", (c) =>
c
.command(
"local",
"local [args..]",
"Local dev server cache",
(c) => withPopulateCacheOptions(c),
(args) => populateCacheCommand("local", withWranglerPassthroughArgs(args))
)
.command(
"remote",
"remote [args..]",
"Remote Cloudflare Worker cache",
(c) => withPopulateCacheOptions(c),
(args) => populateCacheCommand("remote", withWranglerPassthroughArgs(args))
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function previewCommand(
*/
export function addPreviewCommand<T extends yargs.Argv>(y: T) {
return y.command(
"preview",
"preview [args..]",
"Preview a built OpenNext app with a Wrangler dev server",
(c) =>
withPopulateCacheOptions(c).option("remote", {
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu
*/
export function addUploadCommand<T extends yargs.Argv>(y: T) {
return y.command(
"upload",
"upload [args..]",
"Upload a built OpenNext app to Cloudflare Workers",
(c) => withPopulateCacheOptions(c),
(args) => uploadCommand(withWranglerPassthroughArgs(args))
Expand Down
11 changes: 8 additions & 3 deletions packages/cloudflare/src/cli/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ type WranglerInputArgs = {
* @param args
* @returns An array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
*/
function getWranglerArgs(args: WranglerInputArgs & { _: (string | number)[] }): string[] {
function getWranglerArgs(
args: WranglerInputArgs & {
_: (string | number)[];
args?: (string | number)[];
}
): string[] {
if (args.configPath) {
logger.warn("The `--configPath` flag is deprecated, please use `--config` instead.");

Expand All @@ -159,8 +164,8 @@ function getWranglerArgs(args: WranglerInputArgs & { _: (string | number)[] }):
...(args.config ? ["--config", args.config] : []),
...(args.env ? ["--env", args.env] : []),
...(args.remote ? ["--remote"] : []),
// Note: the first args in `_` will be the commands.
...args._.slice(args._[0] === "populateCache" ? 2 : 1).map((a) => `${a}`),
// Note: the `args` array contains unrecognised flags.
...(args.args?.map((a) => `${a}`) ?? []),
];
}

Expand Down
19 changes: 18 additions & 1 deletion packages/cloudflare/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node

import logger from "@opennextjs/aws/logger.js";
import yargs from "yargs";

import { getVersion } from "./build/utils/version.js";
import { addBuildCommand } from "./commands/build.js";
import { addDeployCommand } from "./commands/deploy.js";
import { addMigrateCommand } from "./commands/migrate.js";
Expand All @@ -12,7 +14,22 @@ import { addUploadCommand } from "./commands/upload.js";
export function runCommand() {
const y = yargs(process.argv.slice(2).filter((arg) => arg !== "--"))
.scriptName("opennextjs-cloudflare")
.parserConfiguration({ "unknown-options-as-args": true });
.parserConfiguration({ "unknown-options-as-args": true })
.strictCommands()
.help()
.alias("h", "help")
.version(getVersion().cloudflare)
.alias("v", "version")
.fail((msg, err, yargs) => {
if (msg) {
logger.error(`${msg}\n`);
}
if (err) {
throw err;
}
yargs.showHelp();
process.exit(1);
});

addBuildCommand(y);
addPreviewCommand(y);
Expand Down
Loading