|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; |
| 6 | +import logger from "@opennextjs/aws/logger.js"; |
| 7 | +import type { |
| 8 | + IncludedIncrementalCache, |
| 9 | + IncludedTagCache, |
| 10 | + LazyLoadedOverride, |
| 11 | + OpenNextConfig, |
| 12 | +} from "@opennextjs/aws/types/open-next.js"; |
| 13 | +import type { IncrementalCache, TagCache } from "@opennextjs/aws/types/overrides.js"; |
| 14 | + |
| 15 | +export type CacheBindingMode = "local" | "remote"; |
| 16 | + |
| 17 | +async function resolveCacheName( |
| 18 | + value: |
| 19 | + | IncludedIncrementalCache |
| 20 | + | IncludedTagCache |
| 21 | + | LazyLoadedOverride<IncrementalCache> |
| 22 | + | LazyLoadedOverride<TagCache> |
| 23 | +) { |
| 24 | + return typeof value === "function" ? (await value()).name : value; |
| 25 | +} |
| 26 | + |
| 27 | +function runWrangler(opts: BuildOptions, mode: CacheBindingMode, args: string[]) { |
| 28 | + const result = spawnSync( |
| 29 | + opts.packager, |
| 30 | + ["exec", "wrangler", ...args, mode === "remote" && "--remote"].filter((v): v is string => !!v), |
| 31 | + { |
| 32 | + shell: true, |
| 33 | + stdio: ["ignore", "ignore", "inherit"], |
| 34 | + } |
| 35 | + ); |
| 36 | + |
| 37 | + if (result.status !== 0) { |
| 38 | + logger.error("Failed to populate cache"); |
| 39 | + process.exit(1); |
| 40 | + } else { |
| 41 | + logger.info("Successfully populated cache"); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export async function populateCache(opts: BuildOptions, config: OpenNextConfig, mode: CacheBindingMode) { |
| 46 | + const { incrementalCache, tagCache } = config.default.override ?? {}; |
| 47 | + |
| 48 | + if (!existsSync(opts.outputDir)) { |
| 49 | + logger.error("Unable to populate cache: Open Next build not found"); |
| 50 | + process.exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + if (!config.dangerous?.disableIncrementalCache && incrementalCache) { |
| 54 | + logger.info("Incremental cache does not need populating"); |
| 55 | + } |
| 56 | + |
| 57 | + if (!config.dangerous?.disableTagCache && !config.dangerous?.disableIncrementalCache && tagCache) { |
| 58 | + const name = await resolveCacheName(tagCache); |
| 59 | + switch (name) { |
| 60 | + case "d1-tag-cache": { |
| 61 | + logger.info("\nPopulating D1 tag cache..."); |
| 62 | + |
| 63 | + runWrangler(opts, mode, [ |
| 64 | + "d1 execute", |
| 65 | + "NEXT_CACHE_D1", |
| 66 | + `--file ${JSON.stringify(path.join(opts.outputDir, "cloudflare/cache-assets-manifest.sql"))}`, |
| 67 | + ]); |
| 68 | + break; |
| 69 | + } |
| 70 | + default: |
| 71 | + logger.info("Tag cache does not need populating"); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function isCacheBindingMode(v: string | undefined): v is CacheBindingMode { |
| 77 | + return !!v && ["local", "remote"].includes(v); |
| 78 | +} |
0 commit comments