Skip to content

Commit 5b80895

Browse files
committed
set env interface and types path in one place
1 parent 4ea8163 commit 5b80895

File tree

10 files changed

+47
-96
lines changed

10 files changed

+47
-96
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ describe.concurrent(
176176
project.path,
177177
logStream,
178178
);
179-
await verifyBuildCfTypesScript(testConfig, project.path, logStream);
179+
await verifyBuildCfTypesScript(
180+
frameworkConfig,
181+
project.path,
182+
logStream,
183+
);
180184
await verifyBuildScript(testConfig, project.path, logStream);
181185
} catch (e) {
182186
console.error("ERROR", e);
@@ -353,11 +357,11 @@ const verifyPreviewScript = async (
353357
};
354358

355359
const verifyBuildCfTypesScript = async (
356-
{ verifyBuildCfTypes }: FrameworkTestConfig,
360+
{ workersTypes, typesPath, envInterfaceName }: TemplateConfig,
357361
projectPath: string,
358362
logStream: Writable,
359363
) => {
360-
if (!verifyBuildCfTypes) {
364+
if (workersTypes === "none") {
361365
return;
362366
}
363367

@@ -370,23 +374,27 @@ const verifyBuildCfTypesScript = async (
370374

371375
await waitForExit(buildTypesProc);
372376

373-
const { outputFile, envInterfaceName } = verifyBuildCfTypes;
374-
375-
const outputFileContent = readFile(join(projectPath, outputFile)).split("\n");
377+
const outputFileContent = readFile(
378+
join(projectPath, typesPath ?? "worker-configuration.d.ts"),
379+
).split("\n");
376380

377381
// the file contains the env interface
378382
const hasEnvInterfacePre = outputFileContent.some(
379383
(line) =>
380384
// old type gen - some framework templates pin older versions of wrangler
381-
line === `interface ${envInterfaceName} {` ||
385+
line === `interface ${envInterfaceName ?? "Env"} {` ||
382386
// new after importable env change
383-
line === `interface ${envInterfaceName} extends Cloudflare.Env {}`,
387+
line ===
388+
`interface ${envInterfaceName ?? "Env"} extends Cloudflare.Env {}`,
384389
);
385390
expect(hasEnvInterfacePre).toBe(true);
386391

387-
expect(outputFileContent[2]).match(
388-
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/,
389-
);
392+
// if the types were installed, only the Env types will be present
393+
if (workersTypes === "generated") {
394+
expect(outputFileContent[2]).match(
395+
/\/\/ Runtime types generated with workerd@1\.\d{8}\.\d \d{4}-\d{2}-\d{2} ([a-z_]+,?)*/,
396+
);
397+
}
390398
};
391399

392400
const verifyBuildScript = async (

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

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ export default function getFrameworkTestConfig(pm: string) {
1616
expectedText: "Hello from Cloudflare",
1717
previewArgs: ["--host=127.0.0.1"],
1818
},
19-
verifyBuildCfTypes: {
20-
outputFile: "worker-configuration.d.ts",
21-
envInterfaceName: "Env",
22-
},
2319
flags: ["--no-install", "--no-git-init"],
2420
},
2521
"astro:pages": {
@@ -40,10 +36,6 @@ export default function getFrameworkTestConfig(pm: string) {
4036
route: "/test",
4137
expectedText: "C3_TEST",
4238
},
43-
verifyBuildCfTypes: {
44-
outputFile: "worker-configuration.d.ts",
45-
envInterfaceName: "Env",
46-
},
4739
flags: [
4840
"--skip-houston",
4941
"--no-install",
@@ -72,10 +64,6 @@ export default function getFrameworkTestConfig(pm: string) {
7264
route: "/test",
7365
expectedText: "C3_TEST",
7466
},
75-
verifyBuildCfTypes: {
76-
outputFile: "worker-configuration.d.ts",
77-
envInterfaceName: "Env",
78-
},
7967
flags: [
8068
"--skip-houston",
8169
"--no-install",
@@ -150,10 +138,6 @@ export default function getFrameworkTestConfig(pm: string) {
150138
route: "/api/v1/test",
151139
expectedText: "C3_TEST",
152140
},
153-
verifyBuildCfTypes: {
154-
outputFile: "worker-configuration.d.ts",
155-
envInterfaceName: "Env",
156-
},
157141
verifyBuild: {
158142
outputDir: "./dist/analog/public",
159143
script: "build",
@@ -192,10 +176,6 @@ export default function getFrameworkTestConfig(pm: string) {
192176
route: "/",
193177
expectedText: "Congratulations! Your app is running.",
194178
},
195-
verifyBuildCfTypes: {
196-
outputFile: "worker-configuration.d.ts",
197-
envInterfaceName: "Env",
198-
},
199179
flags: ["--style", "sass"],
200180
},
201181
"gatsby:pages": {
@@ -250,10 +230,6 @@ export default function getFrameworkTestConfig(pm: string) {
250230
route: "/",
251231
expectedText: "Hello!",
252232
},
253-
verifyBuildCfTypes: {
254-
outputFile: "worker-configuration.d.ts",
255-
envInterfaceName: "CloudflareBindings",
256-
},
257233
promptHandlers: [
258234
{
259235
matcher: /Do you want to install project dependencies\?/,
@@ -273,10 +249,6 @@ export default function getFrameworkTestConfig(pm: string) {
273249
route: "/message",
274250
expectedText: "Hello Hono!",
275251
},
276-
verifyBuildCfTypes: {
277-
outputFile: "worker-configuration.d.ts",
278-
envInterfaceName: "CloudflareBindings",
279-
},
280252
promptHandlers: [
281253
{
282254
matcher: /Do you want to install project dependencies\?/,
@@ -374,10 +346,6 @@ export default function getFrameworkTestConfig(pm: string) {
374346
},
375347
],
376348
testCommitMessage: true,
377-
verifyBuildCfTypes: {
378-
outputFile: "env.d.ts",
379-
envInterfaceName: "CloudflareEnv",
380-
},
381349
verifyDeploy: {
382350
route: "/",
383351
expectedText: "Create Next App",
@@ -414,10 +382,6 @@ export default function getFrameworkTestConfig(pm: string) {
414382
"@/*",
415383
"--src-dir",
416384
],
417-
verifyBuildCfTypes: {
418-
outputFile: "cloudflare-env.d.ts",
419-
envInterfaceName: "CloudflareEnv",
420-
},
421385
verifyPreview: {
422386
previewArgs: ["--"],
423387
route: "/test",
@@ -455,10 +419,6 @@ export default function getFrameworkTestConfig(pm: string) {
455419
route: "/test",
456420
expectedText: "C3_TEST",
457421
},
458-
verifyBuildCfTypes: {
459-
outputFile: "worker-configuration.d.ts",
460-
envInterfaceName: "Env",
461-
},
462422
verifyBuild: {
463423
outputDir: "./dist",
464424
script: "build",
@@ -486,10 +446,6 @@ export default function getFrameworkTestConfig(pm: string) {
486446
route: "/test",
487447
expectedText: "C3_TEST",
488448
},
489-
verifyBuildCfTypes: {
490-
outputFile: "worker-configuration.d.ts",
491-
envInterfaceName: "Env",
492-
},
493449
},
494450
"react:pages": {
495451
argv: ["--platform", "pages"],
@@ -510,10 +466,6 @@ export default function getFrameworkTestConfig(pm: string) {
510466
route: "/",
511467
expectedText: "Vite + React",
512468
},
513-
verifyBuildCfTypes: {
514-
outputFile: "worker-configuration.d.ts",
515-
envInterfaceName: "Env",
516-
},
517469
},
518470
"react:workers": {
519471
argv: ["--platform", "workers"],
@@ -542,10 +494,6 @@ export default function getFrameworkTestConfig(pm: string) {
542494
// not actually running the client side JS.
543495
expectedText: "Vite + React + TS",
544496
},
545-
verifyBuildCfTypes: {
546-
outputFile: "worker-configuration.d.ts",
547-
envInterfaceName: "Env",
548-
},
549497
},
550498
solid: {
551499
promptHandlers: [
@@ -570,10 +518,6 @@ export default function getFrameworkTestConfig(pm: string) {
570518
route: "/",
571519
expectedText: "Hello world",
572520
},
573-
verifyBuildCfTypes: {
574-
outputFile: "worker-configuration.d.ts",
575-
envInterfaceName: "Env",
576-
},
577521
},
578522
"svelte:pages": {
579523
argv: ["--platform", "pages"],
@@ -602,10 +546,6 @@ export default function getFrameworkTestConfig(pm: string) {
602546
route: "/test",
603547
expectedText: "C3_TEST",
604548
},
605-
verifyBuildCfTypes: {
606-
outputFile: "./src/worker-configuration.d.ts",
607-
envInterfaceName: "Env",
608-
},
609549
},
610550
"svelte:workers": {
611551
argv: ["--platform", "workers"],
@@ -628,10 +568,6 @@ export default function getFrameworkTestConfig(pm: string) {
628568
route: "/test",
629569
expectedText: "C3_TEST",
630570
},
631-
verifyBuildCfTypes: {
632-
outputFile: "./src/worker-configuration.d.ts",
633-
envInterfaceName: "Env",
634-
},
635571
},
636572
"vue:pages": {
637573
argv: ["--platform", "pages"],
@@ -645,10 +581,6 @@ export default function getFrameworkTestConfig(pm: string) {
645581
route: "/",
646582
expectedText: "Vite App",
647583
},
648-
verifyBuildCfTypes: {
649-
outputFile: "worker-configuration.d.ts",
650-
envInterfaceName: "Env",
651-
},
652584
flags: ["--ts"],
653585
},
654586
"vue:workers": {
@@ -664,10 +596,6 @@ export default function getFrameworkTestConfig(pm: string) {
664596
route: "/",
665597
expectedText: "Vite App",
666598
},
667-
verifyBuildCfTypes: {
668-
outputFile: "worker-configuration.d.ts",
669-
envInterfaceName: "Env",
670-
},
671599
},
672600
};
673601
}

packages/create-cloudflare/src/templates.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export type TemplateConfig = {
144144

145145
/** The file path to the generated types file. Defaults to worker-configuration.d.ts*/
146146
typesPath?: string;
147+
/** The name of the Env type generated by wrangler types. Defaults to `Env`*/
148+
envInterfaceName?: string;
147149

148150
/** The file path of the template. This is used internally and isn't a user facing config value.*/
149151
path?: string;
@@ -614,6 +616,7 @@ export const createContext = async (
614616
// set default to generate types
615617
template.workersTypes ??= "generated";
616618
template.typesPath ??= "worker-configuration.d.ts";
619+
template.envInterfaceName ??= "Env";
617620
return {
618621
project: { name, path },
619622
// We need to maintain a reference to the original args

packages/create-cloudflare/src/workers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ export async function updateTsConfig(ctx: C3Context) {
8383
);
8484
newTypes.push(typesEntrypoint);
8585
}
86-
}
87-
if (ctx.template.workersTypes === "generated") {
86+
} else if (ctx.template.workersTypes === "generated") {
8887
newTypes.push(ctx.template.typesPath ?? "./worker-configuration.d.ts");
8988
// if generated types include runtime types, remove @cloudflare/workers-types
9089
const typegen = readFile(

packages/create-cloudflare/templates/hono/pages/c3.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const generate = async (ctx: C3Context) => {
1919
logRaw(""); // newline
2020
};
2121

22+
const envInterfaceName = "CloudflareBindings";
23+
2224
const config: TemplateConfig = {
2325
configVersion: 1,
2426
id: "hono",
@@ -33,12 +35,13 @@ const config: TemplateConfig = {
3335
generate,
3436
transformPackageJson: async () => ({
3537
scripts: {
36-
"cf-typegen": "wrangler types --env-interface CloudflareBindings",
38+
"cf-typegen": `wrangler types --env-interface ${envInterfaceName}`,
3739
preview: "vite build && wrangler pages dev",
3840
},
3941
}),
4042
devScript: "dev",
4143
deployScript: "deploy",
4244
previewScript: "preview",
45+
envInterfaceName,
4346
};
4447
export default config;

packages/create-cloudflare/templates/hono/workers/c3.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const generate = async (ctx: C3Context) => {
1919
logRaw(""); // newline
2020
};
2121

22+
const envInterfaceName = "CloudflareBindings";
2223
const config: TemplateConfig = {
2324
configVersion: 1,
2425
id: "hono",
@@ -32,11 +33,12 @@ const config: TemplateConfig = {
3233
generate,
3334
transformPackageJson: async () => ({
3435
scripts: {
35-
"cf-typegen": "wrangler types --env-interface CloudflareBindings",
36+
"cf-typegen": `wrangler types --env-interface ${envInterfaceName}`,
3637
},
3738
}),
3839
devScript: "dev",
3940
deployScript: "deploy",
4041
previewScript: "dev",
42+
envInterfaceName,
4143
};
4244
export default config;

packages/create-cloudflare/templates/next/pages/c3.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ const addDevDependencies = async (installEslintPlugin: boolean) => {
155155
});
156156
};
157157

158+
const envInterfaceName = "CloudflareEnv";
159+
const typesPath = "./env.d.ts";
160+
158161
export default {
159162
configVersion: 1,
160163
id: "next",
@@ -212,7 +215,7 @@ export default {
212215
preview: `${pagesBuildRunCommand} && wrangler pages dev`,
213216
deploy: `${pagesBuildRunCommand} && wrangler pages deploy`,
214217
...(usesTypescript(ctx) && {
215-
"cf-typegen": `wrangler types --env-interface CloudflareEnv env.d.ts`,
218+
"cf-typegen": `wrangler types --env-interface ${envInterfaceName} ${typesPath}`,
216219
}),
217220
},
218221
};
@@ -221,5 +224,6 @@ export default {
221224
previewScript: "preview",
222225
deployScript: "deploy",
223226
compatibilityFlags: ["nodejs_compat"],
224-
typesPath: "./env.d.ts",
227+
typesPath,
228+
envInterfaceName,
225229
} as TemplateConfig;

packages/create-cloudflare/templates/next/workers/c3.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const updateNextConfig = (usesTs: boolean) => {
4242
s.stop(`${brandColor(`updated`)} ${dim(`\`${configFile}\``)}`);
4343
};
4444

45+
const envInterfaceName = "CloudflareBindings";
46+
const typesPath = "./cloudflare-env.d.ts";
4547
export default {
4648
configVersion: 1,
4749
id: "next",
@@ -59,13 +61,13 @@ export default {
5961
scripts: {
6062
deploy: `opennextjs-cloudflare build && opennextjs-cloudflare deploy`,
6163
preview: `opennextjs-cloudflare build && opennextjs-cloudflare preview`,
62-
"cf-typegen": `wrangler types --env-interface CloudflareEnv ./cloudflare-env.d.ts`,
64+
"cf-typegen": `wrangler types --env-interface ${envInterfaceName} ${typesPath}`,
6365
},
6466
}),
6567
devScript: "dev",
6668
previewScript: "preview",
6769
deployScript: "deploy",
68-
69-
typesPath: "./cloudflare-env.d.ts",
70+
typesPath,
71+
envInterfaceName,
7072
compatibilityFlags: ["nodejs_compat"],
7173
} as TemplateConfig;

0 commit comments

Comments
 (0)