-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9744261
commit 2ef9aa4
Showing
6 changed files
with
195 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ describe("types", () => { | |
.split("\n"); | ||
|
||
expect(file[0]).toMatchInlineSnapshot( | ||
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\`"` | ||
`"// Generated by Wrangler by running \`wrangler types ./types.d.ts\` (hash: e82ba4d7b995dd9ca6fb0332d81f889b)"` | ||
); | ||
expect(file[1]).match( | ||
/\/\/ Runtime types generated with workerd@1\.\d+\.\d \d\d\d\d-\d\d-\d\d ([a-z_]+,?)*/ | ||
|
@@ -96,29 +96,43 @@ describe("types", () => { | |
"FAKE RUNTIME", | ||
].join("\n") | ||
); | ||
console.log( | ||
[ | ||
file[0], | ||
file[1], | ||
"FAKE ENV", | ||
"// Begin runtime types", | ||
"FAKE RUNTIME", | ||
].join("\n") | ||
); | ||
|
||
await helper.run(`wrangler types`); | ||
|
||
const file2 = (await readFile(typesPath)).toString(); | ||
|
||
expect(file2).toMatchInlineSnapshot(` | ||
"// Generated by Wrangler by running \`wrangler types\` | ||
// Runtime types generated with [email protected] 2023-01-01 nodejs_compat,no_global_navigator | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface,@typescript-eslint/no-empty-object-type | ||
interface Env { | ||
} | ||
// regenerates env types | ||
expect(file2).toContain("interface Env {"); | ||
// uses cached runtime types | ||
expect(file2).toContain("// Begin runtime types"); | ||
expect(file2).toContain("FAKE RUNTIME"); | ||
}); | ||
|
||
// Begin runtime types | ||
FAKE RUNTIME" | ||
`); | ||
it("should prompt you to update types if they've been changed", async () => { | ||
const helper = new WranglerE2ETestHelper(); | ||
await helper.seed(seed); | ||
await helper.run(`wrangler types`); | ||
seed["wrangler.toml"] = dedent` | ||
name = "test-worker" | ||
main = "src/index.ts" | ||
compatibility_date = "2023-01-01" | ||
compatibility_flags = ["nodejs_compat", "no_global_navigator"] | ||
[vars] | ||
BEEP = "BOOP" | ||
`; | ||
await helper.seed(seed); | ||
const worker = helper.runLongLived("wrangler dev"); | ||
await worker.readUntil(/❓ It looks like your types might be out of date./); | ||
seed["wrangler.toml"] = dedent` | ||
name = "test-worker" | ||
main = "src/index.ts" | ||
compatibility_date = "2023-01-01" | ||
compatibility_flags = ["nodejs_compat"] | ||
[vars] | ||
BEEP = "BOOP" | ||
ASDf = "ADSfadsf" | ||
`; | ||
await helper.seed(seed); | ||
await worker.readUntil(/❓ It looks like your types might be out of date./); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { readFileSync } from "fs"; | ||
import { version } from "workerd"; | ||
import { logger } from "../logger"; | ||
import { generateEnvTypes } from "."; | ||
import type { Config } from "../config"; | ||
import type { Entry } from "../deployment-bundle/entry"; | ||
|
||
export const checkTypesDiff = async (config: Config, entry: Entry) => { | ||
if (!entry.file.endsWith(".ts")) { | ||
return; | ||
} | ||
let maybeExistingTypesFile: string[]; | ||
try { | ||
// Note: this checks the default location only | ||
maybeExistingTypesFile = readFileSync( | ||
"./worker-configuration.d.ts", | ||
"utf-8" | ||
).split("\n"); | ||
} catch { | ||
return; | ||
} | ||
const existingEnvHeader = maybeExistingTypesFile.find((line) => | ||
line.startsWith("// Generated by Wrangler by running") | ||
); | ||
const maybeExistingHash = | ||
existingEnvHeader?.match(/hash: (?<hash>.*)\)/)?.groups?.hash; | ||
const previousStrictVars = existingEnvHeader?.match( | ||
/--strict-vars(=|\s)(?<result>true|false)/ | ||
)?.groups?.result; | ||
const previousEnvInterface = existingEnvHeader?.match( | ||
/--env-interface(=|\s)(?<result>[a-zA-Z][a-zA-Z0-9_]*)/ | ||
)?.groups?.result; | ||
|
||
let newEnvHeader: string | undefined; | ||
try { | ||
const { envHeader } = await generateEnvTypes( | ||
config, | ||
{ strictVars: previousStrictVars === "false" ? false : true }, | ||
previousEnvInterface ?? "Env", | ||
"worker-configuration.d.ts", | ||
entry, | ||
// don't log anything | ||
false | ||
); | ||
newEnvHeader = envHeader; | ||
} catch (e) { | ||
logger.error(e); | ||
} | ||
|
||
const newHash = newEnvHeader?.match(/hash: (?<hash>.*)\)/)?.groups?.hash; | ||
|
||
const existingRuntimeHeader = maybeExistingTypesFile.find((line) => | ||
line.startsWith("// Runtime types generated with") | ||
); | ||
const newRuntimeHeader = `// Runtime types generated with workerd@${version} ${config.compatibility_date} ${config.compatibility_flags.sort().join(",")}`; | ||
|
||
const envOutOfDate = existingEnvHeader && maybeExistingHash !== newHash; | ||
const runtimeOutOfDate = | ||
existingRuntimeHeader && existingRuntimeHeader !== newRuntimeHeader; | ||
|
||
if (envOutOfDate || runtimeOutOfDate) { | ||
logger.log( | ||
"❓ It looks like your types might be out of date. Have you updated your config file since last running `wrangler types`?" | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.