wrangler 0.0.0-e9ab55a → 0.0.0-ed4f2ef

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +3 -3
  2. package/bin/wrangler.js +12 -3
  3. package/miniflare-dist/index.mjs +79 -0
  4. package/miniflare-dist/index.mjs.map +7 -0
  5. package/package.json +33 -16
  6. package/pages/functions/buildWorker.ts +1 -1
  7. package/pages/functions/filepath-routing.test.ts +212 -28
  8. package/pages/functions/filepath-routing.ts +83 -130
  9. package/pages/functions/routes.ts +11 -18
  10. package/pages/functions/template-worker.ts +3 -9
  11. package/src/__tests__/configuration.test.ts +2874 -0
  12. package/src/__tests__/dev.test.tsx +582 -61
  13. package/src/__tests__/guess-worker-format.test.ts +83 -0
  14. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  15. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  16. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  17. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  18. package/src/__tests__/helpers/mock-bin.ts +36 -0
  19. package/src/__tests__/{mock-cfetch.ts → helpers/mock-cfetch.ts} +52 -16
  20. package/src/__tests__/helpers/mock-console.ts +70 -0
  21. package/src/__tests__/{mock-dialogs.ts → helpers/mock-dialogs.ts} +1 -1
  22. package/src/__tests__/helpers/mock-kv.ts +33 -0
  23. package/src/__tests__/helpers/mock-oauth-flow.ts +178 -0
  24. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  25. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  26. package/src/__tests__/helpers/run-in-tmp.ts +32 -0
  27. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  28. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  29. package/src/__tests__/https-options.test.ts +127 -0
  30. package/src/__tests__/index.test.ts +777 -77
  31. package/src/__tests__/jest.setup.ts +58 -6
  32. package/src/__tests__/kv.test.ts +716 -462
  33. package/src/__tests__/package-manager.test.ts +206 -0
  34. package/src/__tests__/pages.test.ts +180 -0
  35. package/src/__tests__/parse.test.ts +322 -0
  36. package/src/__tests__/publish.test.ts +3915 -71
  37. package/src/__tests__/r2.test.ts +198 -0
  38. package/src/__tests__/route.test.ts +45 -0
  39. package/src/__tests__/secret.test.ts +425 -0
  40. package/src/__tests__/sentry.test.ts +146 -0
  41. package/src/__tests__/tail.test.ts +638 -0
  42. package/src/__tests__/user.test.ts +83 -0
  43. package/src/__tests__/whoami.test.tsx +95 -0
  44. package/src/bundle.ts +155 -0
  45. package/src/cfetch/index.ts +21 -19
  46. package/src/cfetch/internal.ts +92 -22
  47. package/src/cli.ts +14 -2
  48. package/src/config/README.md +107 -0
  49. package/src/config/config.ts +229 -0
  50. package/src/config/diagnostics.ts +80 -0
  51. package/src/config/environment.ts +371 -0
  52. package/src/config/index.ts +62 -0
  53. package/src/config/validation-helpers.ts +557 -0
  54. package/src/config/validation.ts +1240 -0
  55. package/src/{api/preview.ts → create-worker-preview.ts} +69 -24
  56. package/src/create-worker-upload-form.ts +240 -0
  57. package/src/dev/dev.tsx +413 -0
  58. package/src/dev/local.tsx +287 -0
  59. package/src/dev/remote.tsx +209 -0
  60. package/src/dev/use-esbuild.ts +109 -0
  61. package/src/dialogs.tsx +2 -2
  62. package/src/entry.ts +256 -0
  63. package/src/environment-variables.ts +33 -0
  64. package/src/errors.ts +11 -0
  65. package/src/https-options.ts +122 -0
  66. package/src/index.tsx +1310 -849
  67. package/src/inspect.ts +102 -21
  68. package/src/intl-polyfill.d.ts +139 -0
  69. package/src/{kv.tsx → kv.ts} +104 -61
  70. package/src/miniflare-cli/README.md +30 -0
  71. package/src/miniflare-cli/enum-keys.ts +17 -0
  72. package/src/miniflare-cli/index.ts +38 -0
  73. package/src/module-collection.ts +228 -35
  74. package/src/open-in-browser.ts +25 -0
  75. package/src/package-manager.ts +126 -0
  76. package/src/pages.tsx +214 -48
  77. package/src/parse.ts +183 -0
  78. package/src/paths.ts +26 -0
  79. package/src/proxy.ts +192 -39
  80. package/src/publish.ts +321 -318
  81. package/src/r2.ts +50 -0
  82. package/src/reporting.ts +131 -0
  83. package/src/selfsigned.d.ts +29 -0
  84. package/src/sites.tsx +224 -62
  85. package/src/tail/filters.ts +279 -0
  86. package/src/tail/index.ts +299 -0
  87. package/src/tail/printing.ts +62 -0
  88. package/src/user.tsx +317 -178
  89. package/src/whoami.tsx +69 -0
  90. package/src/{api/worker.ts → worker.ts} +54 -37
  91. package/templates/new-worker.js +15 -0
  92. package/templates/new-worker.ts +15 -0
  93. package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
  94. package/templates/tsconfig.json +105 -0
  95. package/wrangler-dist/cli.js +131262 -112648
  96. package/wrangler-dist/cli.js.map +3 -3
  97. package/src/__tests__/run-in-tmp.ts +0 -19
  98. package/src/__tests__/run-wrangler.ts +0 -32
  99. package/src/api/form_data.ts +0 -131
  100. package/src/config.ts +0 -646
  101. package/src/dev.tsx +0 -784
  102. package/src/tail.tsx +0 -73
