Skip to content

Commit 31c7e9c

Browse files
create an open-next.config.ts file for the user in case one is not already present
1 parent efae3ab commit 31c7e9c

File tree

5 files changed

+819
-746
lines changed

5 files changed

+819
-746
lines changed

packages/cloudflare/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
},
3333
"files": [
3434
"README.md",
35-
"dist"
35+
"dist",
36+
"templates"
3637
],
3738
"repository": {
3839
"type": "git",
@@ -73,7 +74,8 @@
7374
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@684",
7475
"glob": "catalog:",
7576
"rimraf": "catalog:",
76-
"ts-morph": "catalog:"
77+
"ts-morph": "catalog:",
78+
"enquirer": "^2.4.1"
7779
},
7880
"peerDependencies": {
7981
"wrangler": "catalog:"

packages/cloudflare/src/cli/build/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cpSync } from "node:fs";
1+
import { cpSync, existsSync } from "node:fs";
22
import { createRequire } from "node:module";
33
import { dirname, join } from "node:path";
44

@@ -11,7 +11,9 @@ import * as buildHelper from "@opennextjs/aws/build/helper.js";
1111
import { printHeader, showWarningOnWindows } from "@opennextjs/aws/build/utils.js";
1212
import logger from "@opennextjs/aws/logger.js";
1313
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
14+
import Enquirer from "enquirer";
1415

16+
import { getPackageTemplatesDirPath } from "../../utils/get-package-templates-dir-path.js";
1517
import type { ProjectOptions } from "../config.js";
1618
import { containsDotNextDir, getConfig } from "../config.js";
1719
import { bundleServer } from "./bundle-server.js";
@@ -35,6 +37,8 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
3537
const require = createRequire(import.meta.url);
3638
const openNextDistDir = dirname(require.resolve("@opennextjs/aws/index.js"));
3739

40+
await createOpenNextConfigIfNotExistent(baseDir);
41+
3842
const { config, buildDir } = await compileOpenNextConfig(baseDir);
3943

4044
ensureCloudflareConfig(config);
@@ -100,6 +104,34 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
100104
logger.info("OpenNext build complete.");
101105
}
102106

107+
/**
108+
* Creates a `open-next.config.ts` file for the user if it doesn't exist, but only after getting the user's confirmation.
109+
*
110+
* If the users refuses an error is thrown (since the file is mandatory).
111+
*
112+
* @param baseDir the Next.js app root folder
113+
*/
114+
async function createOpenNextConfigIfNotExistent(baseDir: string): Promise<void> {
115+
const openNextConfigPath = join(baseDir, "open-next.config.ts");
116+
117+
if (!existsSync(openNextConfigPath)) {
118+
const questionName = "create-open-next-config";
119+
const answer = (
120+
(await Enquirer.prompt({
121+
name: "create-open-next-config",
122+
message: "Missing required `open-next.config.ts` file, do you want to create one?",
123+
type: "confirm",
124+
initial: "y",
125+
})) as { [questionName]: boolean }
126+
)[questionName];
127+
if (!answer) {
128+
throw new Error("A `open-next.config.ts` file is mandatory, aborting!");
129+
}
130+
131+
cpSync(join(getPackageTemplatesDirPath(), "defaults", "open-next.config.ts"), openNextConfigPath);
132+
}
133+
}
134+
103135
/**
104136
* Ensures open next is configured for cloudflare.
105137
*
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as path from "node:path";
2+
3+
const templatesDirPath = path.resolve(`${import.meta.dirname}/../../templates`);
4+
5+
/**
6+
* Utility for getting the resolved path to the package's templates directory
7+
*
8+
* @returns the resolved path of the templates directory
9+
*/
10+
export function getPackageTemplatesDirPath(): string {
11+
return templatesDirPath;
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const config = {
2+
default: {
3+
override: {
4+
wrapper: "cloudflare-node",
5+
converter: "edge",
6+
incrementalCache: "dummy",
7+
tagCache: "dummy",
8+
queue: "dummy",
9+
},
10+
},
11+
12+
middleware: {
13+
external: true,
14+
override: {
15+
wrapper: "cloudflare-edge",
16+
converter: "edge",
17+
proxyExternalRequest: "fetch",
18+
},
19+
},
20+
21+
dangerous: {
22+
enableCacheInterception: false,
23+
},
24+
};
25+
26+
export default config;

0 commit comments

Comments
 (0)