Skip to content

Commit 5693d07

Browse files
Add wrangler types customization and improvements (#5042)
* [wrangler] feat: add new `--env-interface` to `wrangler types` * [wrangler] feat: add new `path` positional argument to `wrangler types` * [wrangler] refactor: make types command definition more consistent for consistency with (most of) the rest of the wrangler command use a `typesOptions` and `typesHandler` arguments instead of inlining such functions in the `wrangler.command()` call * [wrangler] fix: make `wrangler types` honor top level config argument * [wrangler] fix: make `wrangler types` pick up secrets from `.dev.vars` * [wrangler] feat: include command run in the `wrangler types` comment --------- Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent b03db86 commit 5693d07

File tree

16 files changed

+609
-47
lines changed

16 files changed

+609
-47
lines changed

.changeset/beige-pants-promise.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
feat: include command run in the `wrangler types` comment
6+
7+
In the comment added to the `.d.ts` file generated by `wrangler types`
8+
include the command run to generated the file

.changeset/clever-needles-love.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: add new `--env-interface` to `wrangler types`
6+
7+
Allow users to specify the name of the interface that they want `wrangler types` to generate for the `env` parameter, via the new CLI flag `--env-interface`
8+
9+
Example:
10+
11+
```sh
12+
wrangler types --env-interface CloudflareEnv
13+
```
14+
15+
generates
16+
17+
```ts
18+
interface CloudflareEnv {}
19+
```
20+
21+
instead of
22+
23+
```ts
24+
interface Env {}
25+
```

.changeset/curly-masks-raise.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: add new `path` positional argument to `wrangler types`
6+
7+
Allow users to specify the path to the typings (.d.ts) file they want
8+
`wrangler types` to generate
9+
10+
Example:
11+
12+
```sh
13+
wrangler types ./my-env.d.ts
14+
```
15+
16+
generates a `my-env.d.ts` file in the current directory
17+
instead of creating a `worker-configuration.d.ts` file

.changeset/itchy-dragons-burn.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: make `wrangler types` honor top level config argument
6+
7+
The `wrangler types` command currently ignores the `-c|--config` argument
8+
(although it is still getting shown in the command's help message). Make
9+
sure that the command honors the flag.
10+
Also, if no config file is detected
11+
present a warning to the user

.changeset/serious-socks-tan.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: make the `wrangler types` command pick up local secret keys from `.dev.vars`
6+
7+
Make sure that the `wrangler types` command correctly picks up
8+
secret keys defined in `.dev.vars` and includes them in the generated
9+
file (marking them as generic `string` types of course)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "type-generation-fixture",
3+
"private": true,
4+
"description": "A test for the `wrangler types` utility",
5+
"scripts": {
6+
"test": "vitest run",
7+
"test:watch": "vitest"
8+
},
9+
"devDependencies": {
10+
"wrangler": "workspace:*"
11+
}
12+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { execSync } from "child_process";
2+
import { mkdtempSync, readFileSync, realpathSync, writeFileSync } from "fs";
3+
import { tmpdir } from "os";
4+
import * as path from "path";
5+
import { join } from "path";
6+
import { beforeAll, describe, it } from "vitest";
7+
8+
describe("`wrangler types` - file comment", () => {
9+
let tempDir: string;
10+
11+
beforeAll(() => {
12+
tempDir = realpathSync(mkdtempSync(join(tmpdir(), "wrangler-types-test")));
13+
const tomlFile = join(tempDir, "wrangler.toml");
14+
const tomlFileA = join(tempDir, "wranglerA.toml");
15+
writeFileSync(tomlFile, '\n[vars]\nMY_VAR = "my-var-value"\n');
16+
writeFileSync(tomlFileA, '\n[vars]\nMY_VAR = "my-var-value"\n');
17+
});
18+
19+
function runWranglerTypesCommand(
20+
args = "",
21+
expectedOutputFile = "worker-configuration.d.ts"
22+
): string {
23+
const wranglerPath = path.resolve(
24+
__dirname,
25+
"..",
26+
"..",
27+
"..",
28+
"packages",
29+
"wrangler"
30+
);
31+
execSync(`npx ${wranglerPath} types ${args}`, {
32+
cwd: tempDir,
33+
});
34+
const typesFile = join(tempDir, expectedOutputFile);
35+
return readFileSync(typesFile, "utf-8");
36+
}
37+
38+
describe("includes a comment specifying the command run", () => {
39+
it("(base command)", async ({ expect }) => {
40+
const typesCommandOutput = runWranglerTypesCommand();
41+
expect(typesCommandOutput).toContain("by running `wrangler types`");
42+
});
43+
44+
it("(with types customization)", async ({ expect }) => {
45+
const typesCommandOutput = runWranglerTypesCommand(
46+
"--env-interface MyCloudflareEnv ./cflare-env.d.ts",
47+
"./cflare-env.d.ts"
48+
);
49+
expect(typesCommandOutput).toContain(
50+
"by running `wrangler types --env-interface MyCloudflareEnv ./cflare-env.d.ts`"
51+
);
52+
});
53+
54+
it("(with wrangler top level options)", async ({ expect }) => {
55+
const typesCommandOutput = runWranglerTypesCommand("-c wranglerA.toml");
56+
expect(typesCommandOutput).toContain(
57+
"by running `wrangler types -c wranglerA.toml`"
58+
);
59+
});
60+
});
61+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
testTimeout: 25_000,
6+
hookTimeout: 25_000,
7+
teardownTimeout: 25_000,
8+
useAtomics: true,
9+
},
10+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "type-generation-fixture"
2+
3+
[vars]
4+
MY_VAR = "my-var-value"

packages/wrangler/src/__tests__/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe("wrangler", () => {
5757
wrangler login 🔓 Login to Cloudflare
5858
wrangler logout 🚪 Logout from Cloudflare
5959
wrangler whoami 🕵️ Retrieve your user info and test your auth config
60-
wrangler types 📝 Generate types from bindings & module rules in config
60+
wrangler types [path] 📝 Generate types from bindings & module rules in config
6161
wrangler deployments 🚢 List and view details for deployments
6262
wrangler rollback [deployment-id] 🔙 Rollback a deployment
6363
@@ -111,7 +111,7 @@ describe("wrangler", () => {
111111
wrangler login 🔓 Login to Cloudflare
112112
wrangler logout 🚪 Logout from Cloudflare
113113
wrangler whoami 🕵️ Retrieve your user info and test your auth config
114-
wrangler types 📝 Generate types from bindings & module rules in config
114+
wrangler types [path] 📝 Generate types from bindings & module rules in config
115115
wrangler deployments 🚢 List and view details for deployments
116116
wrangler rollback [deployment-id] 🔙 Rollback a deployment
117117

0 commit comments

Comments
 (0)