@@ -1,76 +1,100 @@
1
+ import * as fs from "node:fs";
1
2
  import * as fsp from "node:fs/promises";
2
3
  import * as TOML from "@iarna/toml";
3
- import { mockConfirm } from "./mock-dialogs";
4
- import { runWrangler } from "./run-wrangler";
5
- import { runInTempDir } from "./run-in-tmp";
6
- import * as fs from "node:fs";
4
+ import { parseConfigFileTextToJson } from "typescript";
5
+ import { version as wranglerVersion } from "../../package.json";
6
+ import { getPackageManager } from "../package-manager";
7
+ import { mockConsoleMethods } from "./helpers/mock-console";
8
+ import { mockConfirm } from "./helpers/mock-dialogs";
9
+ import { runInTempDir } from "./helpers/run-in-tmp";
10
+ import { runWrangler } from "./helpers/run-wrangler";
11
+ import type { PackageManager } from "../package-manager";
7
12
 
8
13
  describe("wrangler", () => {
14
+ let mockPackageManager: PackageManager;
9
15
  runInTempDir();
10
16
 
17
+ beforeEach(() => {
18
+ mockPackageManager = {
19
+ cwd: process.cwd(),
20
+ // @ts-expect-error we're making a fake package manager here
21
+ type: "mockpm",
22
+ addDevDeps: jest.fn(),
23
+ install: jest.fn(),
24
+ };
25
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
26
+ });
27
+
28
+ const std = mockConsoleMethods();
29
+
11
30
  describe("no command", () => {
12
31
  it("should display a list of available commands", async () => {
13
- const { stdout, stderr } = await runWrangler();
32
+ await runWrangler();
14
33
 
15
- expect(stdout).toMatchInlineSnapshot(`
34
+ expect(std.out).toMatchInlineSnapshot(`
16
35
  "wrangler
17
36
 
18
37
  Commands:
19
38
  wrangler init [name] 📥 Create a wrangler.toml configuration file
20
- wrangler dev <filename> 👂 Start a local server for developing your worker
39
+ wrangler dev [script] 👂 Start a local server for developing your worker
21
40
  wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
22
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
41
+ wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
23
42
  wrangler secret 🤫 Generate a secret that can be referenced in the worker script
24
43
  wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
25
44
  wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
26
45
  wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
27
46
  wrangler pages ⚡️ Configure Cloudflare Pages
47
+ wrangler r2 📦 Interact with an R2 store
48
+ wrangler login 🔓 Login to Cloudflare
49
+ wrangler logout 🚪 Logout from Cloudflare
50
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
28
51
 
29
52
  Flags:
30
- -c, --config Path to .toml configuration file [string]
31
- -h, --help Show help [boolean]
32
- -v, --version Show version number [boolean]
33
-
34
- Options:
35
- -l, --local Run on my machine [boolean] [default: false]"
53
+ -c, --config Path to .toml configuration file [string]
54
+ -h, --help Show help [boolean]
55
+ -v, --version Show version number [boolean]
56
+ --legacy-env Use legacy environments [boolean]"
36
57
  `);
37
58
 
38
- expect(stderr).toMatchInlineSnapshot(`""`);
59
+ expect(std.err).toMatchInlineSnapshot(`""`);
39
60
  });
40
61
  });
41
62
 
42
63
  describe("invalid command", () => {
43
64
  it("should display an error", async () => {
44
- const { error, stdout, stderr } = await runWrangler("invalid-command");
65
+ await expect(
66
+ runWrangler("invalid-command")
67
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
68
+ `"Unknown argument: invalid-command"`
69
+ );
45
70
 
46
- expect(stdout).toMatchInlineSnapshot(`""`);
47
- expect(stderr).toMatchInlineSnapshot(`
71
+ expect(std.out).toMatchInlineSnapshot(`""`);
72
+ expect(std.err).toMatchInlineSnapshot(`
48
73
  "wrangler
49
74
 
50
75
  Commands:
51
76
  wrangler init [name] 📥 Create a wrangler.toml configuration file
52
- wrangler dev <filename> 👂 Start a local server for developing your worker
77
+ wrangler dev [script] 👂 Start a local server for developing your worker
53
78
  wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
54
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
79
+ wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
55
80
  wrangler secret 🤫 Generate a secret that can be referenced in the worker script
56
81
  wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
57
82
  wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
58
83
  wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
59
84
  wrangler pages ⚡️ Configure Cloudflare Pages
85
+ wrangler r2 📦 Interact with an R2 store
86
+ wrangler login 🔓 Login to Cloudflare
87
+ wrangler logout 🚪 Logout from Cloudflare
88
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
60
89
 
61
90
  Flags:
62
- -c, --config Path to .toml configuration file [string]
63
- -h, --help Show help [boolean]
64
- -v, --version Show version number [boolean]
91
+ -c, --config Path to .toml configuration file [string]
92
+ -h, --help Show help [boolean]
93
+ -v, --version Show version number [boolean]
94
+ --legacy-env Use legacy environments [boolean]
65
95
 
66
- Options:
67
- -l, --local Run on my machine [boolean] [default: false]
68
-
69
- Unknown command: invalid-command."
96
+ Unknown argument: invalid-command"
70
97
  `);
71
- expect(error).toMatchInlineSnapshot(
72
- `[Error: Unknown command: invalid-command.]`
73
- );
74
98
  });
75
99
  });
76
100
 
@@ -83,24 +107,79 @@ describe("wrangler", () => {
83
107
  await runWrangler("init");
84
108
  const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
85
109
  expect(typeof parsed.compatibility_date).toBe("string");
110
+ expect(parsed.name).toContain("wrangler-tests");
86
111
  expect(fs.existsSync("./package.json")).toBe(false);
87
112
  expect(fs.existsSync("./tsconfig.json")).toBe(false);
88
113
  });
89
114
 
115
+ it("should create a wrangler.toml and a directory for a named Worker ", async () => {
116
+ mockConfirm({
117
+ text: "No package.json found. Would you like to create one?",
118
+ result: false,
119
+ });
120
+ await runWrangler("init my-worker");
121
+ const parsed = TOML.parse(
122
+ await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
123
+ );
124
+ expect(typeof parsed.compatibility_date).toBe("string");
125
+ expect(parsed.name).toBe("my-worker");
126
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
127
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
128
+ });
129
+
90
130
  it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
91
- fs.writeFileSync("./wrangler.toml", "", "utf-8");
131
+ fs.writeFileSync(
132
+ "./wrangler.toml",
133
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
134
+ "utf-8"
135
+ );
92
136
  mockConfirm({
93
137
  text: "Do you want to continue initializing this project?",
94
138
  result: false,
95
139
  });
96
- const { warnings } = await runWrangler("init");
97
- expect(warnings).toContain("wrangler.toml file already exists!");
140
+ await runWrangler("init");
141
+ expect(std.warn).toContain("wrangler.toml file already exists!");
98
142
  const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
99
- expect(typeof parsed.compatibility_date).toBe("undefined");
143
+ expect(parsed.compatibility_date).toBe("something-else");
144
+ });
145
+
146
+ it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
147
+ fs.writeFileSync(
148
+ "./wrangler.toml",
149
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
150
+ "utf-8"
151
+ );
152
+ mockConfirm(
153
+ {
154
+ text: "Do you want to continue initializing this project?",
155
+ result: true,
156
+ },
157
+ {
158
+ text: "No package.json found. Would you like to create one?",
159
+ result: true,
160
+ },
161
+ {
162
+ text: "Would you like to use TypeScript?",
163
+ result: true,
164
+ },
165
+ {
166
+ text: "Would you like to create a Worker at src/index.ts?",
167
+ result: true,
168
+ }
169
+ );
170
+
171
+ await runWrangler("init");
172
+ expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
173
+ `"compatibility_date=\\"something-else\\""`
174
+ );
100
175
  });
