wrangler 0.0.0-e2f9bb2 → 0.0.0-e3278fa

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 (90) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +3 -1
  3. package/bin/wrangler.js +36 -4
  4. package/package.json +12 -7
  5. package/pages/functions/buildPlugin.ts +13 -0
  6. package/pages/functions/buildWorker.ts +14 -1
  7. package/pages/functions/template-plugin.ts +3 -2
  8. package/src/__tests__/configuration.test.ts +370 -87
  9. package/src/__tests__/dev.test.tsx +298 -77
  10. package/src/__tests__/helpers/mock-dialogs.ts +43 -2
  11. package/src/__tests__/index.test.ts +79 -72
  12. package/src/__tests__/init.test.ts +718 -123
  13. package/src/__tests__/jest.setup.ts +8 -0
  14. package/src/__tests__/kv.test.ts +173 -146
  15. package/src/__tests__/package-manager.test.ts +154 -7
  16. package/src/__tests__/pages.test.ts +478 -53
  17. package/src/__tests__/parse.test.ts +8 -0
  18. package/src/__tests__/publish.test.ts +1094 -215
  19. package/src/__tests__/r2.test.ts +55 -36
  20. package/src/__tests__/secret.test.ts +39 -4
  21. package/src/__tests__/whoami.test.tsx +34 -0
  22. package/src/abort.d.ts +3 -0
  23. package/src/bundle.ts +34 -6
  24. package/src/cfetch/index.ts +21 -4
  25. package/src/cfetch/internal.ts +23 -18
  26. package/src/config/config.ts +2 -2
  27. package/src/config/environment.ts +40 -14
  28. package/src/config/index.ts +162 -0
  29. package/src/config/validation-helpers.ts +10 -1
  30. package/src/config/validation.ts +180 -65
  31. package/src/create-worker-preview.ts +32 -22
  32. package/src/create-worker-upload-form.ts +22 -8
  33. package/src/dev/dev.tsx +12 -18
  34. package/src/dev/local.tsx +10 -0
  35. package/src/dev/remote.tsx +40 -16
  36. package/src/dialogs.tsx +48 -0
  37. package/src/durable.ts +102 -0
  38. package/src/entry.ts +64 -9
  39. package/src/index.tsx +548 -317
  40. package/src/inspect.ts +39 -0
  41. package/src/kv.ts +114 -25
  42. package/src/open-in-browser.ts +5 -12
  43. package/src/package-manager.ts +50 -3
  44. package/src/pages.tsx +434 -207
  45. package/src/parse.ts +23 -4
  46. package/src/proxy.ts +59 -35
  47. package/src/publish.ts +374 -127
  48. package/src/sites.tsx +61 -28
  49. package/src/user.tsx +23 -2
  50. package/src/whoami.tsx +3 -2
  51. package/src/worker.ts +10 -1
  52. package/src/zones.ts +73 -0
  53. package/templates/new-worker-scheduled.js +17 -0
  54. package/templates/new-worker-scheduled.ts +32 -0
  55. package/templates/new-worker.ts +16 -1
  56. package/templates/static-asset-facade.js +0 -4
  57. package/wrangler-dist/cli.js +96965 -83414
  58. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  59. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  60. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  61. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  62. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  63. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  64. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  65. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  66. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  67. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  68. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  69. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  70. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  71. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  72. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  73. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  74. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  75. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  76. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  77. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  78. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  79. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  80. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  81. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  82. package/vendor/wrangler-mime/LICENSE +0 -21
  83. package/vendor/wrangler-mime/Mime.js +0 -97
  84. package/vendor/wrangler-mime/README.md +0 -187
  85. package/vendor/wrangler-mime/cli.js +0 -46
  86. package/vendor/wrangler-mime/index.js +0 -4
  87. package/vendor/wrangler-mime/lite.js +0 -4
  88. package/vendor/wrangler-mime/package.json +0 -52
  89. package/vendor/wrangler-mime/types/other.js +0 -1
  90. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -2,14 +2,20 @@ import * as fs from "node:fs";
2
2
  import * as fsp from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import * as TOML from "@iarna/toml";
5
- import { execa } from "execa";
5
+ import { execa, execaSync } from "execa";
6
6
  import { parseConfigFileTextToJson } from "typescript";
7
7
  import { version as wranglerVersion } from "../../package.json";
8
8
  import { getPackageManager } from "../package-manager";
9
9
  import { mockConsoleMethods } from "./helpers/mock-console";
10
- import { mockConfirm, clearConfirmMocks } from "./helpers/mock-dialogs";
10
+ import {
11
+ mockConfirm,
12
+ clearConfirmMocks,
13
+ mockSelect,
14
+ clearSelectMocks,
15
+ } from "./helpers/mock-dialogs";
11
16
  import { runInTempDir } from "./helpers/run-in-tmp";
12
17
  import { runWrangler } from "./helpers/run-wrangler";
18
+ import type { RawConfig } from "../config";
13
19
  import type { PackageManager } from "../package-manager";
14
20
 
