Skip to content

Commit f9d7aeb

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

File tree

6 files changed

+833
-746
lines changed

6 files changed

+833
-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: 28 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

@@ -12,8 +12,10 @@ import { printHeader, showWarningOnWindows } from "@opennextjs/aws/build/utils.j
1212
import logger from "@opennextjs/aws/logger.js";
1313
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
1414

15+
import { getPackageTemplatesDirPath } from "../../utils/get-package-templates-dir-path.js";
1516
import type { ProjectOptions } from "../config.js";
1617
import { containsDotNextDir, getConfig } from "../config.js";
18+
import { askConfirmation } from "../utils/ask-confirmation.js";
1719
import { bundleServer } from "./bundle-server.js";
1820
import { compileEnvFiles } from "./open-next/compile-env-files.js";
1921
import { copyCacheAssets } from "./open-next/copyCacheAssets.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,29 @@ 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 asking for the user's confirmation.
109+
*
110+
* If the user 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 answer = await askConfirmation(
119+
"Missing required `open-next.config.ts` file, do you want to create one?"
120+
);
121+
122+
if (!answer) {
123+
throw new Error("The `open-next.config.ts` file is required, aborting!");
124+
}
125+
126+
cpSync(join(getPackageTemplatesDirPath(), "defaults", "open-next.config.ts"), openNextConfigPath);
127+
}
128+
}
129+
103130
/**
104131
* Ensures open next is configured for cloudflare.
105132
*
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Enquirer from "enquirer";
2+
3+
export async function askConfirmation(message: string): Promise<boolean> {
4+
const questionName = crypto.randomUUID();
5+
6+
const enquirerAnswersObject = await Enquirer.prompt<Record<string, boolean>>({
7+
name: questionName,
8+
message,
9+
type: "confirm",
10+
initial: "y",
11+
});
12+
13+
console.log("");
14+
15+
const answer = !!enquirerAnswersObject[questionName];
16+
return answer;
17+
}
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// default open-next.config.ts file created by @opennextjs/cloudflare
2+
3+
const config = {
4+
default: {
5+
override: {
6+
wrapper: "cloudflare-node",
7+
converter: "edge",
8+
incrementalCache: "dummy",
9+
tagCache: "dummy",
10+
queue: "dummy",
11+
},
12+
},
13+
14+
middleware: {
15+
external: true,
16+
override: {
17+
wrapper: "cloudflare-edge",
18+
converter: "edge",
19+
proxyExternalRequest: "fetch",
20+
},
21+
},
22+
23+
dangerous: {
24+
enableCacheInterception: false,
25+
},
26+
};
27+
28+
export default config;

0 commit comments

Comments
 (0)