101
176
 
102
177
  it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
103
- fs.writeFileSync("./wrangler.toml", "", "utf-8");
178
+ fs.writeFileSync(
179
+ "./wrangler.toml",
180
+ `compatibility_date="something-else"`,
181
+ "utf-8"
182
+ );
104
183
  mockConfirm(
105
184
  {
106
185
  text: "Do you want to continue initializing this project?",
@@ -111,10 +190,10 @@ describe("wrangler", () => {
111
190
  result: false,
112
191
  }
113
192
  );
114
- const { warnings } = await runWrangler("init");
115
- expect(warnings).toContain("wrangler.toml file already exists!");
193
+ await runWrangler("init");
194
+ expect(std.warn).toContain("wrangler.toml file already exists!");
116
195
  const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
117
- expect(typeof parsed.compatibility_date).toBe("string");
196
+ expect(parsed.compatibility_date).toBe("something-else");
118
197
  });
119
198
 
120
199
  it("should create a package.json if none is found and user confirms", async () => {
@@ -124,7 +203,11 @@ describe("wrangler", () => {
124
203
  result: true,
125
204
  },
126
205
  {
127
- text: "Would you like to use typescript?",
206
+ text: "Would you like to use TypeScript?",
207
+ result: false,
208
+ },
209
+ {
210
+ text: "Would you like to create a Worker at src/index.js?",
128
211
  result: false,
129
212
  }
130
213
  );
@@ -133,16 +216,82 @@ describe("wrangler", () => {
133
216
  const packageJson = JSON.parse(
134
217
  fs.readFileSync("./package.json", "utf-8")
135
218
  );
136
- expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
137
- expect(packageJson.version).toEqual("0.0.1");
219
+ expect(packageJson.name).toContain("wrangler-tests");
220
+ expect(packageJson.version).toEqual("0.0.0");
221
+ expect(packageJson.devDependencies).toEqual({
222
+ wrangler: expect.any(String),
223
+ });
138
224
  expect(fs.existsSync("./tsconfig.json")).toBe(false);
225
+ expect(mockPackageManager.install).toHaveBeenCalled();
226
+ });
227
+
228
+ it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
229
+ mockConfirm(
230
+ {
231
+ text: "No package.json found. Would you like to create one?",
232
+ result: true,
233
+ },
234
+ {
235
+ text: "Would you like to use TypeScript?",
236
+ result: false,
237
+ },
238
+ {
239
+ text: "Would you like to create a Worker at src/index.js?",
240
+ result: false,
241
+ }
242
+ );
243
+ await runWrangler("init my-worker");
244
+ const packageJson = JSON.parse(
245
+ fs.readFileSync("./my-worker/package.json", "utf-8")
246
+ );
247
+ expect(packageJson.name).toBe("my-worker");
139
248
  });
