Skip to content

Commit 4fe93de

Browse files
feat(create-cloudflare): Add support for wrangler.json(c) in templates (#7484)
- If wrangler.toml is missing, check for wrangler.json(c) before failing - All templates in the cloudflare/templates repository use wrangler.json - We need to support these templates in C3 before public launch
1 parent a98dfa0 commit 4fe93de

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

.changeset/hot-ads-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": minor
3+
---
4+
5+
feature: Add support for wrangler.json(c) in templates

packages/create-cloudflare/e2e-tests/cli.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,30 @@ describe.skipIf(experimental || frameworkToTest || isQuarantineMode())(
217217
},
218218
);
219219

220+
test({ experimental }).skipIf(process.platform === "win32")(
221+
"Cloning remote template that uses wrangler.json",
222+
async ({ logStream, project }) => {
223+
const { output } = await runC3(
224+
[
225+
project.path,
226+
"--template=cloudflare/templates/multiplayer-globe-template",
227+
"--no-deploy",
228+
"--git=false",
229+
],
230+
[],
231+
logStream,
232+
);
233+
234+
expect(output).toContain(
235+
`repository cloudflare/templates/multiplayer-globe-template`,
236+
);
237+
expect(output).toContain(
238+
`Cloning template from: cloudflare/templates/multiplayer-globe-template`,
239+
);
240+
expect(output).toContain(`template cloned and validated`);
241+
},
242+
);
243+
220244
test({ experimental }).skipIf(process.platform === "win32")(
221245
"Inferring the category, type and language if the type is `hello-world-python`",
222246
async ({ logStream, project }) => {

packages/create-cloudflare/src/deploy.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import { quoteShellArgs, runCommand } from "helpers/command";
1010
import { readFile } from "helpers/files";
1111
import { detectPackageManager } from "helpers/packageManagers";
1212
import { poll } from "helpers/poll";
13+
import { parse as jsoncParse } from "jsonc-parser";
1314
import { isInsideGitRepo } from "./git";
1415
import { chooseAccount, wranglerLogin } from "./wrangler/accounts";
15-
import { readWranglerToml } from "./wrangler/config";
16+
import {
17+
readWranglerJson,
18+
readWranglerToml,
19+
wranglerJsonExists,
20+
} from "./wrangler/config";
1621
import type { C3Context } from "types";
1722

1823
export const offerToDeploy = async (ctx: C3Context) => {
@@ -72,12 +77,17 @@ const isDeployable = async (ctx: C3Context) => {
7277
if (ctx.template.platform === "pages") {
7378
return true;
7479
}
80+
const wranglerConfig = readWranglerConfig(ctx);
81+
return !hasBinding(wranglerConfig);
82+
};
7583

84+
const readWranglerConfig = (ctx: C3Context) => {
85+
if (wranglerJsonExists(ctx)) {
86+
const wranglerJsonStr = readWranglerJson(ctx);
87+
return jsoncParse(wranglerJsonStr);
88+
}
7689
const wranglerTomlStr = readWranglerToml(ctx);
77-
78-
const wranglerToml = TOML.parse(wranglerTomlStr.replace(/\r\n/g, "\n"));
79-
80-
return !hasBinding(wranglerToml);
90+
return TOML.parse(wranglerTomlStr.replace(/\r\n/g, "\n"));
8191
};
8292

8393
export const runDeploy = async (ctx: C3Context) => {

packages/create-cloudflare/src/templates.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,15 @@ const validateTemplate = (path: string, config: TemplateConfig) => {
632632
const validateTemplateSrcDirectory = (path: string, config: TemplateConfig) => {
633633
if (config.platform === "workers") {
634634
const wranglerTomlPath = resolve(path, "wrangler.toml");
635-
if (!existsSync(wranglerTomlPath)) {
635+
const wranglerJsonPath = resolve(path, "wrangler.json");
636+
const wranglerJsoncPath = resolve(path, "wrangler.jsonc");
637+
if (
638+
!existsSync(wranglerTomlPath) &&
639+
!existsSync(wranglerJsonPath) &&
640+
!existsSync(wranglerJsoncPath)
641+
) {
636642
throw new Error(
637-
`create-cloudflare templates must contain a "wrangler.toml" file.`,
643+
`create-cloudflare templates must contain a "wrangler.toml" or "wrangler.json(c)" file.`,
638644
);
639645
}
640646
}

packages/create-cloudflare/src/wrangler/config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,39 @@ const getWranglerTomlPath = (ctx: C3Context) => {
4949
return resolve(ctx.project.path, "wrangler.toml");
5050
};
5151

52+
const getWranglerJsonPath = (ctx: C3Context) => {
53+
return resolve(ctx.project.path, "wrangler.json");
54+
};
55+
56+
const getWranglerJsoncPath = (ctx: C3Context) => {
57+
return resolve(ctx.project.path, "wrangler.jsonc");
58+
};
59+
5260
export const wranglerTomlExists = (ctx: C3Context) => {
5361
const wranglerTomlPath = getWranglerTomlPath(ctx);
5462
return existsSync(wranglerTomlPath);
5563
};
5664

65+
export const wranglerJsonExists = (ctx: C3Context) => {
66+
const wranglerJsonPath = getWranglerJsonPath(ctx);
67+
const wranglerJsoncPath = getWranglerJsoncPath(ctx);
68+
return existsSync(wranglerJsonPath) || existsSync(wranglerJsoncPath);
69+
};
70+
5771
export const readWranglerToml = (ctx: C3Context) => {
5872
const wranglerTomlPath = getWranglerTomlPath(ctx);
5973
return readFile(wranglerTomlPath);
6074
};
6175

76+
export const readWranglerJson = (ctx: C3Context) => {
77+
const wranglerJsonPath = getWranglerJsonPath(ctx);
78+
if (existsSync(wranglerJsonPath)) {
79+
return readFile(wranglerJsonPath);
80+
}
81+
const wranglerJsoncPath = getWranglerJsoncPath(ctx);
82+
return readFile(wranglerJsoncPath);
83+
};
84+
6285
export const writeWranglerToml = (ctx: C3Context, contents: string) => {
6386
const wranglerTomlPath = getWranglerTomlPath(ctx);
6487
return writeFile(wranglerTomlPath, contents);

0 commit comments

Comments
 (0)