this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: pass cli arguments not used by opennext to wrangler (#720)

authored by

James Anderson and committed by
GitHub
9ed8d8aa ae317335

+88 -20
+7
.changeset/six-moons-shine.md
··· 1 + --- 2 + "@opennextjs/cloudflare": patch 3 + --- 4 + 5 + feat: pass cli arguments not used by `opennextjs-cloudflare` to wrangler 6 + 7 + 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`.
+38
packages/cloudflare/src/cli/args.spec.ts
··· 1 + import { describe, expect, it } from "vitest"; 2 + 3 + import { getPassthroughArgs } from "./args.js"; 4 + 5 + describe("getPassthroughArgs", () => { 6 + it("should return args not used by the cli", () => { 7 + const args = [ 8 + "pnpm", 9 + "/opennextjs/cloudflare/examples/ssg-app/node_modules/@opennextjs/cloudflare/dist/cli/index.js", 10 + "preview", 11 + "--skipBuild", 12 + "--preview", 13 + "-t", 14 + "-v=1", 15 + "-pre", 16 + "152", 17 + "--pre2=1543", 18 + "--", 19 + "--port", 20 + "1234", 21 + "--inspector-port", 22 + "1234", 23 + ]; 24 + 25 + expect(getPassthroughArgs(args, { options: { skipBuild: { type: "boolean" } } })).toEqual([ 26 + "--preview", 27 + "-t", 28 + "-v=1", 29 + "-pre", 30 + "152", 31 + "--pre2=1543", 32 + "--port", 33 + "1234", 34 + "--inspector-port", 35 + "1234", 36 + ]); 37 + }); 38 + });
+43 -20
packages/cloudflare/src/cli/args.ts
··· 1 1 import { mkdirSync, type Stats, statSync } from "node:fs"; 2 2 import { resolve } from "node:path"; 3 + import type { ParseArgsConfig } from "node:util"; 3 4 import { parseArgs } from "node:util"; 4 5 5 6 import type { WranglerTarget } from "./utils/run-wrangler.js"; ··· 25 26 } 26 27 ) & { outputDir?: string }; 27 28 29 + // Config for parsing CLI arguments 30 + const config = { 31 + allowPositionals: true, 32 + strict: false, 33 + options: { 34 + skipBuild: { type: "boolean", short: "s", default: false }, 35 + output: { type: "string", short: "o" }, 36 + noMinify: { type: "boolean", default: false }, 37 + skipWranglerConfigCheck: { type: "boolean", default: false }, 38 + cacheChunkSize: { type: "string" }, 39 + }, 40 + } as const satisfies ParseArgsConfig; 41 + 28 42 export function getArgs(): Arguments { 29 - const { positionals, values } = parseArgs({ 30 - options: { 31 - skipBuild: { type: "boolean", short: "s", default: false }, 32 - output: { type: "string", short: "o" }, 33 - noMinify: { type: "boolean", default: false }, 34 - skipWranglerConfigCheck: { type: "boolean", default: false }, 35 - cacheChunkSize: { type: "string" }, 36 - }, 37 - allowPositionals: true, 38 - }); 43 + const { positionals, values } = parseArgs(config); 39 44 40 - const outputDir = values.output ? resolve(values.output) : undefined; 45 + const outputDir = typeof values.output === "string" ? resolve(values.output) : undefined; 41 46 if (outputDir) assertDirArg(outputDir, "output", true); 42 - 43 - const passthroughArgs = getPassthroughArgs(); 44 47 45 48 switch (positionals[0]) { 46 49 case "build": ··· 48 51 command: "build", 49 52 outputDir, 50 53 skipNextBuild: 51 - values.skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), 54 + !!values.skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)), 52 55 skipWranglerConfigCheck: 53 - values.skipWranglerConfigCheck || 56 + !!values.skipWranglerConfigCheck || 54 57 ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)), 55 58 minify: !values.noMinify, 56 59 }; ··· 60 63 return { 61 64 command: positionals[0], 62 65 outputDir, 63 - passthroughArgs, 66 + passthroughArgs: getPassthroughArgs(process.argv, config), 64 67 ...(values.cacheChunkSize && { cacheChunkSize: Number(values.cacheChunkSize) }), 65 68 }; 66 69 case "populateCache": ··· 71 74 command: "populateCache", 72 75 outputDir, 73 76 target: positionals[1], 74 - environment: getWranglerEnvironmentFlag(passthroughArgs), 77 + environment: getWranglerEnvironmentFlag(process.argv), 75 78 ...(values.cacheChunkSize && { cacheChunkSize: Number(values.cacheChunkSize) }), 76 79 }; 77 80 default: ··· 81 84 } 82 85 } 83 86 84 - function getPassthroughArgs() { 85 - const passthroughPos = process.argv.indexOf("--"); 86 - return passthroughPos === -1 ? [] : process.argv.slice(passthroughPos + 1); 87 + export function getPassthroughArgs<T extends ParseArgsConfig>(args: string[], { options = {} }: T) { 88 + const passthroughArgs: string[] = []; 89 + 90 + for (let i = 0; i < args.length; i++) { 91 + if (args[i] === "--") { 92 + passthroughArgs.push(...args.slice(i + 1)); 93 + return passthroughArgs; 94 + } 95 + 96 + // look for `--arg(=value)`, `-arg(=value)` 97 + const [, name] = /^--?(\w[\w-]*)(=.+)?$/.exec(args[i]!) ?? []; 98 + if (name && !(name in options)) { 99 + passthroughArgs.push(args[i]!); 100 + 101 + // Array args can have multiple values 102 + // ref https://github.com/yargs/yargs-parser/blob/main/README.md#greedy-arrays 103 + while (i < args.length - 1 && !args[i + 1]?.startsWith("-")) { 104 + passthroughArgs.push(args[++i]!); 105 + } 106 + } 107 + } 108 + 109 + return passthroughArgs; 87 110 } 88 111 89 112 function assertDirArg(path: string, argName?: string, make?: boolean) {