140
249
 
141
250
  it("should not touch an existing package.json in the same directory", async () => {
142
- mockConfirm({
143
- text: "Would you like to use typescript?",
144
- result: false,
145
- });
251
+ mockConfirm(
252
+ {
253
+ text: "Would you like to install wrangler into your package.json?",
254
+ result: false,
255
+ },
256
+ {
257
+ text: "Would you like to use TypeScript?",
258
+ result: false,
259
+ },
260
+ {
261
+ text: "Would you like to create a Worker at src/index.js?",
262
+ result: false,
263
+ }
264
+ );
265
+
266
+ fs.writeFileSync(
267
+ "./package.json",
268
+ JSON.stringify({ name: "test", version: "1.0.0" }),
269
+ "utf-8"
270
+ );
271
+
272
+ await runWrangler("init");
273
+ const packageJson = JSON.parse(
274
+ fs.readFileSync("./package.json", "utf-8")
275
+ );
276
+ expect(packageJson.name).toEqual("test");
277
+ expect(packageJson.version).toEqual("1.0.0");
278
+ });
279
+
280
+ it("should offer to install wrangler into an existing package.json", async () => {
281
+ mockConfirm(
282
+ {
283
+ text: "Would you like to install wrangler into your package.json?",
284
+ result: true,
285
+ },
286
+ {
287
+ text: "Would you like to use TypeScript?",
288
+ result: false,
289
+ },
290
+ {
291
+ text: "Would you like to create a Worker at src/index.js?",
292
+ result: false,
293
+ }
294
+ );
146
295
 
147
296
  fs.writeFileSync(
148
297
  "./package.json",
@@ -156,13 +305,26 @@ describe("wrangler", () => {
156
305
  );
157
306
  expect(packageJson.name).toEqual("test");
158
307
  expect(packageJson.version).toEqual("1.0.0");
308
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
309
+ `wrangler@${wranglerVersion}`
310
+ );
159
311
  });
160
312
 
161
313
  it("should not touch an existing package.json in an ancestor directory", async () => {
162
- mockConfirm({
163
- text: "Would you like to use typescript?",
164
- result: false,
165
- });
314
+ mockConfirm(
315
+ {
316
+ text: "Would you like to install wrangler into your package.json?",
317
+ result: false,
318
+ },
319
+ {
320
+ text: "Would you like to use TypeScript?",
321
+ result: false,
322
+ },
323
+ {
324
+ text: "Would you like to create a Worker at src/index.js?",
325
+ result: false,
326
+ }
327
+ );
166
328
 
167
329
  fs.writeFileSync(
168
330
  "./package.json",
@@ -180,8 +342,292 @@ describe("wrangler", () => {
180
342
  const packageJson = JSON.parse(
181
343
  fs.readFileSync("../../package.json", "utf-8")
182
344
  );
183
- expect(packageJson.name).toEqual("test");
184
- expect(packageJson.version).toEqual("1.0.0");
345
+ expect(packageJson).toMatchInlineSnapshot(`
346
+ Object {
347
+ "name": "test",
348
+ "version": "1.0.0",
349
+ }
350
+ `);
351
+ });
352
+
353
+ it("should offer to create a worker in a non-typescript project", async () => {
354
+ mockConfirm(
355
+ {
356
+ text: "Would you like to install wrangler into your package.json?",
357
+ result: false,
358
+ },
359
+ {
360
+ text: "Would you like to use TypeScript?",
361
+ result: false,
362
+ },
363
+ {
364
+ text: "Would you like to create a Worker at src/index.js?",
365
+ result: true,
366
+ }
367
+ );
368
+
369
+ fs.writeFileSync(
370
+ "./package.json",
371
+ JSON.stringify({ name: "test", version: "1.0.0" }),
372
+ "utf-8"
373
+ );
374
+
375
+ await runWrangler("init");
376
+ expect(fs.existsSync("./src/index.js")).toBe(true);
377
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
378
+ });
379
+
380
+ it("should offer to create a worker in a typescript project", async () => {
381
+ mockConfirm(
382
+ {
383
+ text: "Would you like to install wrangler into your package.json?",
384
+ result: false,
385
+ },
386
+ {
387
+ text: "Would you like to use TypeScript?",
388
+ result: true,
389
+ },
390
+ {
391
+ text: "Would you like to create a Worker at src/index.ts?",
392
+ result: true,
393
+ }
394
+ );
395
+
396
+ fs.writeFileSync(
397
+ "./package.json",
398
+ JSON.stringify({ name: "test", version: "1.0.0" }),
399
+ "utf-8"
400
+ );
401
+
402
+ await runWrangler("init");
403
+ expect(fs.existsSync("./src/index.js")).toBe(false);
404
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
405
+ });
406
+
407
+ it("should add scripts for a typescript project with .ts extension", async () => {
408
+ mockConfirm(
409
+ {
410
+ text: "No package.json found. Would you like to create one?",
411
+ result: true,
412
+ },
413
+ {
414
+ text: "Would you like to install wrangler into your package.json?",
415
+ result: false,
416
+ },
417
+ {
418
+ text: "Would you like to use TypeScript?",
419
+ result: true,
420
+ },
421
+ {
422
+ text: "Would you like to create a Worker at src/index.ts?",
423
+ result: true,
424
+ }
425
+ );
426
+ await runWrangler("init");
427
+
428
+ expect(fs.existsSync("./package.json")).toBe(true);
429
+ const packageJson = JSON.parse(
430
+ fs.readFileSync("./package.json", "utf-8")
431
+ );
432
+
433
+ expect(fs.existsSync("./src/index.js")).toBe(false);
434
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
435
+
436
+ expect(packageJson.scripts.start).toBe("wrangler dev");
437
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
438
+ expect(packageJson.name).toContain("wrangler-tests");
439
+ expect(packageJson.version).toEqual("0.0.0");
440
+ expect(std.out).toMatchInlineSnapshot(`
441
+ "✨ Successfully created wrangler.toml
442
+ ✨ Created package.json
443
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
444
+ ✨ Created src/index.ts
445
+
446
+ To start developing your Worker, run \`npm start\`
447
+ To publish your Worker to the Internet, run \`npm run publish\`"
448
+ `);
449
+ });
450
+
451
+ it("should not overwrite package.json scripts for a typescript project", async () => {
452
+ mockConfirm(
453
+ {
454
+ text: "Would you like to install wrangler into your package.json?",
455
+ result: false,
456
+ },
457
+ {
458
+ text: "Would you like to use TypeScript?",
459
+ result: true,
460
+ },
461
+ {
462
+ text: "Would you like to create a Worker at src/index.ts?",
463
+ result: true,
464
+ }
465
+ );
466
+ await fsp.writeFile(
467
+ "./package.json",
468
+ JSON.stringify({
469
+ scripts: {
470
+ start: "test-start",
471
+ publish: "test-publish",
472
+ },
473
+ })
474
+ );
475
+ const packageJson = JSON.parse(
476
+ fs.readFileSync("./package.json", "utf-8")
477
+ );
478
+ await runWrangler("init");
479
+
480
+ expect(fs.existsSync("./src/index.js")).toBe(false);
481
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
482
+
483
+ expect(packageJson.scripts.start).toBe("test-start");
484
+ expect(packageJson.scripts.publish).toBe("test-publish");
485
+ expect(std.out).toMatchInlineSnapshot(`
486
+ "✨ Successfully created wrangler.toml
487
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
488
+ ✨ Created src/index.ts
489
+
490
+ To start developing your Worker, run \`npx wrangler dev\`
491
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
492
+ `);
493
+ });
494
+
495
+ it("should add missing scripts for a non-ts project with .js extension", async () => {
496
+ mockConfirm(
497
+ {
498
+ text: "No package.json found. Would you like to create one?",
499
+ result: true,
500
+ },
501
+ {
502
+ text: "Would you like to install wrangler into your package.json?",
503
+ result: false,
504
+ },
505
+ {
506
+ text: "Would you like to use TypeScript?",
507
+ result: false,
508
+ },
509
+ {
510
+ text: "Would you like to create a Worker at src/index.js?",
511
+ result: true,
512
+ }
513
+ );
514
+ await runWrangler("init");
515
+
516
+ expect(fs.existsSync("./package.json")).toBe(true);
517
+ const packageJson = JSON.parse(
518
+ fs.readFileSync("./package.json", "utf-8")
519
+ );
520
+
521
+ expect(fs.existsSync("./src/index.js")).toBe(true);
522
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
523
+
524
+ expect(packageJson.scripts.start).toBe("wrangler dev");
525
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
526
+ expect(packageJson.name).toContain("wrangler-tests");
527
+ expect(packageJson.version).toEqual("0.0.0");
528
+ expect(std.out).toMatchInlineSnapshot(`
529
+ "✨ Successfully created wrangler.toml
530
+ ✨ Created package.json
531
+ ✨ Created src/index.js
532
+
533
+ To start developing your Worker, run \`npm start\`
534
+ To publish your Worker to the Internet, run \`npm run publish\`"
535
+ `);
536
+ });
537
+
538
+ it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
539
+ mockConfirm(
540
+ {
541
+ text: "Would you like to install wrangler into your package.json?",
542
+ result: false,
543
+ },
544
+ {
545
+ text: "Would you like to use TypeScript?",
546
+ result: false,
547
+ },
548
+ {
549
+ text: "Would you like to create a Worker at src/index.js?",
550
+ result: true,
551
+ }
552
+ );
553
+ await fsp.writeFile(
554
+ "./package.json",
555
+ JSON.stringify({
556
+ scripts: {
557
+ start: "test-start",
558
+ publish: "test-publish",
559
+ },
560
+ })
561
+ );
562
+ const packageJson = JSON.parse(
563
+ fs.readFileSync("./package.json", "utf-8")
564
+ );
565
+ await runWrangler("init");
566
+
567
+ expect(fs.existsSync("./src/index.js")).toBe(true);
568
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
569
+
570
+ expect(packageJson.scripts.start).toBe("test-start");
571
+ expect(packageJson.scripts.publish).toBe("test-publish");
572
+ expect(std.out).toMatchInlineSnapshot(`
573
+ "✨ Successfully created wrangler.toml
574
+ ✨ Created src/index.js
575
+
576
+ To start developing your Worker, run \`npx wrangler dev\`
577
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
578
+ `);
579
+ });
580
+
581
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
582
+ mockConfirm(
583
+ {
584
+ text: "Would you like to install wrangler into your package.json?",
585
+ result: false,
586
+ },
587
+ {
588
+ text: "Would you like to use TypeScript?",
589
+ result: false,
590
+ }
591
+ );
592
+
593
+ fs.writeFileSync(
594
+ "./package.json",
595
+ JSON.stringify({ name: "test", version: "1.0.0" }),
596
+ "utf-8"
597
+ );
598
+ fs.mkdirSync("./src", { recursive: true });
599
+ const PLACEHOLDER = "/* placeholder text */";
600
+ fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
601
+
602
+ await runWrangler("init");
603
+ expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
604
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
605
+ });
606
+
607
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
608
+ mockConfirm(
609
+ {
610
+ text: "Would you like to install wrangler into your package.json?",
611
+ result: false,
612
+ },
613
+ {
614
+ text: "Would you like to use TypeScript?",
615
+ result: true,
616
+ }
617
+ );
618
+
619
+ fs.writeFileSync(
620
+ "./package.json",
621
+ JSON.stringify({ name: "test", version: "1.0.0" }),
622
+ "utf-8"
623
+ );
624
+ fs.mkdirSync("./src", { recursive: true });
625
+ const PLACEHOLDER = "/* placeholder text */";
626
+ fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
627
+
628
+ await runWrangler("init");
629
+ expect(fs.existsSync("./src/index.js")).toBe(false);
630
+ expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
185
631
  });
