Skip to content

Commit 4d5aab7

Browse files
committed
fixups
1 parent ac17888 commit 4d5aab7

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe("updateTsConfig", () => {
3838

3939
test("installing workers types", async () => {
4040
ctx.template.installWorkersTypes = true;
41+
ctx.template.skipWranglerTypegen = true;
4142
await updateTsConfig(ctx);
4243

4344
expect(writeFile).toHaveBeenCalled();
@@ -55,6 +56,7 @@ describe("updateTsConfig", () => {
5556

5657
test("latest entrypoint not found", async () => {
5758
ctx.template.installWorkersTypes = true;
59+
ctx.template.skipWranglerTypegen = true;
5860
vi.mocked(getLatestTypesEntrypoint).mockReturnValue(null);
5961
await updateTsConfig(ctx);
6062

@@ -63,6 +65,7 @@ describe("updateTsConfig", () => {
6365

6466
test("don't clobber existing entrypoints", async () => {
6567
ctx.template.installWorkersTypes = true;
68+
ctx.template.skipWranglerTypegen = true;
6669
vi.mocked(readFile).mockImplementation(
6770
() =>
6871
`{ "compilerOptions": { "types" : ["@cloudflare/workers-types/2021-03-20"]} }`,
@@ -74,17 +77,36 @@ describe("updateTsConfig", () => {
7477
);
7578
});
7679

77-
test("will remove workers-types when generating types", async () => {
78-
vi.mocked(readFile).mockImplementation(
79-
() =>
80-
`{ "compilerOptions": { "types" : ["@cloudflare/workers-types/2021-03-20"]} }`,
81-
);
80+
test("will remove workers-types when generating types, if generated types include runtime types", async () => {
81+
vi.mocked(readFile).mockImplementation((path) => {
82+
console.log("path", path);
83+
if (path.includes("tsconfig.json")) {
84+
return `{ "compilerOptions": { "types" : ["@cloudflare/workers-types/2021-03-20"]} }`;
85+
} else {
86+
return "// Runtime types generated with workerd";
87+
}
88+
});
8289
await updateTsConfig(ctx);
8390
expect(vi.mocked(writeFile).mock.calls[0][1]).not.toContain(
8491
`"@cloudflare/workers-types/2021-03-20"`,
8592
);
8693
});
8794

95+
test("will NOT remove workers-types when generating types, if generated types don't include runtime types", async () => {
96+
vi.mocked(readFile).mockImplementation((path) => {
97+
if (path.includes("tsconfig.json")) {
98+
return `{ "compilerOptions": { "types" : ["@cloudflare/workers-types/2021-03-20"]} }`;
99+
} else {
100+
return "no runtime types here";
101+
}
102+
});
103+
await updateTsConfig(ctx);
104+
105+
expect(vi.mocked(writeFile).mock.calls[0][1]).toContain(
106+
`"@cloudflare/workers-types/2021-03-20"`,
107+
);
108+
});
109+
88110
test("will add generated types file", async () => {
89111
await updateTsConfig(ctx);
90112
expect(vi.mocked(writeFile).mock.calls[0][1]).toContain(

packages/create-cloudflare/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ const configure = async (ctx: C3Context) => {
170170
if (ctx.template.installWorkersTypes) {
171171
await installWorkersTypes(ctx);
172172
}
173-
if (ctx.template.skipWranglerTypegen) {
173+
if (!ctx.template.skipWranglerTypegen) {
174174
await generateWorkersTypes(ctx);
175175
}
176176

packages/create-cloudflare/src/workers.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, read } from "fs";
1+
import { existsSync } from "fs";
22
import { join, resolve } from "path";
33
import { warn } from "@cloudflare/cli";
44
import { brandColor, dim } from "@cloudflare/cli/colors";
@@ -61,13 +61,11 @@ export async function updateTsConfig(ctx: C3Context) {
6161
if (!existsSync(tsconfigPath)) {
6262
return;
6363
}
64-
6564
const tsconfig = readFile(tsconfigPath);
66-
6765
try {
6866
const config = jsonc.parse(tsconfig);
6967
const currentTypes = config.compilerOptions?.types ?? [];
70-
const newTypes: string[] = currentTypes;
68+
let newTypes: string[] = [...currentTypes];
7169
if (ctx.template.installWorkersTypes) {
7270
const entrypointVersion = getLatestTypesEntrypoint(ctx);
7371
if (entrypointVersion === null) {
@@ -77,16 +75,18 @@ export async function updateTsConfig(ctx: C3Context) {
7775
const explicitEntrypoint = (currentTypes as string[]).some((t) =>
7876
t.match(/@cloudflare\/workers-types\/\d{4}-\d{2}-\d{2}/),
7977
);
80-
// If a type declaration with an explicit entrypoint exists, leave the types as is
78+
// If a type declaration with an explicit entrypoint exists, leave the types as is.
8179
// Otherwise, add the latest entrypoint
8280
if (!explicitEntrypoint) {
83-
newTypes
84-
.filter((t: string) => t !== "@cloudflare/workers-types")
85-
.push(typesEntrypoint);
81+
newTypes = newTypes.filter(
82+
(t: string) => t !== "@cloudflare/workers-types",
83+
);
84+
newTypes.push(typesEntrypoint);
8685
}
8786
}
8887
if (!ctx.template.skipWranglerTypegen) {
89-
// if generated types include runtime types, remove workers-types
88+
newTypes.push(ctx.template.typesPath ?? "./worker-configuration.d.ts");
89+
// if generated types include runtime types, remove @cloudflare/workers-types
9090
const typegen = readFile(
9191
ctx.template.typesPath ?? "./worker-configuration.d.ts",
9292
).split("\n");
@@ -95,14 +95,14 @@ export async function updateTsConfig(ctx: C3Context) {
9595
line.includes("// Runtime types generated with workerd"),
9696
)
9797
) {
98-
newTypes.filter(
98+
newTypes = newTypes.filter(
9999
(t: string) => !t.startsWith("@cloudflare/workers-types"),
100100
);
101101
}
102+
// add node types if nodejs_compat is enabled
102103
if (ctx.template.compatibilityFlags?.includes("nodejs_compat")) {
103104
newTypes.push("node");
104105
}
105-
newTypes.push(ctx.template.typesPath ?? "./worker-configuration.d.ts");
106106
}
107107
if (newTypes.sort() === currentTypes.sort()) {
108108
return;

0 commit comments

Comments
 (0)