|
| 1 | +import { statSync } from "node:fs"; |
| 2 | +import { readFile, writeFile } from "node:fs/promises"; |
| 3 | +import { brandColor, dim } from "@cloudflare/cli/colors"; |
| 4 | +import { spinner } from "@cloudflare/cli/interactive"; |
| 5 | +import { getPackageManager } from "../../package-manager"; |
| 6 | +import { dedent } from "../../utils/dedent"; |
| 7 | +import { installPackages } from "../c3-vendor/packages"; |
| 8 | +import { AutoConfigFrameworkConfigurationError } from "../errors"; |
| 9 | +import { appendToGitIgnore } from "../git"; |
| 10 | +import { usesTypescript } from "../uses-typescript"; |
| 11 | +import { Framework } from "."; |
| 12 | +import type { ConfigurationOptions, ConfigurationResults } from "."; |
| 13 | + |
| 14 | +export class NextJs extends Framework { |
| 15 | + preview = "opennextjs-cloudflare build && opennextjs-cloudflare preview"; |
| 16 | + deploy = "opennextjs-cloudflare build && opennextjs-cloudflare deploy"; |
| 17 | + |
| 18 | + async configure({ |
| 19 | + dryRun, |
| 20 | + projectPath, |
| 21 | + }: ConfigurationOptions): Promise<ConfigurationResults> { |
| 22 | + const usesTs = usesTypescript(projectPath); |
| 23 | + |
| 24 | + const nextConfigPath = probeForNextConfigPath(usesTs); |
| 25 | + if (!nextConfigPath) { |
| 26 | + throw new AutoConfigFrameworkConfigurationError( |
| 27 | + "No Next.js configuration file could be detected. Note: only next.config.ts and next.config.mjs files are supported." |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + if (!dryRun) { |
| 32 | + await installPackages(["@opennextjs/cloudflare@^1.12.0"], { |
| 33 | + startText: "Installing @opennextjs/cloudflare adapter", |
| 34 | + doneText: `${brandColor("installed")}`, |
| 35 | + }); |
| 36 | + |
| 37 | + await updateNextConfig(nextConfigPath); |
| 38 | + |
| 39 | + await createOpenNextConfigFile(projectPath); |
| 40 | + |
| 41 | + await appendToGitIgnore( |
| 42 | + projectPath, |
| 43 | + dedent` |
| 44 | + # OpenNext |
| 45 | + .open-next |
| 46 | + `, |
| 47 | + { |
| 48 | + startText: "Adding open-next section to .gitignore file", |
| 49 | + doneText: `${brandColor(`added`)} open-next section to .gitignore file`, |
| 50 | + } |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + const { npx } = await getPackageManager(); |
| 55 | + |
| 56 | + return { |
| 57 | + wranglerConfig: { |
| 58 | + main: ".open-next/worker.js", |
| 59 | + compatibility_flags: ["nodejs_compat", "global_fetch_strictly_public"], |
| 60 | + assets: { |
| 61 | + binding: "ASSETS", |
| 62 | + directory: ".open-next/assets", |
| 63 | + }, |
| 64 | + }, |
| 65 | + buildCommand: `${npx} @opennextjs/cloudflare build`, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + configurationDescription = "Configuring project for Next.js with OpenNext"; |
| 70 | +} |
| 71 | + |
| 72 | +function probeForNextConfigPath(usesTs: boolean): string | undefined { |
| 73 | + const pathsToProbe = [ |
| 74 | + ...(usesTs ? ["next.config.ts"] : []), |
| 75 | + "next.config.mjs", |
| 76 | + ]; |
| 77 | + |
| 78 | + for (const path of pathsToProbe) { |
| 79 | + const stats = statSync(path, { |
| 80 | + throwIfNoEntry: false, |
| 81 | + }); |
| 82 | + if (stats?.isFile()) { |
| 83 | + return path; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +async function updateNextConfig(nextConfigPath: string) { |
| 89 | + const s = spinner(); |
| 90 | + |
| 91 | + s.start(`Updating \`${nextConfigPath}\``); |
| 92 | + |
| 93 | + const configContent = await readFile(nextConfigPath); |
| 94 | + |
| 95 | + const updatedConfigFile = |
| 96 | + configContent + |
| 97 | + ` |
| 98 | + // added by create cloudflare to enable calling \`getCloudflareContext()\` in \`next dev\` |
| 99 | + import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare'; |
| 100 | + initOpenNextCloudflareForDev(); |
| 101 | + `.replace(/\n\t*/g, "\n"); |
| 102 | + |
| 103 | + await writeFile(nextConfigPath, updatedConfigFile); |
| 104 | + |
| 105 | + s.stop(`${brandColor(`updated`)} ${dim(`\`${nextConfigPath}\``)}`); |
| 106 | +} |
| 107 | + |
| 108 | +async function createOpenNextConfigFile(projectPath: string) { |
| 109 | + const s = spinner(); |
| 110 | + |
| 111 | + s.start("Creating open-next.config.ts file"); |
| 112 | + |
| 113 | + await writeFile( |
| 114 | + // TODO: this always saves the file as open-next.config.ts, is a js alternative also supported? |
| 115 | + // (since the project might not be using TypeScript) |
| 116 | + `${projectPath}/open-next.config.ts`, |
| 117 | + dedent`import { defineCloudflareConfig } from "@opennextjs/cloudflare"; |
| 118 | +
|
| 119 | + export default defineCloudflareConfig({ |
| 120 | + // Uncomment to enable R2 cache, |
| 121 | + // It should be imported as: |
| 122 | + // \`import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";\` |
| 123 | + // See https://opennext.js.org/cloudflare/caching for more details |
| 124 | + // incrementalCache: r2IncrementalCache, |
| 125 | + }); |
| 126 | + ` |
| 127 | + ); |
| 128 | + |
| 129 | + s.stop(`${brandColor("created")} open-next.config.ts file`); |
| 130 | +} |
0 commit comments