186
632
 
187
633
  it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
@@ -191,30 +637,42 @@ describe("wrangler", () => {
191
637
  result: true,
192
638
  },
193
639
  {
194
- text: "Would you like to use typescript?",
640
+ text: "Would you like to use TypeScript?",
195
641
  result: true,
642
+ },
643
+ {
644
+ text: "Would you like to create a Worker at src/index.ts?",
645
+ result: false,
196
646
  }
197
647
  );
198
648
  await runWrangler("init");
199
649
  expect(fs.existsSync("./tsconfig.json")).toBe(true);
200
- const tsconfigJson = JSON.parse(
201
- fs.readFileSync("./tsconfig.json", "utf-8")
202
- );
650
+ const { config: tsconfigJson, error: tsConfigParseError } =
651
+ parseConfigFileTextToJson(
652
+ "./tsconfig.json",
653
+ fs.readFileSync("./tsconfig.json", "utf-8")
654
+ );
655
+ expect(tsConfigParseError).toBeUndefined();
203
656
  expect(tsconfigJson.compilerOptions.types).toEqual([
204
657
  "@cloudflare/workers-types",
205
658
  ]);
206
- const packageJson = JSON.parse(
207
- fs.readFileSync("./package.json", "utf-8")
659
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
660
+ "@cloudflare/workers-types",
661
+ "typescript"
208
662
  );
