Skip to content

Commit 447f58c

Browse files
committed
move adding commands to their own files
1 parent 776c1c5 commit 447f58c

File tree

7 files changed

+147
-117
lines changed

7 files changed

+147
-117
lines changed
Lines changed: 13 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,21 @@
11
import yargs from "yargs";
22

3-
import { buildCommand } from "./commands/build.js";
4-
import { deployCommand } from "./commands/deploy.js";
5-
import { populateCacheCommand } from "./commands/populate-cache.js";
6-
import { previewCommand } from "./commands/preview.js";
7-
import { uploadCommand } from "./commands/upload.js";
3+
import { addBuildCommand } from "./commands/build.js";
4+
import { addDeployCommand } from "./commands/deploy.js";
5+
import { addPopulateCacheCommand } from "./commands/populate-cache.js";
6+
import { addPreviewCommand } from "./commands/preview.js";
7+
import { addUploadCommand } from "./commands/upload.js";
88

99
export function runCommand() {
10-
return yargs(process.argv.slice(2))
10+
let y = yargs(process.argv.slice(2))
1111
.scriptName("opennextjs-cloudflare")
12-
.parserConfiguration({ "unknown-options-as-args": true })
13-
.command(
14-
"build",
15-
"Build an OpenNext Cloudflare worker",
16-
(c) =>
17-
withWranglerOptions(c)
18-
.option("skipNextBuild", {
19-
type: "boolean",
20-
alias: ["skipBuild", "s"],
21-
default: ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)),
22-
desc: "Skip building the Next.js app",
23-
})
24-
.option("noMinify", {
25-
type: "boolean",
26-
default: false,
27-
desc: "Disable worker minification",
28-
})
29-
.option("skipWranglerConfigCheck", {
30-
type: "boolean",
31-
default: ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)),
32-
desc: "Skip checking for a Wrangler config",
33-
}),
34-
(args) => buildCommand(withWranglerPassthroughArgs(args))
35-
)
36-
.command(
37-
"preview",
38-
"Preview a built OpenNext app with a Wrangler dev server",
39-
(c) => withPopulateCacheOptions(c),
40-
(args) => previewCommand(withWranglerPassthroughArgs(args))
41-
)
42-
.command(
43-
"deploy",
44-
"Deploy a built OpenNext app to Cloudflare Workers",
45-
(c) => withPopulateCacheOptions(c),
46-
(args) => deployCommand(withWranglerPassthroughArgs(args))
47-
)
48-
.command(
49-
"upload",
50-
"Upload a built OpenNext app to Cloudflare Workers",
51-
(c) => withPopulateCacheOptions(c),
52-
(args) => uploadCommand(withWranglerPassthroughArgs(args))
53-
)
54-
.command("populateCache", "Populate the cache for a built Next.js app", (c) =>
55-
c
56-
.command(
57-
"local",
58-
"Local dev server cache",
59-
(c) => withPopulateCacheOptions(c),
60-
(args) => populateCacheCommand("local", withWranglerPassthroughArgs(args))
61-
)
62-
.command(
63-
"remote",
64-
"Remote Cloudflare Worker cache",
65-
(c) => withPopulateCacheOptions(c),
66-
(args) => populateCacheCommand("remote", withWranglerPassthroughArgs(args))
67-
)
68-
.demandCommand(1, 1)
69-
)
70-
.demandCommand(1, 1)
71-
.parse();
72-
}
73-
74-
function withWranglerOptions<T extends yargs.Argv>(args: T) {
75-
return args
76-
.options("config", {
77-
type: "string",
78-
alias: "c",
79-
desc: "Path to Wrangler configuration file",
80-
})
81-
.options("env", {
82-
type: "string",
83-
alias: "e",
84-
desc: "Wrangler environment to use for operations",
85-
});
86-
}
12+
.parserConfiguration({ "unknown-options-as-args": true });
8713

88-
function withPopulateCacheOptions<T extends yargs.Argv>(args: T) {
89-
return withWranglerOptions(args).options("cacheChunkSize", {
90-
type: "number",
91-
default: 25,
92-
desc: "Number of entries per chunk when populating the cache",
93-
});
94-
}
95-
96-
function getWranglerArgs(args: {
97-
_: (string | number)[];
98-
config: string | undefined;
99-
env: string | undefined;
100-
}): string[] {
101-
return [
102-
...(args.config ? ["--config", args.config] : []),
103-
...(args.env ? ["--env", args.env] : []),
104-
// Note: the first args in `_` will be the commands.
105-
...args._.slice(args._[0] === "populateCache" ? 2 : 1).map((a) => `${a}`),
106-
];
107-
}
14+
y = addBuildCommand(y);
15+
y = addPreviewCommand(y);
16+
y = addDeployCommand(y);
17+
y = addUploadCommand(y);
18+
y = addPopulateCacheCommand(y);
10819

