Skip to content

Commit 2aab6fb

Browse files
committed
cleanup and move around some utils
1 parent 29c74d1 commit 2aab6fb

File tree

6 files changed

+58
-50
lines changed

6 files changed

+58
-50
lines changed

packages/cloudflare/src/cli/args.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { mkdirSync, type Stats, statSync } from "node:fs";
22
import { resolve } from "node:path";
33
import { parseArgs } from "node:util";
44

5-
import type { CacheBindingTarget } from "./build/utils/index.js";
6-
import { isCacheBindingTarget } from "./build/utils/index.js";
5+
import { isWranglerTarget, WranglerTarget } from "./utils/run-wrangler";
76

87
export type Arguments = (
98
| {
@@ -13,7 +12,7 @@ export type Arguments = (
1312
minify: boolean;
1413
}
1514
| { command: "preview" | "deploy" }
16-
| { command: "populateCache"; target: CacheBindingTarget }
15+
| { command: "populateCache"; target: WranglerTarget }
1716
) & { outputDir?: string };
1817

1918
export function getArgs(): Arguments {
@@ -47,7 +46,7 @@ export function getArgs(): Arguments {
4746
case "deploy":
4847
return { command: "preview", outputDir };
4948
case "populateCache":
50-
if (!isCacheBindingTarget(positionals[1])) {
49+
if (!isWranglerTarget(positionals[1])) {
5150
throw new Error(`Error: invalid target for populating the cache, expected 'local' | 'remote'`);
5251
}
5352
return { command: "populateCache", outputDir, target: positionals[1] };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
2+
import { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
3+
4+
export async function deploy(options: BuildOptions, config: OpenNextConfig) {}
Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { spawnSync } from "node:child_process";
21
import { existsSync } from "node:fs";
32
import path from "node:path";
43

5-
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
4+
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
65
import logger from "@opennextjs/aws/logger.js";
76
import type {
87
IncludedIncrementalCache,
@@ -13,7 +12,7 @@ import type {
1312
import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides.js";
1413
import { globSync } from "glob";
1514

16-
export type CacheBindingMode = "local" | "remote";
15+
import { runWrangler, WranglerTarget } from "../utils/run-wrangler.js";
1716

1817
async function resolveCacheName(
1918
value:
@@ -25,32 +24,6 @@ async function resolveCacheName(
2524
return typeof value === "function" ? (await value()).name : value;
2625
}
2726

28-
function runWrangler(
29-
opts: BuildOptions,
30-
wranglerOpts: { mode: CacheBindingMode; excludeRemoteFlag?: boolean },
31-
args: string[]
32-
) {
33-
const result = spawnSync(
34-
opts.packager,
35-
[
36-
"exec",
37-
"wrangler",
38-
...args,
39-
wranglerOpts.mode === "remote" && !wranglerOpts.excludeRemoteFlag && "--remote",
40-
wranglerOpts.mode === "local" && "--local",
41-
].filter((v): v is string => !!v),
42-
{
43-
shell: true,
44-
stdio: ["ignore", "ignore", "inherit"],
45-
}
46-
);
47-
48-
if (result.status !== 0) {
49-
logger.error("Failed to populate cache");
50-
process.exit(1);
51-
}
52-
}
53-
5427
function getCacheAssetPaths(opts: BuildOptions) {
5528
return globSync(path.join(opts.outputDir, "cache/**/*"), { withFileTypes: true })
5629
.filter((f) => f.isFile())
@@ -66,10 +39,14 @@ function getCacheAssetPaths(opts: BuildOptions) {
6639
});
6740
}
6841

69-
export async function populateCache(opts: BuildOptions, config: OpenNextConfig, mode: CacheBindingMode) {
42+
export async function populateCache(
43+
options: BuildOptions,
44+
config: OpenNextConfig,
45+
populateCacheOptions: { target: WranglerTarget }
46+
) {
7047
const { incrementalCache, tagCache } = config.default.override ?? {};
7148

72-
if (!existsSync(opts.outputDir)) {
49+
if (!existsSync(options.outputDir)) {
7350
logger.error("Unable to populate cache: Open Next build not found");
7451
process.exit(1);
7552
}
@@ -80,15 +57,15 @@ export async function populateCache(opts: BuildOptions, config: OpenNextConfig,
8057
case "r2-incremental-cache": {
8158
logger.info("\nPopulating R2 incremental cache...");
8259

83-
const assets = getCacheAssetPaths(opts);
60+
const assets = getCacheAssetPaths(options);
8461
assets.forEach(({ fsPath, destPath }) => {
8562
const fullDestPath = path.join(
8663
"NEXT_CACHE_R2_BUCKET",
8764
process.env.NEXT_CACHE_R2_PREFIX ?? "incremental-cache",
8865
destPath
8966
);
9067

91-
runWrangler(opts, { mode, excludeRemoteFlag: true }, [
68+
runWrangler(options.packager, { ...populateCacheOptions, excludeRemoteFlag: true }, [
9269
"r2 object put",
9370
JSON.stringify(fullDestPath),
9471
`--file ${JSON.stringify(fsPath)}`,
@@ -108,10 +85,10 @@ export async function populateCache(opts: BuildOptions, config: OpenNextConfig,
10885
case "d1-tag-cache": {
10986
logger.info("\nPopulating D1 tag cache...");
11087

111-
runWrangler(opts, { mode }, [
88+
runWrangler(options.packager, populateCacheOptions, [
11289
"d1 execute",
11390
"NEXT_CACHE_D1",
114-
`--file ${JSON.stringify(path.join(opts.outputDir, "cloudflare/cache-assets-manifest.sql"))}`,
91+
`--file ${JSON.stringify(path.join(options.outputDir, "cloudflare/cache-assets-manifest.sql"))}`,
11592
]);
11693
logger.info("Successfully populated cache");
11794
break;
@@ -121,7 +98,3 @@ export async function populateCache(opts: BuildOptions, config: OpenNextConfig,
12198
}
12299
}
123100
}
124-
125-
export function isCacheBindingMode(v: string | undefined): v is CacheBindingMode {
126-
return !!v && ["local", "remote"].includes(v);
127-
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
2+
import { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
3+
4+
export async function preview(options: BuildOptions, config: OpenNextConfig) {}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import type { CacheBindingMode } from "./build/utils/index.js";
2-
31
export type ProjectOptions = {
4-
// Next app root folder
5-
sourceDir: string;
6-
// The directory to save the output to (defaults to the app's directory)
7-
outputDir: string;
82
// Whether the Next.js build should be skipped (i.e. if the `.next` dir is already built)
93
skipNextBuild: boolean;
104
// Whether the check to see if a wrangler config file exists should be skipped
115
skipWranglerConfigCheck: boolean;
126
// Whether minification of the worker should be enabled
137
minify: boolean;
14-
populateCache?: { mode: CacheBindingMode; onlyPopulateWithoutBuilding: boolean };
158
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { spawnSync } from "node:child_process";
2+
3+
import logger from "@opennextjs/aws/logger.js";
4+
5+
export type WranglerTarget = "local" | "remote";
6+
7+
export function runWrangler(
8+
pm: string,
9+
wranglerOpts: { target: WranglerTarget; excludeRemoteFlag?: boolean },
10+
args: string[]
11+
) {
12+
const result = spawnSync(
13+
pm,
14+
[
15+
"exec",
16+
"wrangler",
17+
...args,
18+
wranglerOpts.target === "remote" && !wranglerOpts.excludeRemoteFlag && "--remote",
19+
wranglerOpts.target === "local" && "--local",
20+
].filter((v): v is string => !!v),
21+
{
22+
shell: true,
23+
stdio: ["ignore", "ignore", "inherit"],
24+
}
25+
);
26+
27+
if (result.status !== 0) {
28+
logger.error("Failed to populate cache");
29+
process.exit(1);
30+
}
31+
}
32+
33+
export function isWranglerTarget(v: string | undefined): v is WranglerTarget {
34+
return !!v && ["local", "remote"].includes(v);
35+
}

0 commit comments

Comments
 (0)