209
- expect(packageJson.devDependencies).toEqual({
210
- "@cloudflare/workers-types": expect.any(String),
211
- });
212
663
  });
213
664
 
214
665
  it("should not touch an existing tsconfig.json in the same directory", async () => {
215
666
  fs.writeFileSync(
216
667
  "./package.json",
217
- JSON.stringify({ name: "test", version: "1.0.0" }),
668
+ JSON.stringify({
669
+ name: "test",
670
+ version: "1.0.0",
671
+ devDependencies: {
672
+ wrangler: "0.0.0",
673
+ "@cloudflare/workers-types": "0.0.0",
674
+ },
675
+ }),
218
676
  "utf-8"
219
677
  );
220
678
  fs.writeFileSync(
@@ -230,10 +688,57 @@ describe("wrangler", () => {
230
688
  expect(tsconfigJson.compilerOptions).toEqual({});
231
689
  });
232
690
 
233
- it("should not touch an existing package.json in an ancestor directory", async () => {
691
+ it("should offer to install type definitions in an existing typescript project", async () => {
692
+ mockConfirm(
693
+ {
694
+ text: "Would you like to install wrangler into your package.json?",
695
+ result: false,
696
+ },
697
+ {
698
+ text: "Would you like to install the type definitions for Workers into your package.json?",
699
+ result: true,
700
+ },
701
+ {
702
+ text: "Would you like to create a Worker at src/index.ts?",
703
+ result: false,
704
+ }
705
+ );
234
706
  fs.writeFileSync(
235
707
  "./package.json",
236
- JSON.stringify({ name: "test", version: "1.0.0" }),
708
+ JSON.stringify({
709
+ name: "test",
710
+ version: "1.0.0",
711
+ }),
712
+ "utf-8"
713
+ );
714
+ fs.writeFileSync(
715
+ "./tsconfig.json",
716
+ JSON.stringify({ compilerOptions: {} }),
717
+ "utf-8"
718
+ );
719
+
720
+ await runWrangler("init");
721
+ const tsconfigJson = JSON.parse(
722
+ fs.readFileSync("./tsconfig.json", "utf-8")
723
+ );
724
+ // unchanged tsconfig
725
+ expect(tsconfigJson.compilerOptions).toEqual({});
726
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
727
+ "@cloudflare/workers-types"
728
+ );
729
+ });
730
+
731
+ it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
732
+ fs.writeFileSync(
733
+ "./package.json",
734
+ JSON.stringify({
735
+ name: "test",
736
+ version: "1.0.0",
737
+ devDependencies: {
738
+ wrangler: "0.0.0",
739
+ "@cloudflare/workers-types": "0.0.0",
740
+ },
741
+ }),
237
742
  "utf-8"
238
743
  );
239
744
  fs.writeFileSync(
@@ -255,33 +760,228 @@ describe("wrangler", () => {
255
760
  expect(tsconfigJson.compilerOptions).toEqual({});
256
761
  });
257
762
 
258
- it("should error if `--type` is used", async () => {
259
- const { error } = await runWrangler("init --type");
260
- expect(error).toMatchInlineSnapshot(
261
- `[Error: The --type option is no longer supported.]`
763
+ it("should initialize with no interactive prompts if `--yes` is used", async () => {
764
+ await runWrangler("init --yes");
765
+
766
+ expect(fs.existsSync("./src/index.js")).toBe(false);
767
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
768
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
769
+ expect(fs.existsSync("./package.json")).toBe(true);
770
+ expect(fs.existsSync("./wrangler.toml")).toBe(true);
771
+ expect(std.out).toMatchInlineSnapshot(`
772
+ "✨ Successfully created wrangler.toml
773
+ ✨ Created package.json
774
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
775
+ ✨ Created src/index.ts
776
+
777
+ To start developing your Worker, run \`npm start\`
778
+ To publish your Worker to the Internet, run \`npm run publish\`"
779
+ `);
780
+ expect(std.err).toMatchInlineSnapshot(`""`);
781
+ expect(std.warn).toMatchInlineSnapshot(`""`);
782
+ });
783
+
784
+ it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
785
+ await runWrangler("init my-worker --yes");
786
+
787
+ expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
788
+ expect(fs.existsSync("./my-worker/src/index.ts")).toBe(true);
789
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(true);
790
+ expect(fs.existsSync("./my-worker/package.json")).toBe(true);
791
+ expect(fs.existsSync("./my-worker/wrangler.toml")).toBe(true);
792
+ const parsedWranglerToml = TOML.parse(
793
+ fs.readFileSync("./my-worker/wrangler.toml", "utf-8")
262
794
  );
795
+ expect(parsedWranglerToml.main).toEqual("src/index.ts");
796
+ expect(std.out).toMatchInlineSnapshot(`
797
+ "✨ Successfully created wrangler.toml
798
+ ✨ Created package.json
799
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
800
+ ✨ Created src/index.ts
801
+
802
+ To start developing your Worker, run \`cd my-worker && npm start\`
803
+ To publish your Worker to the Internet, run \`npm run publish\`"
804
+ `);
805
+ expect(std.err).toMatchInlineSnapshot(`""`);
806
+ expect(std.warn).toMatchInlineSnapshot(`""`);
807
+ });
808
+
809
+ it("should initialize with no interactive prompts if `-y` is used", async () => {
810
+ await runWrangler("init -y");
811
+
812
+ expect(fs.existsSync("./src/index.js")).toBe(false);
813
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
814
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
815
+ expect(fs.existsSync("./package.json")).toBe(true);
816
+ expect(fs.existsSync("./wrangler.toml")).toBe(true);
263
817
  });
264
818
 
265
819
  it("should error if `--type javascript` is used", async () => {
266
- const { error } = await runWrangler("init --type javascript");
267
- expect(error).toMatchInlineSnapshot(
268
- `[Error: The --type option is no longer supported.]`
820
+ await expect(
821
+ runWrangler("init --type javascript")
822
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
823
+ `"The --type option is no longer supported."`
269
824
  );
270
825
  });
271
826
 
272
827
  it("should error if `--type rust` is used", async () => {
273
- const { error } = await runWrangler("init --type rust");
274
- expect(error).toMatchInlineSnapshot(
275
- `[Error: The --type option is no longer supported.]`
828
+ await expect(
829
+ runWrangler("init --type rust")
830
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
831
+ `"The --type option is no longer supported."`
276
832
  );
277
833
  });
278
834
 
279
835
  it("should error if `--type webpack` is used", async () => {
280
- const { error } = await runWrangler("init --type webpack");
281
- expect(error).toMatchInlineSnapshot(`
282
- [Error: The --type option is no longer supported.
283
- If you wish to use webpack then you will need to create a custom build.]
836
+ await expect(runWrangler("init --type webpack")).rejects
837
+ .toThrowErrorMatchingInlineSnapshot(`
838
+ "The --type option is no longer supported.
839
+ If you wish to use webpack then you will need to create a custom build."
840
+ `);
841
+ });
842
+ });
843
+
844
+ describe("preview", () => {
845
+ it("should throw an error if the deprecated command is used with positional arguments", async () => {
846
+ await expect(runWrangler("preview GET")).rejects
847
+ .toThrowErrorMatchingInlineSnapshot(`
848
+ "🦺 DEPRECATION:
849
+ The \`wrangler preview\` command has been deprecated.
850
+ Try using \`wrangler dev\` to to try out a worker during development.
851
+ "
852
+ `);
853
+ await expect(runWrangler(`preview GET "SomeBody"`)).rejects
854
+ .toThrowErrorMatchingInlineSnapshot(`
855
+ "🦺 DEPRECATION:
856
+ The \`wrangler preview\` command has been deprecated.
857
+ Try using \`wrangler dev\` to to try out a worker during development.
858
+ "
859
+ `);
860
+ });
861
+ });
862
+
863
+ describe("subcommand implicit help ran on imcomplete command execution", () => {
864
+ function endEventLoop() {
865
+ return new Promise((resolve) => setImmediate(resolve));
866
+ }
867
+ it("no subcommand for 'secret' should display a list of available subcommands", async () => {
868
+ await runWrangler("secret");
869
+ await endEventLoop();
870
+ expect(std.out).toMatchInlineSnapshot(`
871
+ "wrangler secret
872
+
873
+ 🤫 Generate a secret that can be referenced in the worker script
874
+
875
+ Commands:
876
+ wrangler secret put <key> Create or update a secret variable for a script
877
+ wrangler secret delete <key> Delete a secret variable from a script
878
+ wrangler secret list List all secrets for a script
879
+
880
+ Flags:
881
+ -c, --config Path to .toml configuration file [string]
882
+ -h, --help Show help [boolean]
883
+ -v, --version Show version number [boolean]
884
+ --legacy-env Use legacy environments [boolean]"
885
+ `);
886
+ });
887
+
888
+ it("no subcommand 'kv:namespace' should display a list of available subcommands", async () => {
889
+ await runWrangler("kv:namespace");
890
+ await endEventLoop();
891
+ expect(std.out).toMatchInlineSnapshot(`
892
+ "wrangler kv:namespace
893
+
894
+ 🗂️ Interact with your Workers KV Namespaces
895
+
896
+ Commands:
897
+ wrangler kv:namespace create <namespace> Create a new namespace
898
+ wrangler kv:namespace list Outputs a list of all KV namespaces associated with your account id.
899
+ wrangler kv:namespace delete Deletes a given namespace.
900
+
901
+ Flags:
902
+ -c, --config Path to .toml configuration file [string]
903
+ -h, --help Show help [boolean]
904
+ -v, --version Show version number [boolean]
905
+ --legacy-env Use legacy environments [boolean]"
284
906
  `);
285
907
  });
908
+
909
+ it("no subcommand 'kv:key' should display a list of available subcommands", async () => {
910
+ await runWrangler("kv:key");
911
+ await endEventLoop();
912
+ expect(std.out).toMatchInlineSnapshot(`
913
+ "wrangler kv:key
914
+
915
+ 🔑 Individually manage Workers KV key-value pairs
916
+
917
+ Commands:
918
+ wrangler kv:key put <key> [value] Writes a single key/value pair to the given namespace.
919
+ wrangler kv:key list Outputs a list of all keys in a given namespace.
920
+ wrangler kv:key get <key> Reads a single value by key from the given namespace.
921
+ wrangler kv:key delete <key> Removes a single key value pair from the given namespace.
922
+
923
+ Flags:
924
+ -c, --config Path to .toml configuration file [string]
925
+ -h, --help Show help [boolean]
926
+ -v, --version Show version number [boolean]
927
+ --legacy-env Use legacy environments [boolean]"
928
+ `);
929
+ });
930
+
931
+ it("no subcommand 'kv:bulk' should display a list of available subcommands", async () => {
932
+ await runWrangler("kv:bulk");
933
+ await endEventLoop();
934
+ expect(std.out).toMatchInlineSnapshot(`
935
+ "wrangler kv:bulk
936
+
937
+ 💪 Interact with multiple Workers KV key-value pairs at once
938
+
939
+ Commands:
940
+ wrangler kv:bulk put <filename> Upload multiple key-value pairs to a namespace
941
+ wrangler kv:bulk delete <filename> Delete multiple key-value pairs from a namespace
942
+
943
+ Flags:
944
+ -c, --config Path to .toml configuration file [string]
945
+ -h, --help Show help [boolean]
946
+ -v, --version Show version number [boolean]
947
+ --legacy-env Use legacy environments [boolean]"
948
+ `);
949
+ });
950
+ it("no subcommand 'r2' should display a list of available subcommands", async () => {
951
+ await runWrangler("r2");
952
+ await endEventLoop();
953
+ expect(std.out).toMatchInlineSnapshot(`
954
+ "wrangler r2
955
+
956
+ 📦 Interact with an R2 store
957
+
958
+ Commands:
959
+ wrangler r2 bucket Manage R2 buckets
960
+
961
+ Flags:
962
+ -c, --config Path to .toml configuration file [string]
963
+ -h, --help Show help [boolean]
964
+ -v, --version Show version number [boolean]
965
+ --legacy-env Use legacy environments [boolean]"
966
+ `);
967
+ });
968
+ });
969
+ describe("Deprecated commands", () => {
970
+ it("should print a deprecation message for 'generate'", async () => {
971
+ await runWrangler("generate").catch((err) => {
972
+ expect(err.message).toMatchInlineSnapshot(`
973
+ "🦺 DEPRECATION:
974
+ \`wrangler generate\` has been deprecated, please refer to https://github.com/cloudflare/wrangler2/blob/main/docs/deprecations.md#generate for alternatives"
975
+ `);
976
+ });
977
+ });
978
+ it("should print a deprecation message for 'build'", async () => {
979
+ await runWrangler("build").catch((err) => {
980
+ expect(err.message).toMatchInlineSnapshot(`
981
+ "🦺 DEPRECATION:
982
+ \`wrangler build\` has been deprecated, please refer to https://github.com/cloudflare/wrangler2/blob/main/docs/deprecations.md#build for alternatives"
983
+ `);
984
+ });
985
+ });
286
986
  });
287
987
  });