15
21
  describe("init", () => {
@@ -29,6 +35,7 @@ describe("init", () => {
29
35
 
30
36
  afterEach(() => {
31
37
  clearConfirmMocks();
38
+ clearSelectMocks();
32
39
  });
33
40
 
34
41
  const std = mockConsoleMethods();
@@ -43,14 +50,15 @@ describe("init", () => {
43
50
  expect(fs.existsSync("./package.json")).toBe(true);
44
51
  expect(fs.existsSync("./wrangler.toml")).toBe(true);
45
52
  expect(std.out).toMatchInlineSnapshot(`
46
- "✨ Successfully created wrangler.toml
53
+ "✨ Created wrangler.toml
47
54
  ✨ Initialized git repository
48
55
  ✨ Created package.json
49
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
56
+ ✨ Created tsconfig.json
50
57
  ✨ Created src/index.ts
58
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
51
59
 
52
60
  To start developing your Worker, run \`npm start\`
53
- To publish your Worker to the Internet, run \`npm run publish\`"
61
+ To publish your Worker to the Internet, run \`npm run deploy\`"
54
62
  `);
55
63
  expect(std.err).toMatchInlineSnapshot(`""`);
56
64
  expect(std.warn).toMatchInlineSnapshot(`""`);
@@ -69,14 +77,15 @@ describe("init", () => {
69
77
  );
70
78
  expect(parsedWranglerToml.main).toEqual("src/index.ts");
71
79
  expect(std.out).toMatchInlineSnapshot(`
72
- "✨ Successfully created wrangler.toml
73
- ✨ Initialized git repository
74
- ✨ Created package.json
75
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
76
- ✨ Created src/index.ts
80
+ "✨ Created my-worker/wrangler.toml
81
+ ✨ Initialized git repository at my-worker
82
+ ✨ Created my-worker/package.json
83
+ ✨ Created my-worker/tsconfig.json
84
+ ✨ Created my-worker/src/index.ts
85
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
77
86
 
78
87
  To start developing your Worker, run \`cd my-worker && npm start\`
79
- To publish your Worker to the Internet, run \`npm run publish\`"
88
+ To publish your Worker to the Internet, run \`npm run deploy\`"
80
89
  `);
81
90
  expect(std.err).toMatchInlineSnapshot(`""`);
82
91
  expect(std.warn).toMatchInlineSnapshot(`""`);
@@ -94,14 +103,15 @@ describe("init", () => {
94
103
  Object {
95
104
  "debug": "",
96
105
  "err": "",
97
- "out": "✨ Successfully created wrangler.toml
106
+ "out": "✨ Created wrangler.toml
98
107
  ✨ Initialized git repository
99
108
  ✨ Created package.json
100
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
109
+ ✨ Created tsconfig.json
101
110
  ✨ Created src/index.ts
111
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
102
112
 
103
113
  To start developing your Worker, run \`npm start\`
104
- To publish your Worker to the Internet, run \`npm run publish\`",
114
+ To publish your Worker to the Internet, run \`npm run deploy\`",
105
115
  "warn": "",
106
116
  }
107
117
  `);
@@ -166,6 +176,14 @@ describe("init", () => {
166
176
  expect(parsed.name).toContain("wrangler-tests");
167
177
  expect(fs.existsSync("./package.json")).toBe(false);
168
178
  expect(fs.existsSync("./tsconfig.json")).toBe(false);
179
+ expect(std).toMatchInlineSnapshot(`
180
+ Object {
181
+ "debug": "",
182
+ "err": "",
183
+ "out": "✨ Created wrangler.toml",
184
+ "warn": "",
185
+ }
186
+ `);
169
187
  });
170
188
 
171
189
  it("should create a wrangler.toml and a directory for a named Worker ", async () => {
@@ -187,6 +205,15 @@ describe("init", () => {
187
205
  expect(parsed.name).toBe("my-worker");
188
206
  expect(fs.existsSync("./my-worker/package.json")).toBe(false);
189
207
  expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
208
+
209
+ expect(std).toMatchInlineSnapshot(`
210
+ Object {
211
+ "debug": "",
212
+ "err": "",
213
+ "out": "✨ Created my-worker/wrangler.toml",
214
+ "warn": "",
215
+ }
216
+ `);
190
217
  });
191
218
 
192
219
  it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
@@ -200,9 +227,49 @@ describe("init", () => {
200
227
  result: false,
201
228
  });
202
229
  await runWrangler("init");
203
- expect(std.warn).toContain("wrangler.toml file already exists!");
230
+ expect(std.warn).toContain("wrangler.toml already exists!");
204
231
  const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
205
232
  expect(parsed.compatibility_date).toBe("something-else");
233
+
234
+ expect(std).toMatchInlineSnapshot(`
235
+ Object {
236
+ "debug": "",
237
+ "err": "",
238
+ "out": "",
239
+ "warn": "▲ [WARNING] wrangler.toml already exists!
240
+
241
+ ",
242
+ }
243
+ `);
244
+ });
245
+
246
+ it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
247
+ fs.mkdirSync("path/to/worker", { recursive: true });
248
+ fs.writeFileSync(
249
+ "path/to/worker/wrangler.toml",
250
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
251
+ "utf-8"
252
+ );
253
+ mockConfirm({
254
+ text: "Do you want to continue initializing this project?",
255
+ result: false,
256
+ });
257
+ await runWrangler("init path/to/worker");
258
+ expect(std.warn).toContain("wrangler.toml already exists!");
259
+ const parsed = TOML.parse(
260
+ await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
261
+ );
262
+ expect(parsed.compatibility_date).toBe("something-else");
263
+ expect(std).toMatchInlineSnapshot(`
264
+ Object {
265
+ "debug": "",
266
+ "err": "",
267
+ "out": "",
268
+ "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
269
+
270
+ ",
271
+ }
272
+ `);
206
273
  });
207
274
 
208
275
  it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
@@ -227,13 +294,14 @@ describe("init", () => {
227
294
  {
228
295
  text: "Would you like to use TypeScript?",
229
296
  result: true,
230
- },
231
- {
232
- text: "Would you like to create a Worker at src/index.ts?",
233
- result: true,
234
297
  }
235
298
  );
236
299
 
300
+ mockSelect({
301
+ text: "Would you like to create a Worker at src/index.ts?",
302
+ result: "fetch",
303
+ });
304
+
237
305
  await runWrangler("init");
238
306
  expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
239
307
  `"compatibility_date=\\"something-else\\""`
@@ -261,9 +329,85 @@ describe("init", () => {
261
329
  }
262
330
  );
263
331
  await runWrangler("init");
264
- expect(std.warn).toContain("wrangler.toml file already exists!");
332
+ expect(std.warn).toContain("wrangler.toml already exists!");
265
333
  const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
266
334
  expect(parsed.compatibility_date).toBe("something-else");
335
+ expect(std).toMatchInlineSnapshot(`
336
+ Object {
337
+ "debug": "",
338
+ "err": "",
339
+ "out": "",
340
+ "warn": "▲ [WARNING] wrangler.toml already exists!
341
+
342
+ ",
343
+ }
344
+ `);
345
+ });
346
+
347
+ it("should not add a Cron Trigger to wrangler.toml when creating a Scheduled Worker if wrangler.toml already exists", async () => {
348
+ fs.writeFileSync(
349
+ "./wrangler.toml",
350
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
351
+ "utf-8"
352
+ );
353
+ mockConfirm(
354
+ {
355
+ text: "Would you like to use git to manage this Worker?",
356
+ result: true,
357
+ },
358
+ {
359
+ text: "Do you want to continue initializing this project?",
360
+ result: true,
361
+ },
362
+ {
363
+ text: "No package.json found. Would you like to create one?",
364
+ result: true,
365
+ },
366
+ {
367
+ text: "Would you like to use TypeScript?",
368
+ result: true,
369
+ }
370
+ );
371
+
372
+ mockSelect({
373
+ text: "Would you like to create a Worker at src/index.ts?",
374
+ result: "scheduled",
375
+ });
376
+
377
+ await runWrangler("init");
378
+ expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
379
+ `"compatibility_date=\\"something-else\\""`
380
+ );
381
+ });
382
+
383
+ it("should add a Cron Trigger to wrangler.toml when creating a Scheduled Worker, but only if wrangler.toml is being created during init", async () => {
384
+ mockConfirm(
385
+ {
386
+ text: "Would you like to use git to manage this Worker?",
387
+ result: true,
388
+ },
389
+ {
390
+ text: "No package.json found. Would you like to create one?",
391
+ result: true,
392
+ },
393
+ {
394
+ text: "Would you like to use TypeScript?",
395
+ result: true,
396
+ }
397
+ );
398
+
399
+ mockSelect({
400
+ text: "Would you like to create a Worker at src/index.ts?",
401
+ result: "scheduled",
402
+ });
403
+
404
+ await runWrangler("init");
405
+ const parsed = TOML.parse(
406
+ await fsp.readFile("./wrangler.toml", "utf-8")
407
+ ) as RawConfig;
408
+ expect(typeof parsed.compatibility_date).toBe("string");
409
+ expect(parsed.name).toContain("wrangler-tests");
410
+ expect(parsed?.triggers?.crons[0]).toEqual("1 * * * *");
267
411
  });
268
412
  });
269
413
 
@@ -284,13 +428,16 @@ describe("init", () => {
284
428
  Object {
285
429
  "debug": "",
286
430
  "err": "",
287
- "out": "✨ Successfully created wrangler.toml
431
+ "out": "✨ Created wrangler.toml
288
432
  ✨ Initialized git repository",
289
433
  "warn": "",
290
434
  }
291
435
  `);
292
436
  expect(fs.lstatSync(".git").isDirectory()).toBe(true);
293
437
  expect(fs.lstatSync(".gitignore").isFile()).toBe(true);
438
+ expect((await execa("git", ["branch", "--show-current"])).stdout).toEqual(
439
+ "main"
440
+ );
294
441
  });
295
442
 
296
443
  it("should not offer to initialize a git repo if it's already inside one", async () => {
@@ -304,23 +451,75 @@ describe("init", () => {
304
451
  Object {
305
452
  "debug": "",
306
453
  "err": "",
307
- "out": "✨ Successfully created wrangler.toml
308
- ✨ Initialized git repository
454
+ "out": "✨ Created wrangler.toml
309
455
  ✨ Created package.json
310
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
456
+ ✨ Created tsconfig.json
311
457
  ✨ Created src/index.ts
458
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
312
459
 
313
460
  To start developing your Worker, run \`npm start\`
314
- To publish your Worker to the Internet, run \`npm run publish\`",
461
+ To publish your Worker to the Internet, run \`npm run deploy\`",
462
+ "warn": "",
463
+ }
464
+ `);
465
+ });
466
+
467
+ it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
468
+ fs.mkdirSync("path/to/worker", { recursive: true });
469
+ await execa("git", ["init"], { cwd: "path/to/worker" });
470
+ expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
471
+
472
+ await runWrangler("init path/to/worker/my-worker -y");
473
+
474
+ // Note the lack of "✨ Initialized git repository" in the log
475
+ expect(std).toMatchInlineSnapshot(`
476
+ Object {
477
+ "debug": "",
478
+ "err": "",
479
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
480
+ ✨ Created path/to/worker/my-worker/package.json
481
+ ✨ Created path/to/worker/my-worker/tsconfig.json
482
+ ✨ Created path/to/worker/my-worker/src/index.ts
483
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
484
+
485
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
486
+ To publish your Worker to the Internet, run \`npm run deploy\`",
315
487
  "warn": "",
316
488
  }
317
489
  `);
318
490
  });
