Skip to content

Commit ab92f83

Browse files
authored
Output .json by default in C3 (#7676)
* Output .json by default * Create young-apes-battle.md * Address comments * Add more comments * Add more comments * sort json config files * Enable trailing commas * fix e2e * fix e2e again * formatting * Always install latest Wrangler * Don't overwrite the wrangler config that remix already provides * fix unit tests * Add back remix .toml file
1 parent 5c2c55a commit ab92f83

File tree

94 files changed

+935
-2584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+935
-2584
lines changed

.changeset/young-apes-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-cloudflare": patch
3+
---
4+
5+
Output the `wrangler.json` config format when running `create-cloudflare`

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { existsSync } from "fs";
22
import { cp } from "fs/promises";
33
import { join } from "path";
4-
import { readFile, readToml, writeToml } from "helpers/files";
4+
import {
5+
readFile,
6+
readJSON,
7+
readToml,
8+
writeJSON,
9+
writeToml,
10+
} from "helpers/files";
511
import { detectPackageManager } from "helpers/packageManagers";
612
import { retry } from "helpers/retry";
713
import { sleep } from "helpers/sleep";
@@ -764,17 +770,22 @@ const runCli = async (
764770
*/
765771
const addTestVarsToWranglerToml = async (projectPath: string) => {
766772
const wranglerTomlPath = join(projectPath, "wrangler.toml");
767-
let wranglerToml: JsonMap = {};
768-
const wranglerTomlExists = existsSync(wranglerTomlPath);
769-
if (wranglerTomlExists) {
770-
wranglerToml = readToml(wranglerTomlPath);
773+
const wranglerJsonPath = join(projectPath, "wrangler.json");
774+
if (existsSync(wranglerTomlPath)) {
775+
const wranglerToml = readToml(wranglerTomlPath);
776+
// Add a TEST var to the wrangler.toml
777+
wranglerToml.vars ??= {};
778+
(wranglerToml.vars as JsonMap).TEST = "C3_TEST";
779+
780+
writeToml(wranglerTomlPath, wranglerToml);
781+
} else if (existsSync(wranglerJsonPath)) {
782+
const wranglerJson = readJSON(wranglerJsonPath);
783+
// Add a TEST var to the wrangler.toml
784+
wranglerJson.vars ??= {};
785+
wranglerJson.vars.TEST = "C3_TEST";
786+
787+
writeJSON(wranglerJsonPath, wranglerJson);
771788
}
772-
773-
// Add a TEST var to the wrangler.toml
774-
wranglerToml.vars ??= {};
775-
(wranglerToml.vars as JsonMap).TEST = "C3_TEST";
776-
777-
writeToml(wranglerTomlPath, wranglerToml);
778789
};
779790

780791
const verifyDeployment = async (

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { join } from "path";
2-
import { readToml } from "helpers/files";
2+
import { readJSON, readToml } from "helpers/files";
33
import { detectPackageManager } from "helpers/packageManagers";
44
import { retry } from "helpers/retry";
55
import { sleep } from "helpers/sleep";
@@ -147,10 +147,17 @@ describe
147147
expect(wranglerPath).toExist();
148148

149149
const tomlPath = join(project.path, "wrangler.toml");
150-
expect(tomlPath).toExist();
150+
const jsonPath = join(project.path, "wrangler.json");
151151

152-
const config = readToml(tomlPath) as { main: string };
153-
expect(join(project.path, config.main)).toExist();
152+
try {
153+
expect(jsonPath).toExist();
154+
const config = readJSON(jsonPath) as { main: string };
155+
expect(join(project.path, config.main)).toExist();
156+
} catch {
157+
expect(tomlPath).toExist();
158+
const config = readToml(tomlPath) as { main: string };
159+
expect(join(project.path, config.main)).toExist();
160+
}
154161

155162
const { verifyDeploy } = testConfig;
156163
if (verifyDeploy) {

packages/create-cloudflare/src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
} from "./templates";
3636
import { validateProjectDirectory } from "./validators";
3737
import { installWorkersTypes } from "./workers";
38-
import { updateWranglerToml } from "./wrangler/config";
38+
import { updateWranglerConfig } from "./wrangler/config";
3939
import type { C3Args, C3Context } from "types";
4040

4141
const { npm } = detectPackageManager();
@@ -156,9 +156,9 @@ const configure = async (ctx: C3Context) => {
156156
await installWrangler();
157157
await installWorkersTypes(ctx);
158158

159-
// Note: updateWranglerToml _must_ be called before the configure phase since
159+
// Note: This _must_ be called before the configure phase since
160160
// pre-existing workers assume its presence in their configure phase
161-
await updateWranglerToml(ctx);
161+
await updateWranglerConfig(ctx);
162162

163163
const { template } = ctx;
164164
if (template.configure) {

packages/create-cloudflare/src/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const isDeployable = async (ctx: C3Context) => {
8484
const readWranglerConfig = (ctx: C3Context) => {
8585
if (wranglerJsonExists(ctx)) {
8686
const wranglerJsonStr = readWranglerJson(ctx);
87-
return jsoncParse(wranglerJsonStr);
87+
return jsoncParse(wranglerJsonStr, undefined, { allowTrailingComma: true });
8888
}
8989
const wranglerTomlStr = readWranglerToml(ctx);
9090
return TOML.parse(wranglerTomlStr.replace(/\r\n/g, "\n"));

packages/create-cloudflare/src/helpers/__tests__/packages.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe("Package Helpers", () => {
9191
await installWrangler();
9292

9393
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
94-
["npm", "install", "--save-dev", "wrangler"],
94+
["npm", "install", "--save-dev", "wrangler@latest"],
9595
expect.anything(),
9696
);
9797
});

packages/create-cloudflare/src/helpers/compatDate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { C3Context } from "types";
77

88
/**
99
* Look up the latest release of workerd and use its date as the compatibility_date
10-
* configuration value for wrangler.toml.
10+
* configuration value for a wrangler config file.
1111
*
1212
* If the look up fails then we fall back to a well known date.
1313
*

packages/create-cloudflare/src/helpers/files.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs, { existsSync, statSync } from "fs";
22
import { join } from "path";
33
import TOML from "@iarna/toml";
4+
import { parse } from "jsonc-parser";
45
import type { JsonMap } from "@iarna/toml";
56
import type { C3Context } from "types";
67

@@ -58,7 +59,7 @@ export const directoryExists = (path: string): boolean => {
5859

5960
export const readJSON = (path: string) => {
6061
const contents = readFile(path);
61-
return contents ? JSON.parse(contents) : contents;
62+
return contents ? parse(contents) : contents;
6263
};
6364

6465
export const readToml = (path: string) => {

packages/create-cloudflare/src/helpers/packages.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ export async function getLatestPackageVersion(packageSpecifier: string) {
8989
export const installWrangler = async () => {
9090
const { npm } = detectPackageManager();
9191

92-
// Exit early if already installed
93-
if (existsSync(path.resolve("node_modules", "wrangler"))) {
94-
return;
95-
}
96-
97-
await installPackages([`wrangler`], {
92+
// Even if Wrangler is already installed, make sure we install the latest version, as some framework CLIs are pinned to an older version
93+
await installPackages([`wrangler@latest`], {
9894
dev: true,
9995
startText: `Installing wrangler ${dim(
10096
"A command line tool for building Cloudflare Workers",

0 commit comments

Comments
 (0)