Skip to content

Commit b7354bb

Browse files
james-elicxvicb
authored andcommitted
improve jsdoc and consolidate to single steup cli func
1 parent 0776a97 commit b7354bb

File tree

6 files changed

+118
-48
lines changed

6 files changed

+118
-48
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js";
21
import type yargs from "yargs";
32

43
import { build as buildImpl } from "../build/build.js";
5-
import { createOpenNextConfigIfNotExistent } from "../build/utils/create-config-files.js";
64
import type { WithWranglerArgs } from "./setup-cli.js";
75
import { setupCLI, withWranglerOptions, withWranglerPassthroughArgs } from "./setup-cli.js";
86

7+
/**
8+
* Implementation of the `opennextjs-clouflare build` command.
9+
*
10+
* @param args
11+
*/
912
async function buildCommand(
1013
args: WithWranglerArgs<{
1114
skipNextBuild: boolean;
1215
noMinify: boolean;
1316
skipWranglerConfigCheck: boolean;
1417
}>
15-
) {
16-
const { options, config, wranglerConfig, baseDir } = await setupCLI("build", args, async (baseDir) => {
17-
await createOpenNextConfigIfNotExistent(baseDir);
18-
return compileOpenNextConfig(baseDir, undefined, { compileEdge: true });
18+
): Promise<void> {
19+
const { options, config, wranglerConfig, baseDir } = await setupCLI({
20+
command: "build",
21+
shouldCompileConfig: true,
22+
args,
1923
});
2024

21-
return buildImpl(options, config, { ...args, minify: !args.noMinify, sourceDir: baseDir }, wranglerConfig);
25+
await buildImpl(options, config, { ...args, minify: !args.noMinify, sourceDir: baseDir }, wranglerConfig);
2226
}
2327

28+
/**
29+
* Add the `build` command to yargs configuration.
30+
*
31+
* Consumes 1 positional parameter.
32+
*/
2433
export function addBuildCommand<T extends yargs.Argv>(y: T) {
2534
return y.command(
2635
"build",

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import { runWrangler } from "../utils/run-wrangler.js";
55
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
66
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
77
import type { WithWranglerArgs } from "./setup-cli.js";
8-
import { setupCompiledAppCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
8+
import { setupCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
99
import { getDeploymentMapping } from "./skew-protection.js";
1010

11-
export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>) {
12-
const { options, config, wranglerConfig } = await setupCompiledAppCLI("deploy", args);
11+
/**
12+
* Implementation of the `opennextjs-clouflare deploy` command.
13+
*
14+
* @param args
15+
*/
16+
export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
17+
const { options, config, wranglerConfig } = await setupCLI({ command: "deploy", args });
1318

1419
const envVars = await getEnvFromPlatformProxy({
1520
configPath: args.configPath,
@@ -40,6 +45,11 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: num
4045
);
4146
}
4247

48+
/**
49+
* Add the `deploy` command to yargs configuration.
50+
*
51+
* Consumes 1 positional parameter.
52+
*/
4353
export function addDeployCommand<T extends yargs.Argv>(y: T) {
4454
return y.command(
4555
"deploy",

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import type { WranglerTarget } from "../utils/run-wrangler.js";
3939
import { runWrangler } from "../utils/run-wrangler.js";
4040
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
4141
import type { WithWranglerArgs } from "./setup-cli.js";
42-
import { setupCompiledAppCLI, withWranglerOptions, withWranglerPassthroughArgs } from "./setup-cli.js";
42+
import { setupCLI, withWranglerOptions, withWranglerPassthroughArgs } from "./setup-cli.js";
4343

4444
async function resolveCacheName(
4545
value:
@@ -275,11 +275,16 @@ export async function populateCache(
275275
}
276276
}
277277

278-
export async function populateCacheCommand(
278+
/**
279+
* Implementation of the `opennextjs-clouflare populateCache` command.
280+
*
281+
* @param args
282+
*/
283+
async function populateCacheCommand(
279284
target: "local" | "remote",
280285
args: WithWranglerArgs<{ cacheChunkSize: number }>
281286
) {
282-
const { options, config, wranglerConfig } = await setupCompiledAppCLI("populate cache", args);
287+
const { options, config, wranglerConfig } = await setupCLI({ command: "populate cache", args });
283288

284289
await populateCache(options, config, wranglerConfig, {
285290
target,
@@ -289,6 +294,11 @@ export async function populateCacheCommand(
289294
});
290295
}
291296

297+
/**
298+
* Add the `populateCache` command to yargs configuration, with nested commands for `local` and `remote`.
299+
*
300+
* Consumes 2 positional parameters.
301+
*/
292302
export function addPopulateCacheCommand<T extends yargs.Argv>(y: T) {
293303
return y.command("populateCache", "Populate the cache for a built Next.js app", (c) =>
294304
c

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import type yargs from "yargs";
33
import { runWrangler } from "../utils/run-wrangler.js";
44
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
55
import type { WithWranglerArgs } from "./setup-cli.js";
6-
import { setupCompiledAppCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
6+
import { setupCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
77

8-
export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>) {
9-
const { options, config, wranglerConfig } = await setupCompiledAppCLI("preview", args);
8+
/**
9+
* Implementation of the `opennextjs-clouflare preview` command.
10+
*
11+
* @param args
12+
*/
13+
export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
14+
const { options, config, wranglerConfig } = await setupCLI({ command: "preview", args });
1015

1116
await populateCache(options, config, wranglerConfig, {
1217
target: "local",
@@ -18,6 +23,11 @@ export async function previewCommand(args: WithWranglerArgs<{ cacheChunkSize: nu
1823
runWrangler(options, ["dev", ...args.wranglerArgs], { logging: "all" });
1924
}
2025

26+
/**
27+
* Add the `preview` command to yargs configuration.
28+
*
29+
* Consumes 1 positional parameter.
30+
*/
2131
export function addPreviewCommand<T extends yargs.Argv>(y: T) {
2232
return y.command(
2333
"preview",

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

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,44 @@ import { existsSync } from "node:fs";
22
import { createRequire } from "node:module";
33
import path from "node:path";
44

5+
import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js";
56
import { normalizeOptions } from "@opennextjs/aws/build/helper.js";
67
import { printHeader, showWarningOnWindows } from "@opennextjs/aws/build/utils.js";
78
import logger from "@opennextjs/aws/logger.js";
89
import { unstable_readConfig } from "wrangler";
910
import type yargs from "yargs";
1011

1112
import { OpenNextConfig } from "../../api/config.js";
12-
import { ensureCloudflareConfig } from "../build/utils/index.js";
13+
import { createOpenNextConfigIfNotExistent, ensureCloudflareConfig } from "../build/utils/index.js";
1314

1415
export type WithWranglerArgs<T = unknown> = T & {
16+
// Array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
1517
wranglerArgs: string[];
1618
configPath: string | undefined;
1719
env: string | undefined;
1820
};
1921

2022
const nextAppDir = process.cwd();
2123

24+
type Options = {
25+
command: string;
26+
shouldCompileConfig?: boolean;
27+
args: WithWranglerArgs;
28+
};
29+
2230
/**
23-
* Setup the CLI, print necessary messages, and retrieve various options and configs.
31+
* Sets up the CLI and returns config information:
32+
* - Prints necessary header messages and warnings,
33+
* - Retrieves the OpenNext config and validates it.
34+
* - Initialises the OpenNext options.
35+
* - Reads the Wrangler config.
2436
*
2537
* @param command
38+
* @param shouldCompileConfig
2639
* @param args
27-
* @param getOpenNextConfig - Function that resolves to a config file
28-
* @returns CLI options, OpenNext config, and Wrangler config
40+
* @returns CLI options, OpenNext config, and Wrangler config.
2941
*/
30-
export async function setupCLI(
31-
command: string,
32-
args: WithWranglerArgs,
33-
getOpenNextConfig: (baseDir: string) => Promise<{ config: OpenNextConfig; buildDir: string }>
34-
) {
42+
export async function setupCLI({ command, shouldCompileConfig, args }: Options) {
3543
printHeader(`Cloudflare ${command}`);
3644

3745
showWarningOnWindows();
@@ -40,41 +48,44 @@ export async function setupCLI(
4048
const require = createRequire(import.meta.url);
4149
const openNextDistDir = path.dirname(require.resolve("@opennextjs/aws/index.js"));
4250

43-
const { config, buildDir } = await getOpenNextConfig(baseDir);
51+
const { config, buildDir } = await getOpenNextConfig({ shouldCompileConfig, baseDir });
4452
ensureCloudflareConfig(config);
4553

4654
// Initialize options
4755
const options = normalizeOptions(config, openNextDistDir, buildDir);
4856
logger.setLevel(options.debug ? "debug" : "info");
4957

50-
const wranglerConfig = unstable_readConfig({ env: args.env, config: args.config });
58+
const wranglerConfig = unstable_readConfig({ env: args.env, config: args.configPath });
5159

5260
return { options, config, wranglerConfig, baseDir };
5361
}
5462

55-
/**
56-
* Setup the CLI, print necessary messages, and resolve the compiled OpenNext config.
57-
*
58-
* @param command
59-
* @param args
60-
* @returns CLI config
61-
*/
62-
export function setupCompiledAppCLI(command: string, args: WithWranglerArgs) {
63-
return setupCLI(command, args, async (baseDir) => {
64-
const configPath = path.join(baseDir, ".open-next/.build/open-next.config.edge.mjs");
63+
async function getOpenNextConfig(opts: {
64+
shouldCompileConfig?: boolean;
65+
baseDir: string;
66+
}): Promise<{ config: OpenNextConfig; buildDir: string }> {
67+
if (opts.shouldCompileConfig) {
68+
await createOpenNextConfigIfNotExistent(opts.baseDir);
69+
70+
return compileOpenNextConfig(opts.baseDir, undefined, { compileEdge: true });
71+
}
6572

66-
if (!existsSync(configPath)) {
67-
logger.error("Could not find compiled Open Next config");
68-
process.exit(1);
69-
}
73+
const configPath = path.join(opts.baseDir, ".open-next/.build/open-next.config.edge.mjs");
7074

71-
const config = await import(configPath).then((mod) => mod.default);
75+
if (!existsSync(configPath)) {
76+
logger.error("Could not find compiled Open Next config");
77+
process.exit(1);
78+
}
7279

73-
// Note: buildDir is not used when an app is already compiled.
74-
return { config, buildDir: baseDir };
75-
});
80+
const config = await import(configPath).then((mod) => mod.default);
81+
82+
// Note: buildDir is not used when an app is already compiled.
83+
return { config, buildDir: opts.baseDir };
7684
}
7785

86+
/**
87+
* Add flags for the wrangler config path and environment to the yargs configuration.
88+
*/
7889
export function withWranglerOptions<T extends yargs.Argv>(args: T) {
7990
return args
8091
.options("configPath", {
@@ -89,6 +100,11 @@ export function withWranglerOptions<T extends yargs.Argv>(args: T) {
89100
});
90101
}
91102

103+
/**
104+
*
105+
* @param args
106+
* @returns An array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
107+
*/
92108
function getWranglerArgs(args: {
93109
_: (string | number)[];
94110
configPath: string | undefined;
@@ -102,6 +118,11 @@ function getWranglerArgs(args: {
102118
];
103119
}
104120

121+
/**
122+
*
123+
* @param args
124+
* @returns The inputted args, and an array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
125+
*/
105126
export function withWranglerPassthroughArgs<
106127
T extends yargs.ArgumentsCamelCase<{
107128
configPath: string | undefined;

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import { runWrangler } from "../utils/run-wrangler.js";
55
import { getEnvFromPlatformProxy, quoteShellMeta } from "./helpers.js";
66
import { populateCache, withPopulateCacheOptions } from "./populate-cache.js";
77
import type { WithWranglerArgs } from "./setup-cli.js";
8-
import { setupCompiledAppCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
8+
import { setupCLI, withWranglerPassthroughArgs } from "./setup-cli.js";
99
import { getDeploymentMapping } from "./skew-protection.js";
1010

11-
export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>) {
12-
const { options, config, wranglerConfig } = await setupCompiledAppCLI("upload", args);
11+
/**
12+
* Implementation of the `opennextjs-clouflare upload` command.
13+
*
14+
* @param args
15+
*/
16+
export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: number }>): Promise<void> {
17+
const { options, config, wranglerConfig } = await setupCLI({ command: "upload", args });
1318

1419
const envVars = await getEnvFromPlatformProxy({
1520
configPath: args.configPath,
@@ -38,6 +43,11 @@ export async function uploadCommand(args: WithWranglerArgs<{ cacheChunkSize: num
3843
);
3944
}
4045

46+
/**
47+
* Add the `upload` command to yargs configuration.
48+
*
49+
* Consumes 1 positional parameter.
50+
*/
4151
export function addUploadCommand<T extends yargs.Argv>(y: T) {
4252
return y.command(
4353
"upload",

0 commit comments

Comments
 (0)