1- import { cpSync } from "node:fs" ;
1+ import { cpSync , existsSync } from "node:fs" ;
22import { createRequire } from "node:module" ;
33import { dirname , join } from "node:path" ;
44
@@ -11,6 +11,7 @@ import * as buildHelper from "@opennextjs/aws/build/helper.js";
1111import { printHeader , showWarningOnWindows } from "@opennextjs/aws/build/utils.js" ;
1212import logger from "@opennextjs/aws/logger.js" ;
1313import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js" ;
14+ import Enquirer from "enquirer" ;
1415
1516import type { ProjectOptions } from "../config" ;
1617import { containsDotNextDir , getConfig } from "../config" ;
@@ -34,6 +35,8 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
3435 const require = createRequire ( import . meta. url ) ;
3536 const openNextDistDir = dirname ( require . resolve ( "@opennextjs/aws/index.js" ) ) ;
3637
38+ await createOpenNextConfigIfNotExistent ( baseDir ) ;
39+
3740 const { config, buildDir } = await compileOpenNextConfig ( baseDir ) ;
3841
3942 ensureCloudflareConfig ( config ) ;
@@ -94,6 +97,34 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
9497 logger . info ( "OpenNext build complete." ) ;
9598}
9699
100+ /**
101+ * Creates a `open-next.config.ts` file for the user if it doesn't exist, but only after getting the user's confirmation.
102+ *
103+ * If the users refuses an error is thrown (since the file is mandatory).
104+ *
105+ * @param baseDir the Next.js app root folder
106+ */
107+ async function createOpenNextConfigIfNotExistent ( baseDir : string ) : Promise < void > {
108+ const openNextConfigPath = join ( baseDir , "open-next.config.ts" ) ;
109+
110+ if ( ! existsSync ( openNextConfigPath ) ) {
111+ const questionName = "create-open-next-config" ;
112+ const answer = (
113+ ( await Enquirer . prompt ( {
114+ name : "create-open-next-config" ,
115+ message : "Missing required `open-next.config.ts` file, do you want to create one?" ,
116+ type : "confirm" ,
117+ initial : "y" ,
118+ } ) ) as { [ questionName ] : boolean }
119+ ) [ questionName ] ;
120+ if ( ! answer ) {
121+ throw new Error ( "A `open-next.config.ts` file is mandatory, aborting!" ) ;
122+ }
123+
124+ cpSync ( `${ import . meta. dirname } /templates/defaults/open-next.config.ts` , openNextConfigPath ) ;
125+ }
126+ }
127+
97128/**
98129 * Ensures open next is configured for cloudflare.
99130 *
0 commit comments