wrangler 0.0.0-e151223 → 0.0.0-e29a241

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