Skip to content

Commit b158e8b

Browse files
james-elicxvicb
andauthored
refactor: use yargs for the cli (#791)
* refactor: use yargs for the cli * fix e2e environment passing now that `--env` is a proper first class citizen * remove accidental aliases * passthrough -> wranglerArgs * jsdoc comments * move adding commands to their own files * remove args.ts file * bump eslint due to typescript-eslint/issues/10353 * config -> configPath * improve jsdoc and consolidate to single steup cli func * add an extra jsdoc * updates * refactor * misc updates * filter out '--' * break up the setupCli functions * setup-cli.ts -> utils.ts * lint * Update packages/cloudflare/src/cli/commands/utils.ts Co-authored-by: Victor Berchet <[email protected]> --------- Co-authored-by: Victor Berchet <[email protected]>
1 parent fdfa5af commit b158e8b

File tree

18 files changed

+897
-425
lines changed

18 files changed

+897
-425
lines changed

.changeset/hungry-ideas-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": minor
3+
---
4+
5+
refactor: use yargs for the cli

packages/cloudflare/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
"cloudflare": "^4.4.1",
5858
"enquirer": "^2.4.1",
5959
"glob": "catalog:",
60-
"ts-tqdm": "^0.8.6"
60+
"ts-tqdm": "^0.8.6",
61+
"yargs": "catalog:"
6162
},
6263
"devDependencies": {
6364
"@cloudflare/workers-types": "catalog:",
@@ -66,6 +67,7 @@
6667
"@types/mock-fs": "catalog:",
6768
"@types/node": "catalog:",
6869
"@types/picomatch": "^4.0.0",
70+
"@types/yargs": "catalog:",
6971
"diff": "^8.0.2",
7072
"esbuild": "catalog:",
7173
"eslint": "catalog:",

packages/cloudflare/src/cli/args.spec.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/cloudflare/src/cli/args.ts

Lines changed: 0 additions & 127 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import type yargs from "yargs";
2+
3+
import { build as buildImpl } from "../build/build.js";
4+
import type { WithWranglerArgs } from "./utils.js";
5+
import {
6+
compileConfig,
7+
getNormalizedOptions,
8+
nextAppDir,
9+
printHeaders,
10+
readWranglerConfig,
11+
withWranglerOptions,
12+
withWranglerPassthroughArgs,
13+
} from "./utils.js";
14+
15+
/**
16+
* Implementation of the `opennextjs-cloudflare build` command.
17+
*
18+
* @param args
19+
*/
20+
async function buildCommand(
21+
args: WithWranglerArgs<{
22+
skipNextBuild: boolean;
23+
noMinify: boolean;
24+
skipWranglerConfigCheck: boolean;
25+
}>
26+
): Promise<void> {
27+
printHeaders("build");
28+
29+
const { config, buildDir } = await compileConfig();
30+
const options = getNormalizedOptions(config, buildDir);
31+
32+
const wranglerConfig = readWranglerConfig(args);
33+
34+
await buildImpl(
35+
options,
36+
config,
37+
{ ...args, minify: !args.noMinify, sourceDir: nextAppDir },
38+
wranglerConfig
39+
);
40+
}
41+
42+
/**
43+
* Add the `build` command to yargs configuration.
44+
*
45+
* Consumes 1 positional parameter.
46+
*/
47+
export function addBuildCommand<T extends yargs.Argv>(y: T) {
48+
return y.command(
49+
"build",
50+
"Build an OpenNext Cloudflare worker",
51+
(c) =>
52+
withWranglerOptions(c)
53+
.option("skipNextBuild", {
54+
type: "boolean",
55+
alias: ["skipBuild", "s"],
56+
default: ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)),
57+
desc: "Skip building the Next.js app",
58+
})
59+
.option("noMinify", {
60+
type: "boolean",
61+
default: false,
62+
desc: "Disable worker minification",
63+
})
64+
.option("skipWranglerConfigCheck", {
65+
type: "boolean",
66+
default: ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)),
67+
desc: "Skip checking for a Wrangler config",
68+
}),
69+
(args) => buildCommand(withWranglerPassthroughArgs(args))
70+
);
71+
}
Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
1-
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
1+
import type yargs from "yargs";
22

3-
import type { OpenNextConfig } from "../../api/config.js";
43
import { DEPLOYMENT_MAPPING_ENV_NAME } from "../templates/skew-protection.js";
5-
import { getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
4+
import { runWrangler } from "../utils/run-wrangler.js";
65
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
7-
import { populateCache } from "./populate-cache.js";
6+
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
87
import { getDeploymentMapping } from "./skew-protection.js";
8+
import type { WithWranglerArgs } from "./utils.js";
9+
import {
10+
getNormalizedOptions,
11+
printHeaders,
12+
readWranglerConfig,
13+
retrieveCompiledConfig,
14+
withWranglerPassthroughArgs,
15+
} from "./utils.js";
16+
17+
/**
18+
* Implementation of the `opennextjs-cloudflare deploy` command.
19+
*
20+
* @param args
21+
*/
22+
export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
23+
printHeaders("deploy");
24+
25+
const { config } = await retrieveCompiledConfig();
26+
const options = getNormalizedOptions(config);
27+
28+
const wranglerConfig = readWranglerConfig(args);
929

10-
export async function deploy(
11-
options: BuildOptions,
12-
config: OpenNextConfig,
13-
deployOptions: { passthroughArgs: string[]; cacheChunkSize?: number }
14-
) {
1530
const envVars = await getEnvFromPlatformProxy({
16-
// TODO: Pass the configPath, update everywhere applicable
17-
environment: getWranglerEnvironmentFlag(deployOptions.passthroughArgs),
31+
configPath: args.configPath,
32+
environment: args.env,
1833
});
1934

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

22-
await populateCache(options, config, {
37+
await populateCache(options, config, wranglerConfig, {
2338
target: "remote",
24-
environment: getWranglerEnvironmentFlag(deployOptions.passthroughArgs),
25-
cacheChunkSize: deployOptions.cacheChunkSize,
39+
environment: args.env,
40+
configPath: args.configPath,
41+
cacheChunkSize: args.cacheChunkSize,
2642
});
2743

2844
runWrangler(
2945
options,
3046
[
3147
"deploy",
32-
...deployOptions.passthroughArgs,
48+
...args.wranglerArgs,
3349
...(deploymentMapping
3450
? [`--var ${DEPLOYMENT_MAPPING_ENV_NAME}:${quoteShellMeta(JSON.stringify(deploymentMapping))}`]
3551
: []),
@@ -39,3 +55,17 @@ export async function deploy(
3955
}
4056
);
4157
}
58+
59+
/**
60+
* Add the `deploy` command to yargs configuration.
61+
*
62+
* Consumes 1 positional parameter.
63+
*/
64+
export function addDeployCommand<T extends yargs.Argv>(y: T) {
65+
return y.command(
66+
"deploy",
67+
"Deploy a built OpenNext app to Cloudflare Workers",
68+
(c) => withPopulateCacheOptions(c),
69+
(args) => deployCommand(withWranglerPassthroughArgs(args))
70+
);
71+
}

0 commit comments

Comments
 (0)