Skip to content

Commit 600a895

Browse files
committed
fixups
1 parent 29109c8 commit 600a895

File tree

7 files changed

+30
-111
lines changed

7 files changed

+30
-111
lines changed

.changeset/nasty-kiwis-talk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Include runtime types in the output of `wrangler types` by default
88

99
This behaviour was previously gated behind `--experimental-include-runtime`. That flag is no longer necessary and has been removed. It has been replaced by `--include-runtime` and `--include-env`, both of which are set to `true` by default. If you were previously using `--x-include-runtime`, you can drop that flag and remove the separate `runtime.d.ts` file.
1010

11-
If you were previously using `@cloudflare/workers-types` we recommend you run uninstall and run `wrangler types` instead. Note that `@cloudflare/workers-types` will continue to be published.
11+
If you were previously using `@cloudflare/workers-types` we recommend you run uninstall (e.g. `npm uninstall @cloudflare/workers-types`) and run `wrangler types` instead. Note that `@cloudflare/workers-types` will continue to be published.

packages/wrangler/e2e/types.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe("types", () => {
170170
`;
171171
await helper.seed(seed);
172172
const worker = helper.runLongLived("wrangler dev");
173-
await worker.readUntil(/ It looks like your types might be out of date./);
173+
await worker.readUntil(/ Your types might be out of date./);
174174
seed["wrangler.toml"] = dedent`
175175
name = "test-worker"
176176
main = "src/index.ts"
@@ -181,6 +181,6 @@ describe("types", () => {
181181
ASDf = "ADSfadsf"
182182
`;
183183
await helper.seed(seed);
184-
await worker.readUntil(/ It looks like your types might be out of date./);
184+
await worker.readUntil(/ Your types might be out of date./);
185185
});
186186
});

packages/wrangler/src/__tests__/type-generation.test.ts

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,104 +1204,5 @@ describe("generate types", () => {
12041204
"
12051205
`);
12061206
});
1207-
it("prints something helpful about node compat", async () => {
1208-
fs.writeFileSync(
1209-
"./wrangler.toml",
1210-
TOML.stringify({
1211-
compatibility_date: "2022-12-12",
1212-
compatibility_flags: ["nodejs_compat"],
1213-
vars: {
1214-
"var-a": "a",
1215-
},
1216-
} as TOML.JsonMap),
1217-
"utf-8"
1218-
);
1219-
fs.writeFileSync(
1220-
"./tsconfig.json",
1221-
JSON.stringify({
1222-
compilerOptions: {
1223-
types: ["worker-configuration.d.ts"],
1224-
},
1225-
})
1226-
);
1227-
await runWrangler("types --include-env=false");
1228-
expect(std.out).toMatchInlineSnapshot(`
1229-
"Generating runtime types...
1230-
1231-
Runtime types generated.
1232-
1233-
────────────────────────────────────────────────────────────
1234-
✨ Types written to worker-configuration.d.ts
1235-
1236-
Action required Install @types/node
1237-
Since you have the \`nodejs_compat\` flag, you should install Node.js types by running \\"npm i --save-dev @types/node\\".
1238-
1239-
📖 Read about runtime types
1240-
https://developers.cloudflare.com/workers/languages/typescript/#generate-types
1241-
📣 Remember to rerun 'wrangler types' after you change your wrangler.toml file.
1242-
"
1243-
`);
1244-
});
1245-
it("prints all the relevant stuff if you having nothing set up", async () => {
1246-
fs.writeFileSync(
1247-
"./wrangler.toml",
1248-
TOML.stringify({
1249-
compatibility_date: "2022-12-12",
1250-
compatibility_flags: ["nodejs_compat"],
1251-
vars: {
1252-
"var-a": "a",
1253-
},
1254-
} as TOML.JsonMap),
1255-
"utf-8"
1256-
);
1257-
fs.writeFileSync(
1258-
"./tsconfig.json",
1259-
JSON.stringify({
1260-
compilerOptions: {
1261-
types: ["irrelevant.d.ts"],
1262-
},
1263-
})
1264-
);
1265-
await runWrangler("types --path=custom.d.ts --include-env=false");
1266-
expect(std.out).toMatchInlineSnapshot(`
1267-
"Generating runtime types...
1268-
1269-
Runtime types generated.
1270-
1271-
────────────────────────────────────────────────────────────
1272-
✨ Types written to custom.d.ts
1273-
1274-
Action required Install @types/node
1275-
Since you have the \`nodejs_compat\` flag, you should install Node.js types by running \\"npm i --save-dev @types/node\\".
1276-
1277-
📖 Read about runtime types
1278-
https://developers.cloudflare.com/workers/languages/typescript/#generate-types
1279-
📣 Remember to rerun 'wrangler types' after you change your wrangler.toml file.
1280-
"
1281-
`);
1282-
});
1283-
it("should not recommend to install @types/node if 'node' exists in types array", async () => {
1284-
fs.writeFileSync(
1285-
"./wrangler.toml",
1286-
TOML.stringify({
1287-
compatibility_date: "2022-12-12",
1288-
compatibility_flags: ["nodejs_compat"],
1289-
} as TOML.JsonMap),
1290-
"utf-8"
1291-
);
1292-
fs.writeFileSync(
1293-
"./tsconfig.json",
1294-
JSON.stringify({
1295-
compilerOptions: {
1296-
types: ["node"],
1297-
},
1298-
})
1299-
);
1300-
await runWrangler(`types`);
1301-
1302-
expect(std.out).not.toContain(
1303-
`Since you have the \`nodejs_compat\` flag, you should install Node.js`
1304-
);
1305-
});
13061207
});
13071208
});

