Skip to content

Commit ac17888

Browse files
committed
pr feedback
1 parent a751bda commit ac17888

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

packages/create-cloudflare/src/workers.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync } from "fs";
1+
import { existsSync, read } from "fs";
22
import { join, resolve } from "path";
33
import { warn } from "@cloudflare/cli";
44
import { brandColor, dim } from "@cloudflare/cli/colors";
@@ -11,7 +11,7 @@ import * as jsonc from "jsonc-parser";
1111
import type { C3Context, PackageJson } from "types";
1212

1313
/**
14-
* Generate types using `wrangler types` and update tsconfig
14+
* Generate types using the `cf-typegen` script and update tsconfig
1515
*/
1616

1717
export async function generateWorkersTypes(ctx: C3Context) {
@@ -31,10 +31,6 @@ export async function generateWorkersTypes(ctx: C3Context) {
3131
await runCommand(typesCmd, {
3232
cwd: ctx.project.path,
3333
silent: true,
34-
env: {
35-
CLOUDFLARE_ACCOUNT_ID: ctx.account?.id,
36-
NODE_ENV: "production",
37-
},
3834
startText: "Generating types for your application",
3935
doneText: `${brandColor("generated")} ${dim(`to \`${ctx.template.typesPath ?? "worker-configuration.d.ts"}\` via \`${typesCmd.join(" ")}\``)}`,
4036
});
@@ -53,6 +49,13 @@ export async function generateWorkersTypes(ctx: C3Context) {
5349
await updateTsConfig(ctx);
5450
}
5551

52+
/**
53+
* update `types` in tsconfig:
54+
* - set workers-types to latest entrypoint if installed
55+
* - remove workers-types if runtime types have been generated
56+
* - add generated types file if types were generated
57+
* - add node if node compat
58+
*/
5659
export async function updateTsConfig(ctx: C3Context) {
5760
const tsconfigPath = join(ctx.project.path, "tsconfig.json");
5861
if (!existsSync(tsconfigPath)) {
@@ -64,7 +67,7 @@ export async function updateTsConfig(ctx: C3Context) {
6467
try {
6568
const config = jsonc.parse(tsconfig);
6669
const currentTypes = config.compilerOptions?.types ?? [];
67-
let newTypes: string[];
70+
const newTypes: string[] = currentTypes;
6871
if (ctx.template.installWorkersTypes) {
6972
const entrypointVersion = getLatestTypesEntrypoint(ctx);
7073
if (entrypointVersion === null) {
@@ -76,24 +79,30 @@ export async function updateTsConfig(ctx: C3Context) {
7679
);
7780
// If a type declaration with an explicit entrypoint exists, leave the types as is
7881
// Otherwise, add the latest entrypoint
79-
newTypes = explicitEntrypoint
80-
? [...currentTypes]
81-
: [
82-
...currentTypes.filter(
83-
(t: string) => t !== "@cloudflare/workers-types",
84-
),
85-
typesEntrypoint,
86-
];
87-
} else {
88-
newTypes = [
89-
...currentTypes.filter(
90-
(t: string) => !t.startsWith("@cloudflare/workers-types"),
91-
),
82+
if (!explicitEntrypoint) {
83+
newTypes
84+
.filter((t: string) => t !== "@cloudflare/workers-types")
85+
.push(typesEntrypoint);
86+
}
87+
}
88+
if (!ctx.template.skipWranglerTypegen) {
89+
// if generated types include runtime types, remove workers-types
90+
const typegen = readFile(
9291
ctx.template.typesPath ?? "./worker-configuration.d.ts",
93-
...(ctx.template.compatibilityFlags?.includes("nodejs_compat")
94-
? ["node"]
95-
: []),
96-
];
92+
).split("\n");
93+
if (
94+
typegen.some((line) =>
95+
line.includes("// Runtime types generated with workerd"),
96+
)
97+
) {
98+
newTypes.filter(
99+
(t: string) => !t.startsWith("@cloudflare/workers-types"),
100+
);
101+
}
102+
if (ctx.template.compatibilityFlags?.includes("nodejs_compat")) {
103+
newTypes.push("node");
104+
}
105+
newTypes.push(ctx.template.typesPath ?? "./worker-configuration.d.ts");
97106
}
98107
if (newTypes.sort() === currentTypes.sort()) {
99108
return;

0 commit comments

Comments
 (0)