Skip to content

Commit e81c2cf

Browse files
authored
Update config after creation commands (#10414)
* Support editing the config file after resource creation * bail on toml files * tests * Add tests * Create ten-yaks-teach.md * fix lint * format * fix tests * address comments
1 parent 7d314b1 commit e81c2cf

File tree

19 files changed

+634
-161
lines changed

19 files changed

+634
-161
lines changed

.changeset/ten-yaks-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
Support automatically updating the user's config file with newly created resources

packages/wrangler/e2e/r2.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)("r2", () => {
2727
expect(normalize(output.stdout)).toMatchInlineSnapshot(`
2828
"Creating bucket 'tmp-e2e-r2-00000000-0000-0000-0000-000000000000'...
2929
✅ Created bucket 'tmp-e2e-r2-00000000-0000-0000-0000-000000000000' with default storage class of Standard.
30-
Configure your Worker to write objects to this bucket:
30+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
3131
{
3232
"r2_buckets": [
3333
{

packages/wrangler/src/__tests__/__snapshots__/kv.test.ts.snap

Lines changed: 118 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`wrangler > kv namespace > create > wrangler.json > should create a name
44
"Resource location: remote
55
🌀 Creating namespace with title \\"UnitTestNamespace\\"
66
✨ Success!
7-
Add the following to your configuration file in your kv_namespaces array:
7+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
88
{
99
\\"kv_namespaces\\": [
1010
{
@@ -15,11 +15,24 @@ Add the following to your configuration file in your kv_namespaces array:
1515
}"
1616
`;
1717

18+
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace 2`] = `
19+
"{
20+
\\"compatibility_date\\": \\"2022-01-12\\",
21+
\\"name\\": \\"worker\\",
22+
\\"kv_namespaces\\": [
23+
{
24+
\\"binding\\": \\"UnitTestNamespace\\",
25+
\\"id\\": \\"some-namespace-id\\"
26+
}
27+
]
28+
}"
29+
`;
30+
1831
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace in an environment if configured to do so 1`] = `
1932
"Resource location: remote
2033
🌀 Creating namespace with title \\"customEnv-UnitTestNamespace\\"
2134
✨ Success!
22-
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
35+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file in the \\"customEnv\\" environment:
2336
{
2437
\\"kv_namespaces\\": [
2538
{
@@ -30,11 +43,57 @@ Add the following to your configuration file in your kv_namespaces array under [
3043
}"
3144
`;
3245

46+
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace in an environment if configured to do so 2`] = `
47+
"{
48+
\\"compatibility_date\\": \\"2022-01-12\\",
49+
\\"name\\": \\"worker\\",
50+
\\"env\\": {
51+
\\"customEnv\\": {
52+
\\"name\\": \\"worker\\",
53+
\\"kv_namespaces\\": [
54+
{
55+
\\"binding\\": \\"UnitTestNamespace\\",
56+
\\"id\\": \\"some-namespace-id\\"
57+
}
58+
]
59+
}
60+
}
61+
}"
62+
`;
63+
3364
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace using configured worker name 1`] = `
3465
"Resource location: remote
3566
🌀 Creating namespace with title \\"UnitTestNamespace\\"
3667
✨ Success!
37-
Add the following to your configuration file in your kv_namespaces array:
68+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
69+
{
70+
\\"kv_namespaces\\": [
71+
{
72+
\\"binding\\": \\"UnitTestNamespace\\",
73+
\\"id\\": \\"some-namespace-id\\"
74+
}
75+
]
76+
}"
77+
`;
78+
79+
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace using configured worker name 2`] = `
80+
"{
81+
\\"compatibility_date\\": \\"2022-01-12\\",
82+
\\"name\\": \\"other-worker\\",
83+
\\"kv_namespaces\\": [
84+
{
85+
\\"binding\\": \\"UnitTestNamespace\\",
86+
\\"id\\": \\"some-namespace-id\\"
87+
}
88+
]
89+
}"
90+
`;
91+
92+
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace with custom binding name 1`] = `
93+
"Resource location: remote
94+
🌀 Creating namespace with title \\"UnitTestNamespace\\"
95+
✨ Success!
96+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
3897
{
3998
\\"kv_namespaces\\": [
4099
{
@@ -45,11 +104,24 @@ Add the following to your configuration file in your kv_namespaces array:
45104
}"
46105
`;
47106

107+
exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace with custom binding name 2`] = `
108+
"{
109+
\\"compatibility_date\\": \\"2022-01-12\\",
110+
\\"name\\": \\"worker\\",
111+
\\"kv_namespaces\\": [
112+
{
113+
\\"binding\\": \\"HELLO\\",
114+
\\"id\\": \\"some-namespace-id\\"
115+
}
116+
]
117+
}"
118+
`;
119+
48120
exports[`wrangler > kv namespace > create > wrangler.json > should create a preview namespace if configured to do so 1`] = `
49121
"Resource location: remote
50122
🌀 Creating namespace with title \\"UnitTestNamespace_preview\\"
51123
✨ Success!
52-
Add the following to your configuration file in your kv_namespaces array:
124+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
53125
{
54126
\\"kv_namespaces\\": [
55127
{
@@ -64,40 +136,78 @@ exports[`wrangler > kv namespace > create > wrangler.toml > should create a name
64136
"Resource location: remote
65137
🌀 Creating namespace with title \\"UnitTestNamespace\\"
66138
✨ Success!
67-
Add the following to your configuration file in your kv_namespaces array:
139+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
68140
[[kv_namespaces]]
69141
binding = \\"UnitTestNamespace\\"
70142
id = \\"some-namespace-id\\"
71143
"
72144
`;
73145

146+
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace 2`] = `
147+
"compatibility_date = \\"2022-01-12\\"
148+
name = \\"worker\\"
149+
"
150+
`;
151+
74152
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace in an environment if configured to do so 1`] = `
75153
"Resource location: remote
76154
🌀 Creating namespace with title \\"customEnv-UnitTestNamespace\\"
77155
✨ Success!
78-
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
156+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file in the \\"customEnv\\" environment:
79157
[[kv_namespaces]]
80158
binding = \\"UnitTestNamespace\\"
81159
id = \\"some-namespace-id\\"
82160
"
83161
`;
84162

163+
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace in an environment if configured to do so 2`] = `
164+
"compatibility_date = \\"2022-01-12\\"
165+
name = \\"worker\\"
166+
167+
[env.customEnv]
168+
name = \\"worker\\"
169+
"
170+
`;
171+
85172
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace using configured worker name 1`] = `
86173
"Resource location: remote
87174
🌀 Creating namespace with title \\"UnitTestNamespace\\"
88175
✨ Success!
89-
Add the following to your configuration file in your kv_namespaces array:
176+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
90177
[[kv_namespaces]]
91178
binding = \\"UnitTestNamespace\\"
92179
id = \\"some-namespace-id\\"
93180
"
94181
`;
95182

183+
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace using configured worker name 2`] = `
184+
"compatibility_date = \\"2022-01-12\\"
185+
name = \\"other-worker\\"
186+
"
187+
`;
188+
189+
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace with custom binding name 1`] = `
190+
"Resource location: remote
191+
🌀 Creating namespace with title \\"UnitTestNamespace\\"
192+
✨ Success!
193+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
194+
[[kv_namespaces]]
195+
binding = \\"UnitTestNamespace\\"
196+
id = \\"some-namespace-id\\"
197+
"
198+
`;
199+
200+
exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace with custom binding name 2`] = `
201+
"compatibility_date = \\"2022-01-12\\"
202+
name = \\"worker\\"
203+
"
204+
`;
205+
96206
exports[`wrangler > kv namespace > create > wrangler.toml > should create a preview namespace if configured to do so 1`] = `
97207
"Resource location: remote
98208
🌀 Creating namespace with title \\"UnitTestNamespace_preview\\"
99209
✨ Success!
100-
Add the following to your configuration file in your kv_namespaces array:
210+
To access your new KV Namespace in your Worker, add the following snippet to your configuration file:
101211
[[kv_namespaces]]
102212
binding = \\"UnitTestNamespace\\"
103213
preview_id = \\"some-namespace-id\\"

packages/wrangler/src/__tests__/__snapshots__/r2.test.ts.snap

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,71 @@
33
exports[`r2 > bucket > create > wrangler.json > should create a bucket & check request inputs 1`] = `
44
"Creating bucket 'test-bucket'...
55
✅ Created bucket 'test-bucket' with default storage class of Standard.
6-
7-
Configure your Worker to write objects to this bucket:
8-
6+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
97
{
108
\\"r2_buckets\\": [
119
{
1210
\\"bucket_name\\": \\"test-bucket\\",
1311
\\"binding\\": \\"test_bucket\\"
1412
}
1513
]
16-
}"
14+
}
15+
? Would you like Wrangler to add it on your behalf?
16+
🤖 Using fallback value in non-interactive context: No"
1717
`;
1818

1919
exports[`r2 > bucket > create > wrangler.json > should create a bucket with the expected default storage class 1`] = `
2020
"Creating bucket 'test-bucket'...
2121
✅ Created bucket 'test-bucket' with default storage class of InfrequentAccess.
22-
23-
Configure your Worker to write objects to this bucket:
24-
22+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
2523
{
2624
\\"r2_buckets\\": [
2725
{
2826
\\"bucket_name\\": \\"test-bucket\\",
2927
\\"binding\\": \\"test_bucket\\"
3028
}
3129
]
32-
}"
30+
}
31+
? Would you like Wrangler to add it on your behalf?
32+
🤖 Using fallback value in non-interactive context: No"
3333
`;
3434

3535
exports[`r2 > bucket > create > wrangler.json > should create a bucket with the expected jurisdiction 1`] = `
3636
"Creating bucket 'test-bucket (eu)'...
3737
✅ Created bucket 'test-bucket (eu)' with default storage class of Standard.
38-
39-
Configure your Worker to write objects to this bucket:
40-
38+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
4139
{
4240
\\"r2_buckets\\": [
4341
{
4442
\\"bucket_name\\": \\"test-bucket\\",
4543
\\"binding\\": \\"test_bucket\\"
4644
}
4745
]
48-
}"
46+
}
47+
? Would you like Wrangler to add it on your behalf?
48+
🤖 Using fallback value in non-interactive context: No"
4949
`;
5050

5151
exports[`r2 > bucket > create > wrangler.json > should create a bucket with the expected location hint 1`] = `
5252
"Creating bucket 'test-bucket'...
5353
✅ Created bucket 'test-bucket' with location hint weur and default storage class of Standard.
54-
55-
Configure your Worker to write objects to this bucket:
56-
54+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
5755
{
5856
\\"r2_buckets\\": [
5957
{
6058
\\"bucket_name\\": \\"test-bucket\\",
6159
\\"binding\\": \\"test_bucket\\"
6260
}
6361
]
64-
}"
62+
}
63+
? Would you like Wrangler to add it on your behalf?
64+
🤖 Using fallback value in non-interactive context: No"
6565
`;
6666

6767
exports[`r2 > bucket > create > wrangler.toml > should create a bucket & check request inputs 1`] = `
6868
"Creating bucket 'test-bucket'...
6969
✅ Created bucket 'test-bucket' with default storage class of Standard.
70-
71-
Configure your Worker to write objects to this bucket:
72-
70+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
7371
[[r2_buckets]]
7472
bucket_name = \\"test-bucket\\"
7573
binding = \\"test_bucket\\"
@@ -79,9 +77,7 @@ binding = \\"test_bucket\\"
7977
exports[`r2 > bucket > create > wrangler.toml > should create a bucket with the expected default storage class 1`] = `
8078
"Creating bucket 'test-bucket'...
8179
✅ Created bucket 'test-bucket' with default storage class of InfrequentAccess.
82-
83-
Configure your Worker to write objects to this bucket:
84-
80+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
8581
[[r2_buckets]]
8682
bucket_name = \\"test-bucket\\"
8783
binding = \\"test_bucket\\"
@@ -91,9 +87,7 @@ binding = \\"test_bucket\\"
9187
exports[`r2 > bucket > create > wrangler.toml > should create a bucket with the expected jurisdiction 1`] = `
9288
"Creating bucket 'test-bucket (eu)'...
9389
✅ Created bucket 'test-bucket (eu)' with default storage class of Standard.
94-
95-
Configure your Worker to write objects to this bucket:
96-
90+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
9791
[[r2_buckets]]
9892
bucket_name = \\"test-bucket\\"
9993
binding = \\"test_bucket\\"
@@ -103,9 +97,7 @@ binding = \\"test_bucket\\"
10397
exports[`r2 > bucket > create > wrangler.toml > should create a bucket with the expected location hint 1`] = `
10498
"Creating bucket 'test-bucket'...
10599
✅ Created bucket 'test-bucket' with location hint weur and default storage class of Standard.
106-
107-
Configure your Worker to write objects to this bucket:
108-
100+
To access your new R2 Bucket in your Worker, add the following snippet to your configuration file:
109101
[[r2_buckets]]
110102
bucket_name = \\"test-bucket\\"
111103
binding = \\"test_bucket\\"

packages/wrangler/src/__tests__/d1/create.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { mockGetMemberships } from "../helpers/mock-oauth-flow";
66
import { msw } from "../helpers/msw";
77
import { runInTempDir } from "../helpers/run-in-tmp";
88
import { runWrangler } from "../helpers/run-wrangler";
9+
import { writeWranglerConfig } from "../helpers/write-wrangler-config";
910

1011
describe("create", () => {
1112
mockAccountId({ accountId: null });
@@ -40,6 +41,8 @@ describe("create", () => {
4041
});
4142

4243
it("should try send a request to the API for a valid input", async () => {
44+
writeWranglerConfig({ name: "worker" }, "wrangler.json");
45+
4346
setIsTTY(false);
4447
mockGetMemberships([
4548
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
@@ -64,15 +67,18 @@ describe("create", () => {
6467
"✅ Successfully created DB 'test' in region OC
6568
Created your new D1 database.
6669
70+
To access your new D1 Database in your Worker, add the following snippet to your configuration file:
6771
{
6872
\\"d1_databases\\": [
6973
{
70-
\\"binding\\": \\"DB\\",
74+
\\"binding\\": \\"test\\",
7175
\\"database_name\\": \\"test\\",
7276
\\"database_id\\": \\"51e7c314-456e-4167-b6c3-869ad188fc23\\"
7377
}
7478
]
75-
}"
79+
}
80+
? Would you like Wrangler to add it on your behalf?
81+
🤖 Using fallback value in non-interactive context: No"
7682
`);
7783
});
7884
});

0 commit comments

Comments
 (0)