packages/wrangler/src/api/startDevWorker/ConfigController.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,12 @@ async function resolveConfig(
371371
}
372372

373373
// prompt user to update their types if we detect that it is out of date
374-
await checkTypesDiff(config, entry);
374+
const typesChanged = await checkTypesDiff(config, entry);
375+
if (typesChanged) {
376+
logger.log(
377+
"❓ Your types might be out of date. Re-run `wrangler types` to ensure your types are correct."
378+
);
379+
}
375380

376381
return resolved;
377382
}

packages/wrangler/src/type-generation/helpers.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,5 @@ export const checkTypesDiff = async (config: Config, entry: Entry) => {
6262
const runtimeOutOfDate =
6363
existingRuntimeHeader && existingRuntimeHeader !== newRuntimeHeader;
6464

65-
if (envOutOfDate || runtimeOutOfDate) {
66-
logger.log(
67-
"❓ It looks like your types might be out of date. Have you updated your config file since last running `wrangler types`?"
68-
);
69-
}
65+
return envOutOfDate || runtimeOutOfDate;
7066
};

packages/wrangler/src/type-generation/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export const typesCommand = createCommand({
167167

168168
logHorizontalRule();
169169

170+
// don't write an empty Env type for service worker syntax
170171
if ((header.length && content.length) || entrypointFormat === "modules") {
171172
fs.writeFileSync(
172173
outputPath,
@@ -183,7 +184,7 @@ export const typesCommand = createCommand({
183184
config.compatibility_flags
184185
);
185186
if (args.includeRuntime) {
186-
logRuntimeTypesMessage(outputPath, tsconfigTypes, mode !== null);
187+
logRuntimeTypesMessage(tsconfigTypes, mode !== null);
187188
}
188189
logger.log(
189190
`📣 Remember to rerun 'wrangler types' after you change your ${configFileName(config.configPath)} file.\n`

packages/wrangler/src/type-generation/runtime/log-runtime-types-message.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { existsSync } from "node:fs";
2+
import { join } from "node:path";
23
import chalk from "chalk";
4+
import { findUpMultipleSync } from "find-up";
35
import { logger } from "../../logger";
46

57
/**
68
* Constructs a comprehensive log message for the user after generating runtime types.
79
*/
810
export function logRuntimeTypesMessage(
9-
outFile: string,
1011
tsconfigTypes: string[],
1112
isNodeCompat = false
1213
) {
1314
const isWorkersTypesInstalled = tsconfigTypes.find((type) =>
1415
type.startsWith("@cloudflare/workers-types")
1516
);
16-
const isNodeTypesInstalled = tsconfigTypes.find((type) => type === "node");
17+
1718
const maybeHasOldRuntimeFile = existsSync("./.wrangler/types/runtime.d.ts");
1819
if (maybeHasOldRuntimeFile) {
1920
logAction("Remove the old runtime.d.ts file");
@@ -36,6 +37,21 @@ export function logRuntimeTypesMessage(
3637
logger.log("");
3738
}
3839

40+
let isNodeTypesInstalled = Boolean(
41+
tsconfigTypes.find((type) => type === "node")
42+
);
43+
44+
if (!isNodeTypesInstalled && isNodeCompat) {
45+
const nodeModules = findUpMultipleSync("node_modules", {
46+
type: "directory",
47+
});
48+
for (const folder of nodeModules) {
49+
if (nodeModules && existsSync(join(folder, "@types/node"))) {
50+
isNodeTypesInstalled = true;
51+
break;
52+
}
53+
}
54+
}
3955
if (isNodeCompat && !isNodeTypesInstalled) {
4056
logAction("Install @types/node");
4157
logger.log(

0 commit comments

Comments
 (0)