1- import { spawnSync } from "node:child_process" ;
21import { existsSync } from "node:fs" ;
32import path from "node:path" ;
43
5- import type { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
4+ import { BuildOptions } from "@opennextjs/aws/build/helper.js" ;
65import logger from "@opennextjs/aws/logger.js" ;
76import type {
87 IncludedIncrementalCache ,
@@ -13,7 +12,7 @@ import type {
1312import type { IncrementalCache , TagCache } from "@opennextjs/aws/types/overrides.js" ;
1413import { globSync } from "glob" ;
1514
16- export type CacheBindingMode = "local" | "remote ";
15+ import { runWrangler , WranglerTarget } from "../utils/run-wrangler.js ";
1716
1817async 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-
5427function 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- }
0 commit comments