Skip to content

Commit 3ab5680

Browse files
committed
feat: add config flag support for deploy, preview, and upload commands
1 parent 89dbde7 commit 3ab5680

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

.changeset/fresh-hoops-relax.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
feature: Add `config` options to `populateCache` command
6+
7+
This change adds the `config` option to the `populateCache` command in the Cloudflare CLI. This allows users to specify a custom Wrangler configuration file when populating the cache, enhancing flexibility and usability.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
22

33
import type { OpenNextConfig } from "../../api/config.js";
4-
import { getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
4+
import { getWranglerConfigFlag, getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
55
import { populateCache } from "./populate-cache.js";
66

77
export async function deploy(
@@ -12,6 +12,7 @@ export async function deploy(
1212
await populateCache(options, config, {
1313
target: "remote",
1414
environment: getWranglerEnvironmentFlag(deployOptions.passthroughArgs),
15+
config: getWranglerConfigFlag(deployOptions.passthroughArgs),
1516
cacheChunkSize: deployOptions.cacheChunkSize,
1617
});
1718

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,14 @@ async function getPlatformProxyEnv<T extends keyof CloudflareEnv>(options: GetPl
102102

103103
async function populateR2IncrementalCache(
104104
options: BuildOptions,
105-
populateCacheOptions: { target: WranglerTarget; environment?: string }
105+
populateCacheOptions: { target: WranglerTarget; environment?: string; config?: string }
106106
) {
107107
logger.info("\nPopulating R2 incremental cache...");
108108

109-
const config = unstable_readConfig({ env: populateCacheOptions.environment });
109+
const config = unstable_readConfig({
110+
env: populateCacheOptions.environment,
111+
config: populateCacheOptions.config,
112+
});
110113

111114
const binding = config.r2_buckets.find(({ binding }) => binding === R2_CACHE_BINDING_NAME);
112115
if (!binding) {
@@ -145,11 +148,19 @@ async function populateR2IncrementalCache(
145148

146149
async function populateKVIncrementalCache(
147150
options: BuildOptions,
148-
populateCacheOptions: { target: WranglerTarget; environment?: string; cacheChunkSize?: number }
151+
populateCacheOptions: {
152+
target: WranglerTarget;
153+
environment?: string;
154+
config?: string;
155+
cacheChunkSize?: number;
156+
}
149157
) {
150158
logger.info("\nPopulating KV incremental cache...");
151159

152-
const config = unstable_readConfig({ env: populateCacheOptions.environment });
160+
const config = unstable_readConfig({
161+
env: populateCacheOptions.environment,
162+
config: populateCacheOptions.config,
163+
});
153164

154165
const binding = config.kv_namespaces.find(({ binding }) => binding === KV_CACHE_BINDING_NAME);
155166
if (!binding) {
@@ -233,7 +244,12 @@ function populateStaticAssetsIncrementalCache(options: BuildOptions) {
233244
export async function populateCache(
234245
options: BuildOptions,
235246
config: OpenNextConfig,
236-
populateCacheOptions: { target: WranglerTarget; environment?: string; cacheChunkSize?: number }
247+
populateCacheOptions: {
248+
target: WranglerTarget;
249+
environment?: string;
250+
config?: string;
251+
cacheChunkSize?: number;
252+
}
237253
) {
238254
const { incrementalCache, tagCache } = config.default.override ?? {};
239255

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
22

33
import type { OpenNextConfig } from "../../api/config.js";
4-
import { getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
4+
import { getWranglerConfigFlag, getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
55
import { populateCache } from "./populate-cache.js";
66

77
export async function preview(
@@ -12,6 +12,7 @@ export async function preview(
1212
await populateCache(options, config, {
1313
target: "local",
1414
environment: getWranglerEnvironmentFlag(previewOptions.passthroughArgs),
15+
config: getWranglerConfigFlag(previewOptions.passthroughArgs),
1516
cacheChunkSize: previewOptions.cacheChunkSize,
1617
});
1718

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BuildOptions } from "@opennextjs/aws/build/helper.js";
22

33
import type { OpenNextConfig } from "../../api/config.js";
4-
import { getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
4+
import { getWranglerConfigFlag, getWranglerEnvironmentFlag, runWrangler } from "../utils/run-wrangler.js";
55
import { populateCache } from "./populate-cache.js";
66

77
export async function upload(
@@ -12,6 +12,7 @@ export async function upload(
1212
await populateCache(options, config, {
1313
target: "remote",
1414
environment: getWranglerEnvironmentFlag(uploadOptions.passthroughArgs),
15+
config: getWranglerConfigFlag(uploadOptions.passthroughArgs),
1516
cacheChunkSize: uploadOptions.cacheChunkSize,
1617
});
1718

packages/cloudflare/src/cli/utils/run-wrangler.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,24 @@ export function getWranglerEnvironmentFlag(args: string[]) {
110110
}
111111
}
112112
}
113+
114+
/**
115+
* Find the value of the config flag (`--config` / `-c`) used by Wrangler.
116+
*
117+
* @param args - CLI arguments.
118+
* @returns Value of the config flag.
119+
*/
120+
export function getWranglerConfigFlag(args: string[]) {
121+
for (let i = 0; i <= args.length; i++) {
122+
const arg = args[i];
123+
if (!arg) continue;
124+
125+
if (arg === "--config" || arg === "-c") {
126+
return args[i + 1];
127+
}
128+
129+
if (arg.startsWith("--config=") || arg.startsWith("-c=")) {
130+
return arg.split("=")[1];
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)