Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/hungry-ideas-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": minor
---

refactor: use yargs for the cli
4 changes: 3 additions & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"cloudflare": "^4.4.1",
"enquirer": "^2.4.1",
"glob": "catalog:",
"ts-tqdm": "^0.8.6"
"ts-tqdm": "^0.8.6",
"yargs": "catalog:"
},
"devDependencies": {
"@cloudflare/workers-types": "catalog:",
Expand All @@ -66,6 +67,7 @@
"@types/mock-fs": "catalog:",
"@types/node": "catalog:",
"@types/picomatch": "^4.0.0",
"@types/yargs": "catalog:",
"diff": "^8.0.2",
"esbuild": "catalog:",
"eslint": "catalog:",
Expand Down
38 changes: 0 additions & 38 deletions packages/cloudflare/src/cli/args.spec.ts

This file was deleted.

127 changes: 0 additions & 127 deletions packages/cloudflare/src/cli/args.ts

This file was deleted.

71 changes: 71 additions & 0 deletions packages/cloudflare/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type yargs from "yargs";

import { build as buildImpl } from "../build/build.js";
import type { WithWranglerArgs } from "./utils.js";
import {
compileConfig,
getNormalizedOptions,
nextAppDir,
printHeaders,
readWranglerConfig,
withWranglerOptions,
withWranglerPassthroughArgs,
} from "./utils.js";

/**
* Implementation of the `opennextjs-cloudflare build` command.
*
* @param args
*/
async function buildCommand(
args: WithWranglerArgs<{
skipNextBuild: boolean;
noMinify: boolean;
skipWranglerConfigCheck: boolean;
}>
): Promise<void> {
printHeaders("build");

const { config, buildDir } = await compileConfig();
const options = getNormalizedOptions(config, buildDir);

const wranglerConfig = readWranglerConfig(args);

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

/**
* Add the `build` command to yargs configuration.
*
* Consumes 1 positional parameter.
*/
export function addBuildCommand<T extends yargs.Argv>(y: T) {
return y.command(
"build",
"Build an OpenNext Cloudflare worker",
(c) =>
withWranglerOptions(c)
.option("skipNextBuild", {
type: "boolean",
alias: ["skipBuild", "s"],
default: ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)),
desc: "Skip building the Next.js app",
})
.option("noMinify", {
type: "boolean",
default: false,
desc: "Disable worker minification",
})
.option("skipWranglerConfigCheck", {
type: "boolean",
default: ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)),
desc: "Skip checking for a Wrangler config",
}),
(args) => buildCommand(withWranglerPassthroughArgs(args))
);
}
60 changes: 45 additions & 15 deletions packages/cloudflare/src/cli/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
import type yargs from "yargs";

import type { OpenNextConfig } from "../../api/config.js";
import { DEPLOYMENT_MAPPING_ENV_NAME } from "../templates/skew-protection.js";
import { getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
import { runWrangler } from "../utils/run-wrangler.js";
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
import { populateCache } from "./populate-cache.js";
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
import { getDeploymentMapping } from "./skew-protection.js";
import type { WithWranglerArgs } from "./utils.js";
import {
getNormalizedOptions,
printHeaders,
readWranglerConfig,
retrieveCompiledConfig,
withWranglerPassthroughArgs,
} from "./utils.js";

/**
* Implementation of the `opennextjs-cloudflare deploy` command.
*
* @param args
*/
export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
printHeaders("deploy");

const { config } = await retrieveCompiledConfig();
const options = getNormalizedOptions(config);

const wranglerConfig = readWranglerConfig(args);

export async function deploy(
options: BuildOptions,
config: OpenNextConfig,
deployOptions: { passthroughArgs: string[]; cacheChunkSize?: number }
) {
const envVars = await getEnvFromPlatformProxy({
// TODO: Pass the configPath, update everywhere applicable
environment: getWranglerEnvironmentFlag(deployOptions.passthroughArgs),
configPath: args.configPath,
environment: args.env,
});

const deploymentMapping = await getDeploymentMapping(options, config, envVars);

await populateCache(options, config, {
await populateCache(options, config, wranglerConfig, {
target: "remote",
environment: getWranglerEnvironmentFlag(deployOptions.passthroughArgs),
cacheChunkSize: deployOptions.cacheChunkSize,
environment: args.env,
configPath: args.configPath,
cacheChunkSize: args.cacheChunkSize,
});

runWrangler(
options,
[
"deploy",
...deployOptions.passthroughArgs,
...args.wranglerArgs,
...(deploymentMapping
? [`--var ${DEPLOYMENT_MAPPING_ENV_NAME}:${quoteShellMeta(JSON.stringify(deploymentMapping))}`]
: []),
Expand All @@ -39,3 +55,17 @@ export async function deploy(
}
);
}

/**
* Add the `deploy` command to yargs configuration.
*
* Consumes 1 positional parameter.
*/
export function addDeployCommand<T extends yargs.Argv>(y: T) {
return y.command(
"deploy",
"Deploy a built OpenNext app to Cloudflare Workers",
(c) => withPopulateCacheOptions(c),
(args) => deployCommand(withWranglerPassthroughArgs(args))
);
}
Loading