From 26b113faa232ac4aaf31718f56cdcd80d49e170f Mon Sep 17 00:00:00 2001 From: James Date: Mon, 9 Jun 2025 07:35:16 +0100 Subject: [PATCH 1/8] feat: pass cli arguments not used by opennext to wrangler --- .changeset/six-moons-shine.md | 5 ++ packages/cloudflare/src/cli/args.spec.ts | 29 ++++++++++++ packages/cloudflare/src/cli/args.ts | 60 ++++++++++++++++-------- 3 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 .changeset/six-moons-shine.md create mode 100644 packages/cloudflare/src/cli/args.spec.ts diff --git a/.changeset/six-moons-shine.md b/.changeset/six-moons-shine.md new file mode 100644 index 00000000..341d4e08 --- /dev/null +++ b/.changeset/six-moons-shine.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/cloudflare": patch +--- + +feat: pass cli arguments not used by opennext to wrangler diff --git a/packages/cloudflare/src/cli/args.spec.ts b/packages/cloudflare/src/cli/args.spec.ts new file mode 100644 index 00000000..8a6d2646 --- /dev/null +++ b/packages/cloudflare/src/cli/args.spec.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; + +import { getPassthroughArgs } from "./args.js"; + +describe("getPassthroughArgs", () => { + it("should return args not used by the cli", () => { + const args = [ + "pnpm", + "/opennextjs/cloudflare/examples/ssg-app/node_modules/@opennextjs/cloudflare/dist/cli/index.js", + "preview", + "--skipBuild", + "--preview", + "-t", + "-v=1", + "-pre", + "152", + "--pre2=1543", + ]; + + expect(getPassthroughArgs(args, { options: { skipBuild: { type: "boolean" } } })).toEqual([ + "--preview", + "-t", + "-v=1", + "-pre", + "152", + "--pre2=1543", + ]); + }); +}); diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 292e3f7e..f333fab4 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -1,5 +1,6 @@ import { mkdirSync, type Stats, statSync } from "node:fs"; import { resolve } from "node:path"; +import type { ParseArgsConfig } from "node:util"; import { parseArgs } from "node:util"; import type { WranglerTarget } from "./utils/run-wrangler.js"; @@ -25,32 +26,33 @@ export type Arguments = ( } ) & { outputDir?: string }; +const config = { + allowPositionals: true, + strict: false, + options: { + skipBuild: { type: "boolean", short: "s", default: false }, + output: { type: "string", short: "o" }, + noMinify: { type: "boolean", default: false }, + skipWranglerConfigCheck: { type: "boolean", default: false }, + cacheChunkSize: { type: "string" }, + }, +} as const satisfies ParseArgsConfig; + export function getArgs(): Arguments { - const { positionals, values } = parseArgs({ - options: { - skipBuild: { type: "boolean", short: "s", default: false }, - output: { type: "string", short: "o" }, - noMinify: { type: "boolean", default: false }, - skipWranglerConfigCheck: { type: "boolean", default: false }, - cacheChunkSize: { type: "string" }, - }, - allowPositionals: true, - }); + const { positionals, values } = parseArgs(config); - const outputDir = values.output ? resolve(values.output) : undefined; + const outputDir = typeof values.output === "string" ? resolve(values.output) : undefined; if (outputDir) assertDirArg(outputDir, "output", true); - const passthroughArgs = getPassthroughArgs(); - switch (positionals[0]) { case "build": return { command: "build", outputDir, skipNextBuild: - values.skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), + !!values.skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), skipWranglerConfigCheck: - values.skipWranglerConfigCheck || + !!values.skipWranglerConfigCheck || ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)), minify: !values.noMinify, }; @@ -60,7 +62,7 @@ export function getArgs(): Arguments { return { command: positionals[0], outputDir, - passthroughArgs, + passthroughArgs: getPassthroughArgs(process.argv, config), ...(values.cacheChunkSize && { cacheChunkSize: Number(values.cacheChunkSize) }), }; case "populateCache": @@ -71,7 +73,7 @@ export function getArgs(): Arguments { command: "populateCache", outputDir, target: positionals[1], - environment: getWranglerEnvironmentFlag(passthroughArgs), + environment: getWranglerEnvironmentFlag(process.argv), ...(values.cacheChunkSize && { cacheChunkSize: Number(values.cacheChunkSize) }), }; default: @@ -81,9 +83,27 @@ export function getArgs(): Arguments { } } -function getPassthroughArgs() { - const passthroughPos = process.argv.indexOf("--"); - return passthroughPos === -1 ? [] : process.argv.slice(passthroughPos + 1); +export function getPassthroughArgs(args: string[], { options = {} }: T) { + const passthroughArgs: string[] = []; + + args.forEach((fullArg, idx) => { + const [, name] = /^--?(\w+)(=.+)?$/.exec(fullArg) ?? []; + if (name && !(name in options)) { + passthroughArgs.push(fullArg); + + for (let i = idx + 1; i < args.length; i++) { + const arg = args[i]; + + if (!arg || arg.startsWith("-")) { + break; + } else { + passthroughArgs.push(arg); + } + } + } + }); + + return passthroughArgs; } function assertDirArg(path: string, argName?: string, make?: boolean) { From 1dcb950b0ad0fbce9593ca84fba75e87cae09878 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 9 Jun 2025 08:18:22 +0100 Subject: [PATCH 2/8] fix args with dash not being picked up --- packages/cloudflare/src/cli/args.spec.ts | 9 +++++++++ packages/cloudflare/src/cli/args.ts | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/cloudflare/src/cli/args.spec.ts b/packages/cloudflare/src/cli/args.spec.ts index 8a6d2646..90877ea5 100644 --- a/packages/cloudflare/src/cli/args.spec.ts +++ b/packages/cloudflare/src/cli/args.spec.ts @@ -15,6 +15,11 @@ describe("getPassthroughArgs", () => { "-pre", "152", "--pre2=1543", + "--", + "--port", + "1234", + "--inspector-port", + "1234", ]; expect(getPassthroughArgs(args, { options: { skipBuild: { type: "boolean" } } })).toEqual([ @@ -24,6 +29,10 @@ describe("getPassthroughArgs", () => { "-pre", "152", "--pre2=1543", + "--port", + "1234", + "--inspector-port", + "1234", ]); }); }); diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index f333fab4..28f525a9 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -87,7 +87,7 @@ export function getPassthroughArgs(args: string[], { const passthroughArgs: string[] = []; args.forEach((fullArg, idx) => { - const [, name] = /^--?(\w+)(=.+)?$/.exec(fullArg) ?? []; + const [, name] = /^--?(\w[\w-_]*)(=.+)?$/.exec(fullArg) ?? []; if (name && !(name in options)) { passthroughArgs.push(fullArg); From fc0c1cefa62bd9598484a818aab16bb90df981b3 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Mon, 9 Jun 2025 11:04:56 +0100 Subject: [PATCH 3/8] Apply suggestions from code review Co-authored-by: Victor Berchet --- .changeset/six-moons-shine.md | 2 +- packages/cloudflare/src/cli/args.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/six-moons-shine.md b/.changeset/six-moons-shine.md index 341d4e08..4c327421 100644 --- a/.changeset/six-moons-shine.md +++ b/.changeset/six-moons-shine.md @@ -2,4 +2,4 @@ "@opennextjs/cloudflare": patch --- -feat: pass cli arguments not used by opennext to wrangler +feat: pass cli arguments not used by `opennextjs-cloudflare` to wrangler diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 28f525a9..67cc7781 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -26,6 +26,7 @@ export type Arguments = ( } ) & { outputDir?: string }; +// Config for parsing CLI arguments const config = { allowPositionals: true, strict: false, From efda97eddbe809bc2a656c3d6e1c23c9a397364a Mon Sep 17 00:00:00 2001 From: James Date: Wed, 11 Jun 2025 19:57:27 +0100 Subject: [PATCH 4/8] change to for and while loop with early return --- packages/cloudflare/src/cli/args.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 67cc7781..ef9c449b 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -87,22 +87,21 @@ export function getArgs(): Arguments { export function getPassthroughArgs(args: string[], { options = {} }: T) { const passthroughArgs: string[] = []; - args.forEach((fullArg, idx) => { - const [, name] = /^--?(\w[\w-_]*)(=.+)?$/.exec(fullArg) ?? []; - if (name && !(name in options)) { - passthroughArgs.push(fullArg); + for (let i = 0; i < args.length; i++) { + if (args[i] === "--") { + passthroughArgs.push(...args.slice(i + 1)); + return passthroughArgs; + } - for (let i = idx + 1; i < args.length; i++) { - const arg = args[i]; + const [, name] = /^--?(\w[\w-_]*)(=.+)?$/.exec(args[i]!) ?? []; + if (name && !(name in options)) { + passthroughArgs.push(args[i]!); - if (!arg || arg.startsWith("-")) { - break; - } else { - passthroughArgs.push(arg); - } + while (!args[i + 1]?.startsWith("-")) { + passthroughArgs.push(args[++i]!); } } - }); + } return passthroughArgs; } From 7d17b7cc8b8b651f125203f83c7d80ec21b344ad Mon Sep 17 00:00:00 2001 From: James Anderson Date: Wed, 18 Jun 2025 15:59:20 +0100 Subject: [PATCH 5/8] Update packages/cloudflare/src/cli/args.ts Co-authored-by: Victor Berchet --- packages/cloudflare/src/cli/args.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index ef9c449b..03c8389c 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -93,7 +93,8 @@ export function getPassthroughArgs(args: string[], { return passthroughArgs; } - const [, name] = /^--?(\w[\w-_]*)(=.+)?$/.exec(args[i]!) ?? []; + // look for `--arg(=value)`, `-arg(=value)` + const [, name] = /^--?(\w[\w-]*)(=.+)?$/.exec(args[i]!) ?? []; if (name && !(name in options)) { passthroughArgs.push(args[i]!); From d374c1ede4fe48feff3a686a8becdcddd044b51d Mon Sep 17 00:00:00 2001 From: James Anderson Date: Wed, 18 Jun 2025 16:01:58 +0100 Subject: [PATCH 6/8] Update packages/cloudflare/src/cli/args.ts Co-authored-by: Victor Berchet --- packages/cloudflare/src/cli/args.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 03c8389c..38f54236 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -98,7 +98,9 @@ export function getPassthroughArgs(args: string[], { if (name && !(name in options)) { passthroughArgs.push(args[i]!); - while (!args[i + 1]?.startsWith("-")) { + // Array args can have multiple values + // ref https://github.com/yargs/yargs-parser/blob/main/README.md#greedy-arrays + while (i < args.length - 1 && !args[i + 1]?.startsWith("-")) { passthroughArgs.push(args[++i]!); } } From 3fb7a3bf566238d08390502ee0c31925389df806 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 18 Jun 2025 16:03:15 +0100 Subject: [PATCH 7/8] formatting --- packages/cloudflare/src/cli/args.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cloudflare/src/cli/args.ts b/packages/cloudflare/src/cli/args.ts index 38f54236..ab8a8412 100644 --- a/packages/cloudflare/src/cli/args.ts +++ b/packages/cloudflare/src/cli/args.ts @@ -93,13 +93,13 @@ export function getPassthroughArgs(args: string[], { return passthroughArgs; } - // look for `--arg(=value)`, `-arg(=value)` + // look for `--arg(=value)`, `-arg(=value)` const [, name] = /^--?(\w[\w-]*)(=.+)?$/.exec(args[i]!) ?? []; if (name && !(name in options)) { passthroughArgs.push(args[i]!); - // Array args can have multiple values - // ref https://github.com/yargs/yargs-parser/blob/main/README.md#greedy-arrays + // Array args can have multiple values + // ref https://github.com/yargs/yargs-parser/blob/main/README.md#greedy-arrays while (i < args.length - 1 && !args[i + 1]?.startsWith("-")) { passthroughArgs.push(args[++i]!); } From dbc75a6d7bd2194b9408aeed45e40d2708384d4e Mon Sep 17 00:00:00 2001 From: James Anderson Date: Wed, 18 Jun 2025 16:10:01 +0100 Subject: [PATCH 8/8] Update .changeset/six-moons-shine.md --- .changeset/six-moons-shine.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.changeset/six-moons-shine.md b/.changeset/six-moons-shine.md index 4c327421..8381c45c 100644 --- a/.changeset/six-moons-shine.md +++ b/.changeset/six-moons-shine.md @@ -3,3 +3,5 @@ --- feat: pass cli arguments not used by `opennextjs-cloudflare` to wrangler + +Previously, arguments had to be provided after `--` e.g. `opennextjs-cloudflare preview -- --port 12345`. This is no longer necessary, and they can be provided normally, e.g. `opennextjs-cloudflare preview --port 12345`.