319
491
 
492
+ // I... don't know how to test this lol
320
493
  it.todo(
321
494
  "should not offer to initialize a git repo if git is not installed"
322
495
  );
323
- // I... don't know how to test this lol
496
+
497
+ it("should initialize git repo with `main` default branch", async () => {
498
+ mockConfirm(
499
+ {
500
+ text: "Would you like to use git to manage this Worker?",
501
+ result: true,
502
+ },
503
+ {
504
+ text: "No package.json found. Would you like to create one?",
505
+ result: false,
506
+ }
507
+ );
508
+ await runWrangler("init");
509
+ expect(std).toMatchInlineSnapshot(`
510
+ Object {
511
+ "debug": "",
512
+ "err": "",
513
+ "out": "✨ Created wrangler.toml
514
+ ✨ Initialized git repository",
515
+ "warn": "",
516
+ }
517
+ `);
518
+
519
+ expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
520
+ "refs/heads/main"
521
+ );
522
+ });
324
523
  });
325
524
 
326
525
  describe("package.json", () => {
@@ -337,12 +536,12 @@ describe("init", () => {
337
536
  {
338
537
  text: "Would you like to use TypeScript?",
339
538
  result: false,
340
- },
341
- {
342
- text: "Would you like to create a Worker at src/index.js?",
343
- result: false,
344
539
  }
345
540
  );
541
+ mockSelect({
542
+ text: "Would you like to create a Worker at src/index.js?",
543
+ result: "none",
544
+ });
346
545
  await runWrangler("init");
347
546
  expect(fs.existsSync("./package.json")).toBe(true);
348
547
  const packageJson = JSON.parse(
@@ -355,6 +554,15 @@ describe("init", () => {
355
554
  });
356
555
  expect(fs.existsSync("./tsconfig.json")).toBe(false);
357
556
  expect(mockPackageManager.install).toHaveBeenCalled();
557
+ expect(std).toMatchInlineSnapshot(`
558
+ Object {
559
+ "debug": "",
560
+ "err": "",
561
+ "out": "✨ Created wrangler.toml
562
+ ✨ Created package.json",
563
+ "warn": "",
564
+ }
565
+ `);
358
566
  });
359
567
 
360
568
  it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
@@ -370,17 +578,26 @@ describe("init", () => {
370
578
  {
371
579
  text: "Would you like to use TypeScript?",
372
580
  result: false,
373
- },
374
- {
375
- text: "Would you like to create a Worker at src/index.js?",
376
- result: false,
377
581
  }
378
582
  );
583
+ mockSelect({
584
+ text: "Would you like to create a Worker at my-worker/src/index.js?",
585
+ result: "none",
586
+ });
379
587
  await runWrangler("init my-worker");
380
588
  const packageJson = JSON.parse(
381
589
  fs.readFileSync("./my-worker/package.json", "utf-8")
382
590
  );
383
591
  expect(packageJson.name).toBe("my-worker");
592
+ expect(std).toMatchInlineSnapshot(`
593
+ Object {
594
+ "debug": "",
595
+ "err": "",
596
+ "out": "✨ Created my-worker/wrangler.toml
597
+ ✨ Created my-worker/package.json",
598
+ "warn": "",
599
+ }
600
+ `);
384
601
  });
385
602
 
386
603
  it("should not touch an existing package.json in the same directory", async () => {
@@ -390,18 +607,18 @@ describe("init", () => {
390
607
  result: false,
391
608
  },
392
609
  {
393
- text: "Would you like to install wrangler into your package.json?",
610
+ text: "Would you like to install wrangler into package.json?",
394
611
  result: false,
395
612
  },
396
613
  {
397
614
  text: "Would you like to use TypeScript?",
398
615
  result: false,
399
- },
400
- {
401
- text: "Would you like to create a Worker at src/index.js?",
402
- result: false,
403
616
  }
404
617
  );
618
+ mockSelect({
619
+ text: "Would you like to create a Worker at src/index.js?",
620
+ result: "none",
621
+ });
405
622
 
406
623
  fs.writeFileSync(
407
624
  "./package.json",
@@ -415,28 +632,81 @@ describe("init", () => {
415
632
  );
416
633
  expect(packageJson.name).toEqual("test");
417
634
  expect(packageJson.version).toEqual("1.0.0");
635
+ expect(std).toMatchInlineSnapshot(`
636
+ Object {
637
+ "debug": "",
638
+ "err": "",
639
+ "out": "✨ Created wrangler.toml",
640
+ "warn": "",
641
+ }
642
+ `);
418
643
  });
419
644
 
420
- it("should offer to install wrangler into an existing package.json", async () => {
645
+ it("should not touch an existing package.json in a target directory", async () => {
421
646
  mockConfirm(
422
647
  {
423
648
  text: "Would you like to use git to manage this Worker?",
424
649
  result: false,
425
650
  },
426
651
  {
427
- text: "Would you like to install wrangler into your package.json?",
428
- result: true,
652
+ text: "Would you like to install wrangler into path/to/worker/package.json?",
653
+ result: false,
429
654
  },
430
655
  {
431
656
  text: "Would you like to use TypeScript?",
432
657
  result: false,
658
+ }
659
+ );
660
+
661
+ mockSelect({
662
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
663
+ result: "none",
664
+ });
665
+
666
+ fs.mkdirSync("path/to/worker", { recursive: true });
667
+ fs.writeFileSync(
668
+ "path/to/worker/package.json",
669
+ JSON.stringify({ name: "test", version: "1.0.0" }),
670
+ "utf-8"
671
+ );
672
+
673
+ await runWrangler("init path/to/worker/my-worker");
674
+ const packageJson = JSON.parse(
675
+ fs.readFileSync("path/to/worker/package.json", "utf-8")
676
+ );
677
+ expect(packageJson.name).toEqual("test");
678
+ expect(packageJson.version).toEqual("1.0.0");
679
+ expect(std).toMatchInlineSnapshot(`
680
+ Object {
681
+ "debug": "",
682
+ "err": "",
683
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml",
684
+ "warn": "",
685
+ }
686
+ `);
687
+ });
688
+
689
+ it("should offer to install wrangler into an existing package.json", async () => {
690
+ mockConfirm(
691
+ {
692
+ text: "Would you like to use git to manage this Worker?",
693
+ result: false,
694
+ },
695
+ {
696
+ text: "Would you like to install wrangler into package.json?",
697
+ result: true,
433
698
  },
434
699
  {
435
- text: "Would you like to create a Worker at src/index.js?",
700
+ text: "Would you like to use TypeScript?",
436
701
  result: false,
437
702
  }
438
703
  );
439
704
 
705
+ mockSelect({
706
+ text: "Would you like to create a Worker at src/index.js?",
707
+ result: "none",
708
+ });
709
+
440
710
  fs.writeFileSync(
441
711
  "./package.json",
442
712
  JSON.stringify({ name: "test", version: "1.0.0" }),
@@ -452,28 +722,87 @@ describe("init", () => {
452
722
  expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
453
723
  `wrangler@${wranglerVersion}`
454
724
  );
725
+ expect(std).toMatchInlineSnapshot(`
726
+ Object {
727
+ "debug": "",
728
+ "err": "",
729
+ "out": "✨ Created wrangler.toml
730
+ ✨ Installed wrangler into devDependencies",
731
+ "warn": "",
732
+ }
733
+ `);
455
734
  });
456
735
 
457
- it("should not touch an existing package.json in an ancestor directory", async () => {
736
+ it("should offer to install wrangler into a package.json relative to the target directory", async () => {
458
737
  mockConfirm(
459
738
  {
460
739
  text: "Would you like to use git to manage this Worker?",
461
740
  result: false,
462
741
  },
463
742
  {
464
- text: "Would you like to install wrangler into your package.json?",
465
- result: false,
743
+ text: "Would you like to install wrangler into path/to/worker/package.json?",
744
+ result: true,
466
745
  },
467
746
  {
468
747
  text: "Would you like to use TypeScript?",
469
748
  result: false,
749
+ }
750
+ );
751
+
752
+ mockSelect({
753
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
754
+ result: "none",
755
+ });
756
+
757
+ fs.mkdirSync("path/to/worker", { recursive: true });
758
+ fs.writeFileSync(
759
+ "path/to/worker/package.json",
760
+ JSON.stringify({ name: "test", version: "1.0.0" }),
761
+ "utf-8"
762
+ );
763
+
764
+ await runWrangler("init path/to/worker/my-worker");
765
+ const packageJson = JSON.parse(
766
+ fs.readFileSync("path/to/worker/package.json", "utf-8")
767
+ );
768
+ expect(packageJson.name).toEqual("test");
769
+ expect(packageJson.version).toEqual("1.0.0");
770
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
771
+ `wrangler@${wranglerVersion}`
772
+ );
773
+ expect(mockPackageManager.cwd).toBe(process.cwd());
774
+ expect(std).toMatchInlineSnapshot(`
775
+ Object {
776
+ "debug": "",
777
+ "err": "",
778
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
779
+ ✨ Installed wrangler into devDependencies",
780
+ "warn": "",
781
+ }
782
+ `);
783
+ });
784
+
785
+ it("should not touch an existing package.json in an ancestor directory", async () => {
786
+ mockConfirm(
787
+ {
788
+ text: "Would you like to use git to manage this Worker?",
789
+ result: false,
470
790
  },
471
791
  {
472
- text: "Would you like to create a Worker at src/index.js?",
792
+ text: "Would you like to install wrangler into ../../package.json?",
793
+ result: false,
794
+ },
795
+ {
796
+ text: "Would you like to use TypeScript?",
473
797
  result: false,
474
798
  }
475
799
  );
476
800
 
801
+ mockSelect({
802
+ text: "Would you like to create a Worker at src/index.js?",
803
+ result: "none",
804
+ });
805
+
477
806
  fs.writeFileSync(
478
807
  "./package.json",
479
808
  JSON.stringify({ name: "test", version: "1.0.0" }),
@@ -496,6 +825,14 @@ describe("init", () => {
496
825
  "version": "1.0.0",
497
826
  }
498
827
  `);
828
+ expect(std).toMatchInlineSnapshot(`
829
+ Object {
830
+ "debug": "",
831
+ "err": "",
832
+ "out": "✨ Created wrangler.toml",
833
+ "warn": "",
834
+ }
835
+ `);
499
836
  });
500
837
  });
501
838
 
@@ -507,19 +844,20 @@ describe("init", () => {
507
844
  result: false,
508
845
  },
509
846
  {
510
- text: "Would you like to install wrangler into your package.json?",
847
+ text: "Would you like to install wrangler into package.json?",
511
848
  result: false,
512
849
  },
513
850
  {
514
851
  text: "Would you like to use TypeScript?",
515
852
  result: false,
516
- },
517
- {
518
- text: "Would you like to create a Worker at src/index.js?",
519
- result: true,
520
853
  }
521
854
  );
522
855
 
856
+ mockSelect({
857
+ text: "Would you like to create a Worker at src/index.js?",
858
+ result: "fetch",
859
+ });
860
+
523
861
  fs.writeFileSync(
524
862
  "./package.json",
525
863
  JSON.stringify({ name: "test", version: "1.0.0" }),
@@ -529,6 +867,18 @@ describe("init", () => {
529
867
  await runWrangler("init");
530
868
  expect(fs.existsSync("./src/index.js")).toBe(true);
531
869
  expect(fs.existsSync("./src/index.ts")).toBe(false);
870
+ expect(std).toMatchInlineSnapshot(`
871
+ Object {
872
+ "debug": "",
873
+ "err": "",
874
+ "out": "✨ Created wrangler.toml
875
+ ✨ Created src/index.js
876
+
877
+ To start developing your Worker, run \`npx wrangler dev\`
878
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
879
+ "warn": "",
880
+ }
881
+ `);
532
882
  });
533
883
 
534
884
  it("should offer to create a worker in a typescript project", async () => {
@@ -538,19 +888,20 @@ describe("init", () => {
538
888
  result: false,
539
889
  },
540
890
  {
541
- text: "Would you like to install wrangler into your package.json?",
891
+ text: "Would you like to install wrangler into package.json?",
542
892
  result: false,
543
893
  },
544
894
  {
545
895
  text: "Would you like to use TypeScript?",
546
896
  result: true,
547
- },
548
- {
549
- text: "Would you like to create a Worker at src/index.ts?",
550
- result: true,
551
897
  }
552
898
  );
553
899
 
900
+ mockSelect({
901
+ text: "Would you like to create a Worker at src/index.ts?",
902
+ result: "fetch",
903
+ });
904
+
554
905
  fs.writeFileSync(
555
906
  "./package.json",
556
907
  JSON.stringify({ name: "test", version: "1.0.0" }),
@@ -560,6 +911,20 @@ describe("init", () => {
560
911
  await runWrangler("init");
561
912
  expect(fs.existsSync("./src/index.js")).toBe(false);
562
913
  expect(fs.existsSync("./src/index.ts")).toBe(true);
914
+ expect(std).toMatchInlineSnapshot(`
915
+ Object {
916
+ "debug": "",
917
+ "err": "",
918
+ "out": "✨ Created wrangler.toml
919
+ ✨ Created tsconfig.json
920
+ ✨ Created src/index.ts
921
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
922
+
923
+ To start developing your Worker, run \`npx wrangler dev\`
924
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
925
+ "warn": "",
926
+ }
927
+ `);
563
928
  });
564
929
 
565
930
  it("should add scripts for a typescript project with .ts extension", async () => {
@@ -573,18 +938,18 @@ describe("init", () => {
573
938
  result: true,
574
939
  },
575
940
  {
576
- text: "Would you like to install wrangler into your package.json?",
941
+ text: "Would you like to install wrangler into package.json?",
577
942
  result: false,
578
943
  },
579
944
  {
580
945
  text: "Would you like to use TypeScript?",
581
946
  result: true,
582
- },
583
- {
584
- text: "Would you like to create a Worker at src/index.ts?",
585
- result: true,
586
947
  }
587
948
  );
949
+ mockSelect({
950
+ text: "Would you like to create a Worker at src/index.ts?",
951
+ result: "fetch",
952
+ });
588
953
  await runWrangler("init");
589
954
 
590
955
  expect(fs.existsSync("./package.json")).toBe(true);
@@ -596,17 +961,18 @@ describe("init", () => {
596
961
  expect(fs.existsSync("./src/index.ts")).toBe(true);
597
962
 
598
963
  expect(packageJson.scripts.start).toBe("wrangler dev");
599
- expect(packageJson.scripts.publish).toBe("wrangler publish");
964
+ expect(packageJson.scripts.deploy).toBe("wrangler publish");
600
965
  expect(packageJson.name).toContain("wrangler-tests");
601
966
  expect(packageJson.version).toEqual("0.0.0");
602
967
  expect(std.out).toMatchInlineSnapshot(`
603
- "✨ Successfully created wrangler.toml
968
+ "✨ Created wrangler.toml
604
969
  ✨ Created package.json
605
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
970
+ ✨ Created tsconfig.json
606
971
  ✨ Created src/index.ts
972
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
607
973
 
608
974
  To start developing your Worker, run \`npm start\`
609
- To publish your Worker to the Internet, run \`npm run publish\`"
975
+ To publish your Worker to the Internet, run \`npm run deploy\`"
610
976
  `);
611
977
  });
612
978
 
@@ -617,24 +983,24 @@ describe("init", () => {
617
983
  result: false,
618
984
  },
619
985
  {
620
- text: "Would you like to install wrangler into your package.json?",
986
+ text: "Would you like to install wrangler into package.json?",
621
987
  result: false,
622
988
  },
623
989
  {
624
990
  text: "Would you like to use TypeScript?",
625
991
  result: true,
626
- },
627
- {
628
- text: "Would you like to create a Worker at src/index.ts?",
629
- result: true,
630
992
  }
631
993
  );
994
+ mockSelect({
995
+ text: "Would you like to create a Worker at src/index.ts?",
996
+ result: "fetch",
997
+ });
632
998
  await fsp.writeFile(
633
999
  "./package.json",
634
1000
  JSON.stringify({
635
1001
  scripts: {
636
1002
  start: "test-start",
637
- publish: "test-publish",
1003
+ deploy: "test-publish",
638
1004
  },
639
1005
  })
640
1006
  );
@@ -647,11 +1013,12 @@ describe("init", () => {
647
1013
  expect(fs.existsSync("./src/index.ts")).toBe(true);
648
1014
 
649
1015
  expect(packageJson.scripts.start).toBe("test-start");
650
- expect(packageJson.scripts.publish).toBe("test-publish");
1016
+ expect(packageJson.scripts.deploy).toBe("test-publish");
651
1017
  expect(std.out).toMatchInlineSnapshot(`
652
- "✨ Successfully created wrangler.toml
653
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
1018
+ "✨ Created wrangler.toml
1019
+ ✨ Created tsconfig.json
654
1020
  ✨ Created src/index.ts
1021
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
655
1022
 
656
1023
  To start developing your Worker, run \`npx wrangler dev\`
657
1024
  To publish your Worker to the Internet, run \`npx wrangler publish\`"
@@ -665,7 +1032,7 @@ describe("init", () => {
665
1032
  result: false,
666
1033
  },
667
1034
  {
668
- text: "Would you like to install wrangler into your package.json?",
1035
+ text: "Would you like to install wrangler into package.json?",
669
1036
  result: false,
670
1037
  },
671
1038
  {
@@ -686,6 +1053,58 @@ describe("init", () => {
686
1053
  await runWrangler("init");
687
1054
  expect(fs.existsSync("./src/index.js")).toBe(false);
688
1055
  expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
1056
+ expect(std).toMatchInlineSnapshot(`
1057
+ Object {
1058
+ "debug": "",
1059
+ "err": "",
1060
+ "out": "✨ Created wrangler.toml
1061
+ ✨ Created tsconfig.json
1062
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1063
+ "warn": "",
1064
+ }
1065
+ `);
1066
+ });
1067
+
1068
+ it("should not offer to create a worker in a ts project for a named worker if a file already exists at the location", async () => {
1069
+ mockConfirm(
1070
+ {
1071
+ text: "Would you like to use git to manage this Worker?",
1072
+ result: false,
1073
+ },
1074
+ {
1075
+ text: "Would you like to install wrangler into package.json?",
1076
+ result: false,
1077
+ },
1078
+ {
1079
+ text: "Would you like to use TypeScript?",
1080
+ result: true,
1081
+ }
1082
+ );
1083
+
1084
+ fs.writeFileSync(
1085
+ "./package.json",
1086
+ JSON.stringify({ name: "test", version: "1.0.0" }),
1087
+ "utf-8"
1088
+ );
1089
+ fs.mkdirSync("./my-worker/src", { recursive: true });
1090
+ const PLACEHOLDER = "/* placeholder text */";
1091
+ fs.writeFileSync("./my-worker/src/index.ts", PLACEHOLDER, "utf-8");
1092
+
1093
+ await runWrangler("init my-worker");
1094
+ expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
1095
+ expect(fs.readFileSync("./my-worker/src/index.ts", "utf-8")).toBe(
1096
+ PLACEHOLDER
1097
+ );
1098
+ expect(std).toMatchInlineSnapshot(`
1099
+ Object {
1100
+ "debug": "",
1101
+ "err": "",
1102
+ "out": "✨ Created my-worker/wrangler.toml
1103
+ ✨ Created my-worker/tsconfig.json
1104
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1105
+ "warn": "",
1106
+ }
1107
+ `);
689
1108
  });
690
1109
 
691
1110
  it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
@@ -701,12 +1120,12 @@ describe("init", () => {
701
1120
  {
702
1121
  text: "Would you like to use TypeScript?",
703
1122
  result: true,
704
- },
705
- {
706
- text: "Would you like to create a Worker at src/index.ts?",
707
- result: false,
708
1123
  }
709
1124
  );
1125
+ mockSelect({
1126
+ text: "Would you like to create a Worker at src/index.ts?",
1127
+ result: "none",
1128
+ });
710
1129
  await runWrangler("init");
711
1130
  expect(fs.existsSync("./tsconfig.json")).toBe(true);
712
1131
  const { config: tsconfigJson, error: tsConfigParseError } =
@@ -722,6 +1141,17 @@ describe("init", () => {
722
1141
  "@cloudflare/workers-types",
723
1142
  "typescript"
724
1143
  );
1144
+ expect(std).toMatchInlineSnapshot(`
1145
+ Object {
1146
+ "debug": "",
1147
+ "err": "",
1148
+ "out": "✨ Created wrangler.toml
1149
+ ✨ Created package.json
1150
+ ✨ Created tsconfig.json
1151
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1152
+ "warn": "",
1153
+ }
1154
+ `);
725
1155
  });
726
1156
 
727
1157
  it("should not touch an existing tsconfig.json in the same directory", async () => {
@@ -743,22 +1173,82 @@ describe("init", () => {
743
1173
  "utf-8"
744
1174
  );
745
1175
 
746
- mockConfirm(
747
- {
748
- text: "Would you like to use git to manage this Worker?",
749
- result: false,
750
- },
751
- {
752
- text: "Would you like to create a Worker at src/index.ts?",
753
- result: true,
754
- }
755
- );
1176
+ mockConfirm({
1177
+ text: "Would you like to use git to manage this Worker?",
1178
+ result: false,
1179
+ });
1180
+
1181
+ mockSelect({
1182
+ text: "Would you like to create a Worker at src/index.ts?",
1183
+ result: "fetch",
1184
+ });
756
1185
 
757
1186
  await runWrangler("init");
758
1187
  const tsconfigJson = JSON.parse(
759
1188
  fs.readFileSync("./tsconfig.json", "utf-8")
760
1189
  );
761
1190
  expect(tsconfigJson.compilerOptions).toEqual({});
1191
+ expect(std).toMatchInlineSnapshot(`
1192
+ Object {
1193
+ "debug": "",
1194
+ "err": "",
1195
+ "out": "✨ Created wrangler.toml
1196
+ ✨ Created src/index.ts
1197
+
1198
+ To start developing your Worker, run \`npx wrangler dev\`
1199
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1200
+ "warn": "",
1201
+ }
1202
+ `);
1203
+ });
1204
+
1205
+ it("should not touch an existing tsconfig.json in the ancestor of a target directory", async () => {
1206
+ fs.mkdirSync("path/to/worker", { recursive: true });
1207
+ fs.writeFileSync(
1208
+ "path/to/worker/package.json",
1209
+ JSON.stringify({
1210
+ name: "test",
1211
+ version: "1.0.0",
1212
+ devDependencies: {
1213
+ wrangler: "0.0.0",
1214
+ "@cloudflare/workers-types": "0.0.0",
1215
+ },
1216
+ }),
1217
+ "utf-8"
1218
+ );
1219
+ fs.writeFileSync(
1220
+ "path/to/worker/tsconfig.json",
1221
+ JSON.stringify({ compilerOptions: {} }),
1222
+ "utf-8"
1223
+ );
1224
+
1225
+ mockConfirm({
1226
+ text: "Would you like to use git to manage this Worker?",
1227
+ result: false,
1228
+ });
1229
+
1230
+ mockSelect({
1231
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
1232
+ result: "fetch",
1233
+ });
1234
+
1235
+ await runWrangler("init path/to/worker/my-worker");
1236
+ const tsconfigJson = JSON.parse(
1237
+ fs.readFileSync("path/to/worker/tsconfig.json", "utf-8")
1238
+ );
1239
+ expect(tsconfigJson.compilerOptions).toEqual({});
1240
+ expect(std).toMatchInlineSnapshot(`
1241
+ Object {
1242
+ "debug": "",
1243
+ "err": "",
1244
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1245
+ ✨ Created path/to/worker/my-worker/src/index.ts
1246
+
1247
+ To start developing your Worker, run \`npx wrangler dev\`
1248
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1249
+ "warn": "",
1250
+ }
1251
+ `);
762
1252
  });
763
1253
 
764
1254
  it("should offer to install type definitions in an existing typescript project", async () => {
@@ -768,18 +1258,18 @@ describe("init", () => {
768
1258
  result: false,
769
1259
  },
770
1260
  {
771
- text: "Would you like to install wrangler into your package.json?",
1261
+ text: "Would you like to install wrangler into package.json?",
772
1262
  result: false,
773
1263
  },
774
1264
  {
775
1265
  text: "Would you like to install the type definitions for Workers into your package.json?",
776
1266
  result: true,
777
- },
778
- {
779
- text: "Would you like to create a Worker at src/index.ts?",
780
- result: false,
781
1267
  }
782
1268
  );
1269
+ mockSelect({
1270
+ text: "Would you like to create a Worker at src/index.ts?",
1271
+ result: "none",
1272
+ });
783
1273
  fs.writeFileSync(
784
1274
  "./package.json",
785
1275
  JSON.stringify({
@@ -803,6 +1293,16 @@ describe("init", () => {
803
1293
  expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
804
1294
  "@cloudflare/workers-types"
805
1295
  );
1296
+ expect(std).toMatchInlineSnapshot(`
1297
+ Object {
1298
+ "debug": "",
1299
+ "err": "",
1300
+ "out": "✨ Created wrangler.toml
1301
+ ✨ Installed @cloudflare/workers-types into devDependencies
1302
+ 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1303
+ "warn": "",
1304
+ }
1305
+ `);
806
1306
  });
807
1307
 
808
1308
  it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
@@ -824,16 +1324,15 @@ describe("init", () => {
824
1324
  "utf-8"
825
1325
  );
826
1326
 
827
- mockConfirm(
828
- {
829
- text: "Would you like to use git to manage this Worker?",
830
- result: false,
831
- },
832
- {
833
- text: "Would you like to create a Worker at src/index.ts?",
834
- result: true,
835
- }
836
- );
1327
+ mockConfirm({
1328
+ text: "Would you like to use git to manage this Worker?",
1329
+ result: false,
1330
+ });
1331
+
1332
+ mockSelect({
1333
+ text: "Would you like to create a Worker at src/index.ts?",
1334
+ result: "fetch",
1335
+ });
837
1336
 
838
1337
  fs.mkdirSync("./sub-1/sub-2", { recursive: true });
839
1338
  process.chdir("./sub-1/sub-2");
@@ -851,7 +1350,7 @@ describe("init", () => {
851
1350
  Object {
852
1351
  "debug": "",
853
1352
  "err": "",
854
- "out": "✨ Successfully created wrangler.toml
1353
+ "out": "✨ Created wrangler.toml
855
1354
  ✨ Created src/index.ts
856
1355
 
857
1356
  To start developing your Worker, run \`npx wrangler dev\`
@@ -874,18 +1373,18 @@ describe("init", () => {
874
1373
  result: true,
875
1374
  },
876
1375
  {
877
- text: "Would you like to install wrangler into your package.json?",
1376
+ text: "Would you like to install wrangler into package.json?",
878
1377
  result: false,
879
1378
  },
880
1379
  {
881
1380
  text: "Would you like to use TypeScript?",
882
1381
  result: false,
883
- },
884
- {
885
- text: "Would you like to create a Worker at src/index.js?",
886
- result: true,
887
1382
  }
888
1383
  );
1384
+ mockSelect({
1385
+ text: "Would you like to create a Worker at src/index.js?",
1386
+ result: "fetch",
1387
+ });
889
1388
  await runWrangler("init");
890
1389
 
891
1390
  expect(fs.existsSync("./package.json")).toBe(true);
@@ -897,16 +1396,16 @@ describe("init", () => {
897
1396
  expect(fs.existsSync("./src/index.ts")).toBe(false);
898
1397
 
899
1398
  expect(packageJson.scripts.start).toBe("wrangler dev");
900
- expect(packageJson.scripts.publish).toBe("wrangler publish");
1399
+ expect(packageJson.scripts.deploy).toBe("wrangler publish");
901
1400
  expect(packageJson.name).toContain("wrangler-tests");
902
1401
  expect(packageJson.version).toEqual("0.0.0");
903
1402
  expect(std.out).toMatchInlineSnapshot(`
904
- "✨ Successfully created wrangler.toml
1403
+ "✨ Created wrangler.toml
905
1404
  ✨ Created package.json
906
1405
  ✨ Created src/index.js
907
1406
 
908
1407
  To start developing your Worker, run \`npm start\`
909
- To publish your Worker to the Internet, run \`npm run publish\`"
1408
+ To publish your Worker to the Internet, run \`npm run deploy\`"
910
1409
  `);
911
1410
  });
912
1411
 
@@ -917,24 +1416,24 @@ describe("init", () => {
917
1416
  result: false,
918
1417
  },
919
1418
  {
920
- text: "Would you like to install wrangler into your package.json?",
1419
+ text: "Would you like to install wrangler into package.json?",
921
1420
  result: false,
922
1421
  },
923
1422
  {
924
1423
  text: "Would you like to use TypeScript?",
925
1424
  result: false,
926
- },
927
- {
928
- text: "Would you like to create a Worker at src/index.js?",
929
- result: true,
930
1425
  }
931
1426
  );
1427
+ mockSelect({
1428
+ text: "Would you like to create a Worker at src/index.js?",
1429
+ result: "fetch",
1430
+ });
932
1431
  await fsp.writeFile(
933
1432
  "./package.json",
934
1433
  JSON.stringify({
935
1434
  scripts: {
936
1435
  start: "test-start",
937
- publish: "test-publish",
1436
+ deploy: "test-publish",
938
1437
  },
939
1438
  })
940
1439
  );
@@ -947,9 +1446,9 @@ describe("init", () => {
947
1446
  expect(fs.existsSync("./src/index.ts")).toBe(false);
948
1447
 
949
1448
  expect(packageJson.scripts.start).toBe("test-start");
950
- expect(packageJson.scripts.publish).toBe("test-publish");
1449
+ expect(packageJson.scripts.deploy).toBe("test-publish");
951
1450
  expect(std.out).toMatchInlineSnapshot(`
952
- "✨ Successfully created wrangler.toml
1451
+ "✨ Created wrangler.toml
953
1452
  ✨ Created src/index.js
954
1453
 
955
1454
  To start developing your Worker, run \`npx wrangler dev\`
@@ -964,7 +1463,7 @@ describe("init", () => {
964
1463
  result: false,
965
1464
  },
966
1465
  {
967
- text: "Would you like to install wrangler into your package.json?",
1466
+ text: "Would you like to install wrangler into package.json?",
968
1467
  result: false,
969
1468
  },
970
1469
  {
@@ -985,6 +1484,54 @@ describe("init", () => {
985
1484
  await runWrangler("init");
986
1485
  expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
987
1486
  expect(fs.existsSync("./src/index.ts")).toBe(false);
1487
+ expect(std).toMatchInlineSnapshot(`
1488
+ Object {
1489
+ "debug": "",
1490
+ "err": "",
1491
+ "out": "✨ Created wrangler.toml",
1492
+ "warn": "",
1493
+ }
1494
+ `);
1495
+ });
1496
+
1497
+ it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1498
+ mockConfirm(
1499
+ {
1500
+ text: "Would you like to use git to manage this Worker?",
1501
+ result: false,
1502
+ },
1503
+ {
1504
+ text: "Would you like to install wrangler into package.json?",
1505
+ result: false,
1506
+ },
1507
+ {
1508
+ text: "Would you like to use TypeScript?",
1509
+ result: false,
1510
+ }
1511
+ );
1512
+
1513
+ fs.writeFileSync(
1514
+ "./package.json",
1515
+ JSON.stringify({ name: "test", version: "1.0.0" }),
1516
+ "utf-8"
1517
+ );
1518
+ fs.mkdirSync("./my-worker/src", { recursive: true });
1519
+ const PLACEHOLDER = "/* placeholder text */";
1520
+ fs.writeFileSync("./my-worker/src/index.js", PLACEHOLDER, "utf-8");
1521
+
1522
+ await runWrangler("init my-worker");
1523
+ expect(fs.readFileSync("./my-worker/src/index.js", "utf-8")).toBe(
1524
+ PLACEHOLDER
1525
+ );
1526
+ expect(fs.existsSync("./my-worker/src/index.ts")).toBe(false);
1527
+ expect(std).toMatchInlineSnapshot(`
1528
+ Object {
1529
+ "debug": "",
1530
+ "err": "",
1531
+ "out": "✨ Created my-worker/wrangler.toml",
1532
+ "warn": "",
1533
+ }
1534
+ `);
988
1535
  });
989
1536
  });
990
1537
 
@@ -1009,6 +1556,22 @@ describe("init", () => {
1009
1556
  expect(parsed.name).toBe(path.basename(process.cwd()).toLowerCase());
1010
1557
  expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1011
1558
  expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1559
+ expect(std).toMatchInlineSnapshot(`
1560
+ Object {
1561
+ "debug": "",
1562
+ "err": "",
1563
+ "out": "✨ Created wrangler.toml
1564
+ ✨ Initialized git repository
1565
+ ✨ Created package.json
1566
+ ✨ Created tsconfig.json
1567
+ ✨ Created src/index.ts
1568
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1569
+
1570
+ To start developing your Worker, run \`npm start\`
1571
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1572
+ "warn": "",
1573
+ }
1574
+ `);
1012
1575
  });
1013
1576
 
1014
1577
  it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
@@ -1022,6 +1585,22 @@ describe("init", () => {
1022
1585
  expect(parsed.name).toBe("worker");
1023
1586
  expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1024
1587
  expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1588
+ expect(std).toMatchInlineSnapshot(`
1589
+ Object {
1590
+ "debug": "",
1591
+ "err": "",
1592
+ "out": "✨ Created path/to/worker/wrangler.toml
1593
+ ✨ Initialized git repository at path/to/worker
1594
+ ✨ Created path/to/worker/package.json
1595
+ ✨ Created path/to/worker/tsconfig.json
1596
+ ✨ Created path/to/worker/src/index.ts
1597
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1598
+
1599
+ To start developing your Worker, run \`cd path/to/worker && npm start\`
1600
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1601
+ "warn": "",
1602
+ }
1603
+ `);
1025
1604
  });
1026
1605
 
1027
1606
  it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
@@ -1037,6 +1616,22 @@ describe("init", () => {
1037
1616
  expect(parsed.name).toBe("weird_w0rkr_n4m3-js-tsx-tar-gz");
1038
1617
  expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1039
1618
  expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1619
+ expect(std).toMatchInlineSnapshot(`
1620
+ Object {
1621
+ "debug": "",
1622
+ "err": "",
1623
+ "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1624
+ ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1625
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1626
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
1627
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1628
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1629
+
1630
+ To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1631
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1632
+ "warn": "",
1633
+ }
1634
+ `);
1040
1635
  });
1041
1636
  });
1042
1637
  });