109-
function withWranglerPassthroughArgs<
110-
T extends yargs.ArgumentsCamelCase<{
111-
config: string | undefined;
112-
env: string | undefined;
113-
}>,
114-
>(args: T) {
115-
return { ...args, wranglerArgs: getWranglerArgs(args) };
20+
return y.demandCommand(1, 1).parse();
11621
}

packages/cloudflare/src/cli/commands/build.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js";
2+
import type yargs from "yargs";
23

34
import { build as buildImpl } from "../build/build.js";
45
import { createOpenNextConfigIfNotExistent } from "../build/utils/create-config-files.js";
5-
import { setupCLI } from "./setup-cli.js";
6+
import { setupCLI, withWranglerOptions, withWranglerPassthroughArgs } from "./setup-cli.js";
67

7-
export async function buildCommand(args: {
8+
async function buildCommand(args: {
89
wranglerArgs: string[];
910
config: string | undefined;
1011
env: string | undefined;
@@ -19,3 +20,29 @@ export async function buildCommand(args: {
1920

2021
return buildImpl(options, config, { ...args, minify: !args.noMinify, sourceDir: baseDir }, wranglerConfig);
2122
}
23+
24+
export function addBuildCommand<T extends yargs.Argv>(y: T) {
25+
return y.command(
26+
"build",
27+
"Build an OpenNext Cloudflare worker",
28+
(c) =>
29+
withWranglerOptions(c)
30+
.option("skipNextBuild", {
31+
type: "boolean",
32+
alias: ["skipBuild", "s"],
33+
default: ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)),
34+
desc: "Skip building the Next.js app",
35+
})
36+
.option("noMinify", {
37+
type: "boolean",
38+
default: false,
39+
desc: "Disable worker minification",
40+
})
41+
.option("skipWranglerConfigCheck", {
42+
type: "boolean",
43+
default: ["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)),
44+
desc: "Skip checking for a Wrangler config",
45+
}),
46+
(args) => buildCommand(withWranglerPassthroughArgs(args))
47+
);
48+
}

packages/cloudflare/src/cli/commands/deploy.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import type yargs from "yargs";
2+
13
import { DEPLOYMENT_MAPPING_ENV_NAME } from "../templates/skew-protection.js";
24
import { runWrangler } from "../utils/run-wrangler.js";
35
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
4-
import { populateCache } from "./populate-cache.js";
6+
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
57
import type { WithWranglerArgs } from "./setup-cli.js";
6-
import { setupCompiledAppCLI } from "./setup-cli.js";
8+
import { setupCompiledAppCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
79
import { getDeploymentMapping } from "./skew-protection.js";
810

911
export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>) {
@@ -37,3 +39,12 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: num
3739
}
3840
);
3941
}
42+
43+
export function addDeployCommand<T extends yargs.Argv>(y: T) {
44+
return y.command(
45+
"deploy",
46+
"Deploy a built OpenNext app to Cloudflare Workers",
47+
(c) => withPopulateCacheOptions(c),
48+
(args) => deployCommand(withWranglerPassthroughArgs(args))
49+
);
50+
}

packages/cloudflare/src/cli/commands/populate-cache.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides
1313
import { globSync } from "glob";
1414
import { tqdm } from "ts-tqdm";
1515
import type { Unstable_Config as WranglerConfig } from "wrangler";
16+
import type yargs from "yargs";
1617

1718
import {
1819
BINDING_NAME as KV_CACHE_BINDING_NAME,
@@ -38,7 +39,7 @@ import type { WranglerTarget } from "../utils/run-wrangler.js";
3839
import { runWrangler } from "../utils/run-wrangler.js";
3940
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
4041
import type { WithWranglerArgs } from "./setup-cli.js";
41-
import { setupCompiledAppCLI } from "./setup-cli.js";
42+
import { setupCompiledAppCLI, withWranglerOptions, withWranglerPassthroughArgs } from "./setup-cli.js";
4243

4344
async function resolveCacheName(
4445
value:
@@ -287,3 +288,30 @@ export async function populateCacheCommand(
287288
cacheChunkSize: args.cacheChunkSize,
288289
});
289290
}
291+
292+
export function addPopulateCacheCommand<T extends yargs.Argv>(y: T) {
293+
return y.command("populateCache", "Populate the cache for a built Next.js app", (c) =>
294+
c
295+
.command(
296+
"local",
297+
"Local dev server cache",
298+
(c) => withPopulateCacheOptions(c),
299+
(args) => populateCacheCommand("local", withWranglerPassthroughArgs(args))
300+
)
301+
.command(
302+
"remote",
303+
"Remote Cloudflare Worker cache",
304+
(c) => withPopulateCacheOptions(c),
305+
(args) => populateCacheCommand("remote", withWranglerPassthroughArgs(args))
306+
)
307+
.demandCommand(1, 1)
308+
);
309+
}
310+
311+
export function withPopulateCacheOptions<T extends yargs.Argv>(args: T) {
312+
return withWranglerOptions(args).options("cacheChunkSize", {
313+
type: "number",
314+
default: 25,
315+
desc: "Number of entries per chunk when populating the cache",
316+
});
317+
}

packages/cloudflare/src/cli/commands/preview.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import type yargs from "yargs";
2+
13
import { runWrangler } from "../utils/run-wrangler.js";
2-
import { populateCache } from "./populate-cache.js";
4+
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
35
import type { WithWranglerArgs } from "./setup-cli.js";
4-
import { setupCompiledAppCLI } from "./setup-cli.js";
6+
import { setupCompiledAppCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
57

68
export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>) {
79
const { options, config, wranglerConfig } = await setupCompiledAppCLI("preview", args);
@@ -15,3 +17,12 @@ export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: nu
1517

1618
runWrangler(options, ["dev", ...args.wranglerArgs], { logging: "all" });
1719
}
20+
21+
export function addPreviewCommand<T extends yargs.Argv>(y: T) {
22+
return y.command(
23+
"preview",
24+
"Preview a built OpenNext app with a Wrangler dev server",
25+
(c) => withPopulateCacheOptions(c),
26+
(args) => previewCommand(withWranglerPassthroughArgs(args))
27+
);
28+
}

packages/cloudflare/src/cli/commands/setup-cli.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { normalizeOptions } from "@opennextjs/aws/build/helper.js";
66
import { printHeader, showWarningOnWindows } from "@opennextjs/aws/build/utils.js";
77
import logger from "@opennextjs/aws/logger.js";
88
import { unstable_readConfig } from "wrangler";
9+
import type yargs from "yargs";
910

1011
import { OpenNextConfig } from "../../api/config.js";
1112
import { ensureCloudflareConfig } from "../build/utils/index.js";
@@ -73,3 +74,39 @@ export function setupCompiledAppCLI(command: string, args: WithWranglerArgs) {
7374
return { config, buildDir: baseDir };
7475
});
7576
}
77+
78+
export function withWranglerOptions<T extends yargs.Argv>(args: T) {
79+
return args
80+
.options("config", {
81+
type: "string",
82+
alias: "c",
83+
desc: "Path to Wrangler configuration file",
84+
})
85+
.options("env", {
86+
type: "string",
87+
alias: "e",
88+
desc: "Wrangler environment to use for operations",
89+
});
90+
}
91+
92+
function getWranglerArgs(args: {
93+
_: (string | number)[];
94+
config: string | undefined;
95+
env: string | undefined;
96+
}): string[] {
97+
return [
98+
...(args.config ? ["--config", args.config] : []),
99+
...(args.env ? ["--env", args.env] : []),
100+
// Note: the first args in `_` will be the commands.
101+
...args._.slice(args._[0] === "populateCache" ? 2 : 1).map((a) => `${a}`),
102+
];
103+
}
104+
105+
export function withWranglerPassthroughArgs<
106+
T extends yargs.ArgumentsCamelCase<{
107+
config: string | undefined;
108+
env: string | undefined;
109+
}>,
110+
>(args: T) {
111+
return { ...args, wranglerArgs: getWranglerArgs(args) };
112+
}

packages/cloudflare/src/cli/commands/upload.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import type yargs from "yargs";
2+
13
import { DEPLOYMENT_MAPPING_ENV_NAME } from "../templates/skew-protection.js";
24
import { runWrangler } from "../utils/run-wrangler.js";
35
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
4-
import { populateCache } from "./populate-cache.js";
6+
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
57
import type { WithWranglerArgs } from "./setup-cli.js";
6-
import { setupCompiledAppCLI } from "./setup-cli.js";
8+
import { setupCompiledAppCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
79
import { getDeploymentMapping } from "./skew-protection.js";
810

911
export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>) {
@@ -35,3 +37,12 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: num
3537
{ logging: "all" }
3638
);
3739
}
40+
41+
export function addUploadCommand<T extends yargs.Argv>(y: T) {
42+
return y.command(
43+
"upload",
44+
"Upload a built OpenNext app to Cloudflare Workers",
45+
(c) => withPopulateCacheOptions(c),
46+
(args) => uploadCommand(withWranglerPassthroughArgs(args))
47+
);
48+
}

0 commit comments

Comments
 (0)