Skip to content

Commit 1aa668e

Browse files
fixup! create a wrangler.toml file for the user in case one is not already present
create a jsonc file instead of a toml one and check existence of any wrangler config file
1 parent a28f997 commit 1aa668e

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

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

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
/compatibility_date = "\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+
/"compatibility_date": "\d{4}-\d{2}-\d{2}"/,
227+
`"compatibility_date": ${JSON.stringify(compatDate)}`
228+
);
224229
}
230+
231+
writeFileSync(wranglerConfigPath, wranglerConfigContent);
225232
}
226233

227234
function getAppNameFromPackageJson(sourceDir: string): string | undefined {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"main": ".open-next/worker.js",
3+
"name": "app-name",
4+
"compatibility_date": "2024-12-30",
5+
"compatibility_flags": [
6+
"nodejs_compat"
7+
],
8+
"assets": {
9+
"directory": ".open-next/assets",
10+
"binding": "ASSETS"
11+
}
12+
}

packages/cloudflare/templates/defaults/wrangler.toml

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)