@@ -37,7 +37,7 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
3737 const require = createRequire ( import . meta. url ) ;
3838 const openNextDistDir = dirname ( require . resolve ( "@opennextjs/aws/index.js" ) ) ;
3939
40- await createWranglerTomlIfNotExistent ( projectOpts ) ;
40+ await createWranglerConfigIfNotExistent ( projectOpts ) ;
4141
4242 await createOpenNextConfigIfNotExistent ( projectOpts ) ;
4343
@@ -182,46 +182,53 @@ function ensureCloudflareConfig(config: OpenNextConfig) {
182182}
183183
184184/**
185- * Creates a `wrangler.toml ` file for the user if it doesn't exist, but only after asking for the user's confirmation.
185+ * Creates a `wrangler.jsonc ` file for the user if it doesn't exist, but only after asking for the user's confirmation.
186186 *
187187 * If the user refuses an error is thrown (since the file is mandatory).
188188 *
189189 * @param projectOpts The options for the project
190190 */
191- async function createWranglerTomlIfNotExistent ( projectOpts : ProjectOptions ) : Promise < void > {
192- const wranglerTomlPath = join ( projectOpts . sourceDir , "wrangler.toml" ) ;
191+ async function createWranglerConfigIfNotExistent ( projectOpts : ProjectOptions ) : Promise < void > {
192+ const possibleExts = [ "toml" , "json" , "jsonc" ] ;
193+
194+ const wranglerConfigFileExists = possibleExts . some ( ( ext ) =>
195+ existsSync ( join ( projectOpts . sourceDir , `wrangler.${ ext } ` ) )
196+ ) ;
197+ if ( wranglerConfigFileExists ) {
198+ return ;
199+ }
193200
194- if ( ! existsSync ( wranglerTomlPath ) ) {
195- const answer = await askConfirmation ( "Missing required `wrangler.toml` file, do you want to create one?" ) ;
201+ const wranglerConfigPath = join ( projectOpts . sourceDir , "wrangler.jsonc" ) ;
196202
197- if ( ! answer ) {
198- throw new Error ( "The `wrangler.toml` file is required, aborting!" ) ;
199- }
203+ const answer = await askConfirmation ( "Missing required Wrangler config file, do you want to create one?" ) ;
200204
201- const wranglerTomlTemplate = readFileSync (
202- join ( getPackageTemplatesDirPath ( ) , "defaults" , "wrangler.toml" ) ,
203- "utf8"
204- ) ;
205- let wranglerTomlContent = wranglerTomlTemplate ;
206-
207- const appName = getAppNameFromPackageJson ( projectOpts . sourceDir ) ?? "app-name" ;
208- if ( appName ) {
209- wranglerTomlContent = wranglerTomlContent . replace (
210- '"app-name"' ,
211- JSON . stringify ( appName . replaceAll ( "_" , "-" ) )
212- ) ;
213- }
205+ if ( ! answer ) {
206+ console . warn ( "No Wrangler config file created" ) ;
207+ }
214208
215- const compatDate = await getLatestCompatDate ( ) ;
216- if ( compatDate ) {
217- wranglerTomlContent = wranglerTomlContent . replace (
218- / c o m p a t i b i l i t y _ d a t e = " \d { 4 } - \d { 2 } - \d { 2 } " / ,
219- `compatibility_date = ${ JSON . stringify ( compatDate ) } `
220- ) ;
221- }
209+ const wranglerConfigTemplate = readFileSync (
210+ join ( getPackageTemplatesDirPath ( ) , "defaults" , "wrangler.jsonc" ) ,
211+ "utf8"
212+ ) ;
213+ let wranglerConfigContent = wranglerConfigTemplate ;
214+
215+ const appName = getAppNameFromPackageJson ( projectOpts . sourceDir ) ?? "app-name" ;
216+ if ( appName ) {
217+ wranglerConfigContent = wranglerConfigContent . replace (
218+ '"app-name"' ,
219+ JSON . stringify ( appName . replaceAll ( "_" , "-" ) )
220+ ) ;
221+ }
222222
223- writeFileSync ( wranglerTomlPath , wranglerTomlContent ) ;
223+ const compatDate = await getLatestCompatDate ( ) ;
224+ if ( compatDate ) {
225+ wranglerConfigContent = wranglerConfigContent . replace (
226+ / " c o m p a t i b i l i t y _ d a t e " : " \d { 4 } - \d { 2 } - \d { 2 } " / ,
227+ `"compatibility_date": ${ JSON . stringify ( compatDate ) } `
228+ ) ;
224229 }
230+
231+ writeFileSync ( wranglerConfigPath , wranglerConfigContent ) ;
225232}
226233
227234function getAppNameFromPackageJson ( sourceDir : string ) : string | undefined {
0 commit comments