wrangler 0.0.0-e2b7352 → 0.0.0-e2f9bb2

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 (86) hide show
  1. package/README.md +24 -13
  2. package/bin/wrangler.js +3 -0
  3. package/miniflare-dist/index.mjs +4828 -21
  4. package/package.json +29 -22
  5. package/pages/functions/buildPlugin.ts +92 -0
  6. package/pages/functions/buildWorker.ts +80 -4
  7. package/pages/functions/filepath-routing.test.ts +11 -0
  8. package/pages/functions/filepath-routing.ts +8 -0
  9. package/pages/functions/routes.ts +6 -1
  10. package/pages/functions/template-plugin.ts +146 -0
  11. package/pages/functions/template-worker.ts +21 -8
  12. package/src/__tests__/config-cache.test.ts +36 -0
  13. package/src/__tests__/configuration.test.ts +386 -81
  14. package/src/__tests__/dev.test.tsx +242 -21
  15. package/src/__tests__/guess-worker-format.test.ts +18 -1
  16. package/src/__tests__/helpers/mock-cfetch.ts +5 -1
  17. package/src/__tests__/helpers/mock-console.ts +20 -3
  18. package/src/__tests__/helpers/mock-dialogs.ts +24 -0
  19. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  20. package/src/__tests__/helpers/mock-istty.ts +27 -0
  21. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  22. package/src/__tests__/helpers/run-in-tmp.ts +7 -0
  23. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  24. package/src/__tests__/https-options.test.ts +5 -2
  25. package/src/__tests__/index.test.ts +25 -759
  26. package/src/__tests__/init.test.ts +1042 -0
  27. package/src/__tests__/jest.setup.ts +4 -4
  28. package/src/__tests__/kv.test.ts +130 -56
  29. package/src/__tests__/logger.test.ts +115 -0
  30. package/src/__tests__/pages.test.ts +290 -43
  31. package/src/__tests__/publish.test.ts +1699 -333
  32. package/src/__tests__/r2.test.ts +24 -8
  33. package/src/__tests__/route.test.ts +4 -4
  34. package/src/__tests__/secret.test.ts +55 -34
  35. package/src/__tests__/tail.test.ts +97 -45
  36. package/src/__tests__/user.test.ts +131 -0
  37. package/src/__tests__/whoami.test.tsx +14 -9
  38. package/src/bundle.ts +15 -1
  39. package/src/cfetch/internal.ts +0 -2
  40. package/src/cli.ts +5 -11
  41. package/src/config/README.md +1 -1
  42. package/src/config/config.ts +20 -4
  43. package/src/config/environment.ts +26 -3
  44. package/src/config/index.ts +2 -1
  45. package/src/config/validation-helpers.ts +26 -5
  46. package/src/config/validation.ts +233 -41
  47. package/src/config-cache.ts +45 -0
  48. package/src/create-worker-preview.ts +22 -13
  49. package/src/create-worker-upload-form.ts +32 -9
  50. package/src/dev/dev-vars.ts +36 -0
  51. package/src/dev/dev.tsx +60 -34
  52. package/src/dev/local.tsx +101 -21
  53. package/src/dev/remote.tsx +23 -13
  54. package/src/dev/use-esbuild.ts +19 -5
  55. package/src/dialogs.tsx +16 -6
  56. package/src/entry.ts +38 -11
  57. package/src/environment-variables.ts +3 -1
  58. package/src/errors.ts +11 -0
  59. package/src/https-options.ts +3 -2
  60. package/src/index.tsx +529 -303
  61. package/src/inspect.ts +17 -11
  62. package/src/logger.ts +66 -0
  63. package/src/miniflare-cli/index.ts +4 -0
  64. package/src/miniflare-cli/request-context.ts +40 -0
  65. package/src/module-collection.ts +7 -7
  66. package/src/open-in-browser.ts +5 -4
  67. package/src/package-manager.ts +9 -8
  68. package/src/pages.tsx +860 -55
  69. package/src/parse.ts +1 -1
  70. package/src/proxy.ts +25 -11
  71. package/src/publish.ts +260 -141
  72. package/src/sites.tsx +27 -14
  73. package/src/tail/index.ts +1 -1
  74. package/src/tail/printing.ts +13 -8
  75. package/src/update-check.ts +19 -0
  76. package/src/user.tsx +104 -38
  77. package/src/whoami.tsx +2 -1
  78. package/src/worker.ts +15 -6
  79. package/templates/gitignore +171 -0
  80. package/templates/static-asset-facade.js +1 -1
  81. package/wrangler-dist/cli.js +20948 -50169
  82. package/miniflare-dist/index.mjs.map +0 -7
  83. package/src/__tests__/logout.test.ts +0 -52
  84. package/src/__tests__/sentry.test.ts +0 -146
  85. package/src/reporting.ts +0 -131
  86. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,1042 @@
1
+ import * as fs from "node:fs";
2
+ import * as fsp from "node:fs/promises";
3
+ import path from "node:path";
4
+ import * as TOML from "@iarna/toml";
5
+ import { execa } from "execa";
6
+ import { parseConfigFileTextToJson } from "typescript";
7
+ import { version as wranglerVersion } from "../../package.json";
8
+ import { getPackageManager } from "../package-manager";
9
+ import { mockConsoleMethods } from "./helpers/mock-console";
10
+ import { mockConfirm, clearConfirmMocks } from "./helpers/mock-dialogs";
11
+ import { runInTempDir } from "./helpers/run-in-tmp";
12
+ import { runWrangler } from "./helpers/run-wrangler";
13
+ import type { PackageManager } from "../package-manager";
14
+
15
+ describe("init", () => {
16
+ let mockPackageManager: PackageManager;
17
+ runInTempDir();
18
+
19
+ beforeEach(() => {
20
+ mockPackageManager = {
21
+ cwd: process.cwd(),
22
+ // @ts-expect-error we're making a fake package manager here
23
+ type: "mockpm",
24
+ addDevDeps: jest.fn(),
25
+ install: jest.fn(),
26
+ };
27
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
28
+ });
29
+
30
+ afterEach(() => {
31
+ clearConfirmMocks();
32
+ });
33
+
34
+ const std = mockConsoleMethods();
35
+
36
+ describe("options", () => {
37
+ it("should initialize with no interactive prompts if `--yes` is used", async () => {
38
+ await runWrangler("init --yes");
39
+
40
+ expect(fs.existsSync("./src/index.js")).toBe(false);
41
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
42
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
43
+ expect(fs.existsSync("./package.json")).toBe(true);
44
+ expect(fs.existsSync("./wrangler.toml")).toBe(true);
45
+ expect(std.out).toMatchInlineSnapshot(`
46
+ "✨ Successfully created wrangler.toml
47
+ ✨ Initialized git repository
48
+ ✨ Created package.json
49
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
50
+ ✨ Created src/index.ts
51
+
52
+ To start developing your Worker, run \`npm start\`
53
+ To publish your Worker to the Internet, run \`npm run publish\`"
54
+ `);
55
+ expect(std.err).toMatchInlineSnapshot(`""`);
56
+ expect(std.warn).toMatchInlineSnapshot(`""`);
57
+ });
58
+
59
+ it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
60
+ await runWrangler("init my-worker --yes");
61
+
62
+ expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
63
+ expect(fs.existsSync("./my-worker/src/index.ts")).toBe(true);
64
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(true);
65
+ expect(fs.existsSync("./my-worker/package.json")).toBe(true);
66
+ expect(fs.existsSync("./my-worker/wrangler.toml")).toBe(true);
67
+ const parsedWranglerToml = TOML.parse(
68
+ fs.readFileSync("./my-worker/wrangler.toml", "utf-8")
69
+ );
70
+ expect(parsedWranglerToml.main).toEqual("src/index.ts");
71
+ 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
77
+
78
+ To start developing your Worker, run \`cd my-worker && npm start\`
79
+ To publish your Worker to the Internet, run \`npm run publish\`"
80
+ `);
81
+ expect(std.err).toMatchInlineSnapshot(`""`);
82
+ expect(std.warn).toMatchInlineSnapshot(`""`);
83
+ });
84
+
85
+ it("should initialize with no interactive prompts if `-y` is used", async () => {
86
+ await runWrangler("init -y");
87
+
88
+ expect(fs.existsSync("./src/index.js")).toBe(false);
89
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
90
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
91
+ expect(fs.existsSync("./package.json")).toBe(true);
92
+ expect(fs.existsSync("./wrangler.toml")).toBe(true);
93
+ expect(std).toMatchInlineSnapshot(`
94
+ Object {
95
+ "debug": "",
96
+ "err": "",
97
+ "out": "✨ Successfully created wrangler.toml
98
+ ✨ Initialized git repository
99
+ ✨ Created package.json
100
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
101
+ ✨ Created src/index.ts
102
+
103
+ To start developing your Worker, run \`npm start\`
104
+ To publish your Worker to the Internet, run \`npm run publish\`",
105
+ "warn": "",
106
+ }
107
+ `);
108
+ });
109
+
110
+ it("should error if `--type javascript` is used", async () => {
111
+ await expect(
112
+ runWrangler("init --type javascript")
113
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
114
+ `"The --type option is no longer supported."`
115
+ );
116
+ });
117
+
118
+ it("should error if `--type rust` is used", async () => {
119
+ await expect(
120
+ runWrangler("init --type rust")
121
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
122
+ `"The --type option is no longer supported."`
123
+ );
124
+ });
125
+
126
+ it("should error if `--type webpack` is used", async () => {
127
+ await expect(runWrangler("init --type webpack")).rejects
128
+ .toThrowErrorMatchingInlineSnapshot(`
129
+ "The --type option is no longer supported.
130
+ If you wish to use webpack then you will need to create a custom build."
131
+ `);
132
+ });
133
+
134
+ it("should error if `--site` is used", async () => {
135
+ await expect(runWrangler("init --site")).rejects
136
+ .toThrowErrorMatchingInlineSnapshot(`
137
+ "The --site option is no longer supported.
138
+ If you wish to create a brand new Worker Sites project then clone the \`worker-sites-template\` starter repository:
139
+
140
+ \`\`\`
141
+ git clone --depth=1 --branch=wrangler2 https://github.com/cloudflare/worker-sites-template my-site
142
+ cd my-site
143
+ \`\`\`
144
+
145
+ Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.
146
+ Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/."
147
+ `);
148
+ });
149
+ });
150
+
151
+ describe("wrangler.toml", () => {
152
+ it("should create a wrangler.toml", async () => {
153
+ mockConfirm(
154
+ {
155
+ text: "Would you like to use git to manage this Worker?",
156
+ result: false,
157
+ },
158
+ {
159
+ text: "No package.json found. Would you like to create one?",
160
+ result: false,
161
+ }
162
+ );
163
+ await runWrangler("init");
164
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
165
+ expect(typeof parsed.compatibility_date).toBe("string");
166
+ expect(parsed.name).toContain("wrangler-tests");
167
+ expect(fs.existsSync("./package.json")).toBe(false);
168
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
169
+ });
170
+
171
+ it("should create a wrangler.toml and a directory for a named Worker ", async () => {
172
+ mockConfirm(
173
+ {
174
+ text: "Would you like to use git to manage this Worker?",
175
+ result: false,
176
+ },
177
+ {
178
+ text: "No package.json found. Would you like to create one?",
179
+ result: false,
180
+ }
181
+ );
182
+ await runWrangler("init my-worker");
183
+ const parsed = TOML.parse(
184
+ await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
185
+ );
186
+ expect(typeof parsed.compatibility_date).toBe("string");
187
+ expect(parsed.name).toBe("my-worker");
188
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
189
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
190
+ });
191
+
192
+ it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
193
+ fs.writeFileSync(
194
+ "./wrangler.toml",
195
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
196
+ "utf-8"
197
+ );
198
+ mockConfirm({
199
+ text: "Do you want to continue initializing this project?",
200
+ result: false,
201
+ });
202
+ await runWrangler("init");
203
+ expect(std.warn).toContain("wrangler.toml file already exists!");
204
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
205
+ expect(parsed.compatibility_date).toBe("something-else");
206
+ });
207
+
208
+ it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
209
+ fs.writeFileSync(
210
+ "./wrangler.toml",
211
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
212
+ "utf-8"
213
+ );
214
+ mockConfirm(
215
+ {
216
+ text: "Would you like to use git to manage this Worker?",
217
+ result: true,
218
+ },
219
+ {
220
+ text: "Do you want to continue initializing this project?",
221
+ result: true,
222
+ },
223
+ {
224
+ text: "No package.json found. Would you like to create one?",
225
+ result: true,
226
+ },
227
+ {
228
+ text: "Would you like to use TypeScript?",
229
+ result: true,
230
+ },
231
+ {
232
+ text: "Would you like to create a Worker at src/index.ts?",
233
+ result: true,
234
+ }
235
+ );
236
+
237
+ await runWrangler("init");
238
+ expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
239
+ `"compatibility_date=\\"something-else\\""`
240
+ );
241
+ });
242
+
243
+ it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
244
+ fs.writeFileSync(
245
+ "./wrangler.toml",
246
+ `compatibility_date="something-else"`,
247
+ "utf-8"
248
+ );
249
+ mockConfirm(
250
+ {
251
+ text: "Do you want to continue initializing this project?",
252
+ result: true,
253
+ },
254
+ {
255
+ text: "Would you like to use git to manage this Worker?",
256
+ result: false,
257
+ },
258
+ {
259
+ text: "No package.json found. Would you like to create one?",
260
+ result: false,
261
+ }
262
+ );
263
+ await runWrangler("init");
264
+ expect(std.warn).toContain("wrangler.toml file already exists!");
265
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
266
+ expect(parsed.compatibility_date).toBe("something-else");
267
+ });
268
+ });
269
+
270
+ describe("git init", () => {
271
+ it("should offer to initialize a git repository", async () => {
272
+ mockConfirm(
273
+ {
274
+ text: "Would you like to use git to manage this Worker?",
275
+ result: true,
276
+ },
277
+ {
278
+ text: "No package.json found. Would you like to create one?",
279
+ result: false,
280
+ }
281
+ );
282
+ await runWrangler("init");
283
+ expect(std).toMatchInlineSnapshot(`
284
+ Object {
285
+ "debug": "",
286
+ "err": "",
287
+ "out": "✨ Successfully created wrangler.toml
288
+ ✨ Initialized git repository",
289
+ "warn": "",
290
+ }
291
+ `);
292
+ expect(fs.lstatSync(".git").isDirectory()).toBe(true);
293
+ expect(fs.lstatSync(".gitignore").isFile()).toBe(true);
294
+ });
295
+
296
+ it("should not offer to initialize a git repo if it's already inside one", async () => {
297
+ await execa("git", ["init"]);
298
+ fs.mkdirSync("some-folder");
299
+ process.chdir("some-folder");
300
+ await runWrangler("init -y");
301
+
302
+ // Note the lack of "✨ Initialized git repository" in the log
303
+ expect(std).toMatchInlineSnapshot(`
304
+ Object {
305
+ "debug": "",
306
+ "err": "",
307
+ "out": "✨ Successfully created wrangler.toml
308
+ ✨ Initialized git repository
309
+ ✨ Created package.json
310
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
311
+ ✨ Created src/index.ts
312
+
313
+ To start developing your Worker, run \`npm start\`
314
+ To publish your Worker to the Internet, run \`npm run publish\`",
315
+ "warn": "",
316
+ }
317
+ `);
318
+ });
319
+
320
+ it.todo(
321
+ "should not offer to initialize a git repo if git is not installed"
322
+ );
323
+ // I... don't know how to test this lol
324
+ });
325
+
326
+ describe("package.json", () => {
327
+ it("should create a package.json if none is found and user confirms", async () => {
328
+ mockConfirm(
329
+ {
330
+ text: "Would you like to use git to manage this Worker?",
331
+ result: false,
332
+ },
333
+ {
334
+ text: "No package.json found. Would you like to create one?",
335
+ result: true,
336
+ },
337
+ {
338
+ text: "Would you like to use TypeScript?",
339
+ result: false,
340
+ },
341
+ {
342
+ text: "Would you like to create a Worker at src/index.js?",
343
+ result: false,
344
+ }
345
+ );
346
+ await runWrangler("init");
347
+ expect(fs.existsSync("./package.json")).toBe(true);
348
+ const packageJson = JSON.parse(
349
+ fs.readFileSync("./package.json", "utf-8")
350
+ );
351
+ expect(packageJson.name).toContain("wrangler-tests");
352
+ expect(packageJson.version).toEqual("0.0.0");
353
+ expect(packageJson.devDependencies).toEqual({
354
+ wrangler: expect.any(String),
355
+ });
356
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
357
+ expect(mockPackageManager.install).toHaveBeenCalled();
358
+ });
359
+
360
+ it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
361
+ mockConfirm(
362
+ {
363
+ text: "Would you like to use git to manage this Worker?",
364
+ result: false,
365
+ },
366
+ {
367
+ text: "No package.json found. Would you like to create one?",
368
+ result: true,
369
+ },
370
+ {
371
+ text: "Would you like to use TypeScript?",
372
+ result: false,
373
+ },
374
+ {
375
+ text: "Would you like to create a Worker at src/index.js?",
376
+ result: false,
377
+ }
378
+ );
379
+ await runWrangler("init my-worker");
380
+ const packageJson = JSON.parse(
381
+ fs.readFileSync("./my-worker/package.json", "utf-8")
382
+ );
383
+ expect(packageJson.name).toBe("my-worker");
384
+ });
385
+
386
+ it("should not touch an existing package.json in the same directory", async () => {
387
+ mockConfirm(
388
+ {
389
+ text: "Would you like to use git to manage this Worker?",
390
+ result: false,
391
+ },
392
+ {
393
+ text: "Would you like to install wrangler into your package.json?",
394
+ result: false,
395
+ },
396
+ {
397
+ text: "Would you like to use TypeScript?",
398
+ result: false,
399
+ },
400
+ {
401
+ text: "Would you like to create a Worker at src/index.js?",
402
+ result: false,
403
+ }
404
+ );
405
+
406
+ fs.writeFileSync(
407
+ "./package.json",
408
+ JSON.stringify({ name: "test", version: "1.0.0" }),
409
+ "utf-8"
410
+ );
411
+
412
+ await runWrangler("init");
413
+ const packageJson = JSON.parse(
414
+ fs.readFileSync("./package.json", "utf-8")
415
+ );
416
+ expect(packageJson.name).toEqual("test");
417
+ expect(packageJson.version).toEqual("1.0.0");
418
+ });
419
+
420
+ it("should offer to install wrangler into an existing package.json", async () => {
421
+ mockConfirm(
422
+ {
423
+ text: "Would you like to use git to manage this Worker?",
424
+ result: false,
425
+ },
426
+ {
427
+ text: "Would you like to install wrangler into your package.json?",
428
+ result: true,
429
+ },
430
+ {
431
+ text: "Would you like to use TypeScript?",
432
+ result: false,
433
+ },
434
+ {
435
+ text: "Would you like to create a Worker at src/index.js?",
436
+ result: false,
437
+ }
438
+ );
439
+
440
+ fs.writeFileSync(
441
+ "./package.json",
442
+ JSON.stringify({ name: "test", version: "1.0.0" }),
443
+ "utf-8"
444
+ );
445
+
446
+ await runWrangler("init");
447
+ const packageJson = JSON.parse(
448
+ fs.readFileSync("./package.json", "utf-8")
449
+ );
450
+ expect(packageJson.name).toEqual("test");
451
+ expect(packageJson.version).toEqual("1.0.0");
452
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
453
+ `wrangler@${wranglerVersion}`
454
+ );
455
+ });
456
+
457
+ it("should not touch an existing package.json in an ancestor directory", async () => {
458
+ mockConfirm(
459
+ {
460
+ text: "Would you like to use git to manage this Worker?",
461
+ result: false,
462
+ },
463
+ {
464
+ text: "Would you like to install wrangler into your package.json?",
465
+ result: false,
466
+ },
467
+ {
468
+ text: "Would you like to use TypeScript?",
469
+ result: false,
470
+ },
471
+ {
472
+ text: "Would you like to create a Worker at src/index.js?",
473
+ result: false,
474
+ }
475
+ );
476
+
477
+ fs.writeFileSync(
478
+ "./package.json",
479
+ JSON.stringify({ name: "test", version: "1.0.0" }),
480
+ "utf-8"
481
+ );
482
+
483
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
484
+ process.chdir("./sub-1/sub-2");
485
+
486
+ await runWrangler("init");
487
+ expect(fs.existsSync("./package.json")).toBe(false);
488
+ expect(fs.existsSync("../../package.json")).toBe(true);
489
+
490
+ const packageJson = JSON.parse(
491
+ fs.readFileSync("../../package.json", "utf-8")
492
+ );
493
+ expect(packageJson).toMatchInlineSnapshot(`
494
+ Object {
495
+ "name": "test",
496
+ "version": "1.0.0",
497
+ }
498
+ `);
499
+ });
500
+ });
501
+
502
+ describe("typescript", () => {
503
+ it("should offer to create a worker in a non-typescript project", async () => {
504
+ mockConfirm(
505
+ {
506
+ text: "Would you like to use git to manage this Worker?",
507
+ result: false,
508
+ },
509
+ {
510
+ text: "Would you like to install wrangler into your package.json?",
511
+ result: false,
512
+ },
513
+ {
514
+ text: "Would you like to use TypeScript?",
515
+ result: false,
516
+ },
517
+ {
518
+ text: "Would you like to create a Worker at src/index.js?",
519
+ result: true,
520
+ }
521
+ );
522
+
523
+ fs.writeFileSync(
524
+ "./package.json",
525
+ JSON.stringify({ name: "test", version: "1.0.0" }),
526
+ "utf-8"
527
+ );
528
+
529
+ await runWrangler("init");
530
+ expect(fs.existsSync("./src/index.js")).toBe(true);
531
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
532
+ });
533
+
534
+ it("should offer to create a worker in a typescript project", async () => {
535
+ mockConfirm(
536
+ {
537
+ text: "Would you like to use git to manage this Worker?",
538
+ result: false,
539
+ },
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: true,
547
+ },
548
+ {
549
+ text: "Would you like to create a Worker at src/index.ts?",
550
+ result: true,
551
+ }
552
+ );
553
+
554
+ fs.writeFileSync(
555
+ "./package.json",
556
+ JSON.stringify({ name: "test", version: "1.0.0" }),
557
+ "utf-8"
558
+ );
559
+
560
+ await runWrangler("init");
561
+ expect(fs.existsSync("./src/index.js")).toBe(false);
562
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
563
+ });
564
+
565
+ it("should add scripts for a typescript project with .ts extension", async () => {
566
+ mockConfirm(
567
+ {
568
+ text: "Would you like to use git to manage this Worker?",
569
+ result: false,
570
+ },
571
+ {
572
+ text: "No package.json found. Would you like to create one?",
573
+ result: true,
574
+ },
575
+ {
576
+ text: "Would you like to install wrangler into your package.json?",
577
+ result: false,
578
+ },
579
+ {
580
+ text: "Would you like to use TypeScript?",
581
+ result: true,
582
+ },
583
+ {
584
+ text: "Would you like to create a Worker at src/index.ts?",
585
+ result: true,
586
+ }
587
+ );
588
+ await runWrangler("init");
589
+
590
+ expect(fs.existsSync("./package.json")).toBe(true);
591
+ const packageJson = JSON.parse(
592
+ fs.readFileSync("./package.json", "utf-8")
593
+ );
594
+
595
+ expect(fs.existsSync("./src/index.js")).toBe(false);
596
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
597
+
598
+ expect(packageJson.scripts.start).toBe("wrangler dev");
599
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
600
+ expect(packageJson.name).toContain("wrangler-tests");
601
+ expect(packageJson.version).toEqual("0.0.0");
602
+ expect(std.out).toMatchInlineSnapshot(`
603
+ "✨ Successfully created wrangler.toml
604
+ ✨ Created package.json
605
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
606
+ ✨ Created src/index.ts
607
+
608
+ To start developing your Worker, run \`npm start\`
609
+ To publish your Worker to the Internet, run \`npm run publish\`"
610
+ `);
611
+ });
612
+
613
+ it("should not overwrite package.json scripts for a typescript project", async () => {
614
+ mockConfirm(
615
+ {
616
+ text: "Would you like to use git to manage this Worker?",
617
+ result: false,
618
+ },
619
+ {
620
+ text: "Would you like to install wrangler into your package.json?",
621
+ result: false,
622
+ },
623
+ {
624
+ text: "Would you like to use TypeScript?",
625
+ result: true,
626
+ },
627
+ {
628
+ text: "Would you like to create a Worker at src/index.ts?",
629
+ result: true,
630
+ }
631
+ );
632
+ await fsp.writeFile(
633
+ "./package.json",
634
+ JSON.stringify({
635
+ scripts: {
636
+ start: "test-start",
637
+ publish: "test-publish",
638
+ },
639
+ })
640
+ );
641
+ const packageJson = JSON.parse(
642
+ fs.readFileSync("./package.json", "utf-8")
643
+ );
644
+ await runWrangler("init");
645
+
646
+ expect(fs.existsSync("./src/index.js")).toBe(false);
647
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
648
+
649
+ expect(packageJson.scripts.start).toBe("test-start");
650
+ expect(packageJson.scripts.publish).toBe("test-publish");
651
+ expect(std.out).toMatchInlineSnapshot(`
652
+ "✨ Successfully created wrangler.toml
653
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
654
+ ✨ Created src/index.ts
655
+
656
+ To start developing your Worker, run \`npx wrangler dev\`
657
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
658
+ `);
659
+ });
660
+
661
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
662
+ mockConfirm(
663
+ {
664
+ text: "Would you like to use git to manage this Worker?",
665
+ result: false,
666
+ },
667
+ {
668
+ text: "Would you like to install wrangler into your package.json?",
669
+ result: false,
670
+ },
671
+ {
672
+ text: "Would you like to use TypeScript?",
673
+ result: true,
674
+ }
675
+ );
676
+
677
+ fs.writeFileSync(
678
+ "./package.json",
679
+ JSON.stringify({ name: "test", version: "1.0.0" }),
680
+ "utf-8"
681
+ );
682
+ fs.mkdirSync("./src", { recursive: true });
683
+ const PLACEHOLDER = "/* placeholder text */";
684
+ fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
685
+
686
+ await runWrangler("init");
687
+ expect(fs.existsSync("./src/index.js")).toBe(false);
688
+ expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
689
+ });
690
+
691
+ it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
692
+ mockConfirm(
693
+ {
694
+ text: "Would you like to use git to manage this Worker?",
695
+ result: false,
696
+ },
697
+ {
698
+ text: "No package.json found. Would you like to create one?",
699
+ result: true,
700
+ },
701
+ {
702
+ text: "Would you like to use TypeScript?",
703
+ result: true,
704
+ },
705
+ {
706
+ text: "Would you like to create a Worker at src/index.ts?",
707
+ result: false,
708
+ }
709
+ );
710
+ await runWrangler("init");
711
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
712
+ const { config: tsconfigJson, error: tsConfigParseError } =
713
+ parseConfigFileTextToJson(
714
+ "./tsconfig.json",
715
+ fs.readFileSync("./tsconfig.json", "utf-8")
716
+ );
717
+ expect(tsConfigParseError).toBeUndefined();
718
+ expect(tsconfigJson.compilerOptions.types).toEqual([
719
+ "@cloudflare/workers-types",
720
+ ]);
721
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
722
+ "@cloudflare/workers-types",
723
+ "typescript"
724
+ );
725
+ });
726
+
727
+ it("should not touch an existing tsconfig.json in the same directory", async () => {
728
+ fs.writeFileSync(
729
+ "./package.json",
730
+ JSON.stringify({
731
+ name: "test",
732
+ version: "1.0.0",
733
+ devDependencies: {
734
+ wrangler: "0.0.0",
735
+ "@cloudflare/workers-types": "0.0.0",
736
+ },
737
+ }),
738
+ "utf-8"
739
+ );
740
+ fs.writeFileSync(
741
+ "./tsconfig.json",
742
+ JSON.stringify({ compilerOptions: {} }),
743
+ "utf-8"
744
+ );
745
+
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
+ );
756
+
757
+ await runWrangler("init");
758
+ const tsconfigJson = JSON.parse(
759
+ fs.readFileSync("./tsconfig.json", "utf-8")
760
+ );
761
+ expect(tsconfigJson.compilerOptions).toEqual({});
762
+ });
763
+
764
+ it("should offer to install type definitions in an existing typescript project", async () => {
765
+ mockConfirm(
766
+ {
767
+ text: "Would you like to use git to manage this Worker?",
768
+ result: false,
769
+ },
770
+ {
771
+ text: "Would you like to install wrangler into your package.json?",
772
+ result: false,
773
+ },
774
+ {
775
+ text: "Would you like to install the type definitions for Workers into your package.json?",
776
+ result: true,
777
+ },
778
+ {
779
+ text: "Would you like to create a Worker at src/index.ts?",
780
+ result: false,
781
+ }
782
+ );
783
+ fs.writeFileSync(
784
+ "./package.json",
785
+ JSON.stringify({
786
+ name: "test",
787
+ version: "1.0.0",
788
+ }),
789
+ "utf-8"
790
+ );
791
+ fs.writeFileSync(
792
+ "./tsconfig.json",
793
+ JSON.stringify({ compilerOptions: {} }),
794
+ "utf-8"
795
+ );
796
+
797
+ await runWrangler("init");
798
+ const tsconfigJson = JSON.parse(
799
+ fs.readFileSync("./tsconfig.json", "utf-8")
800
+ );
801
+ // unchanged tsconfig
802
+ expect(tsconfigJson.compilerOptions).toEqual({});
803
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
804
+ "@cloudflare/workers-types"
805
+ );
806
+ });
807
+
808
+ it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
809
+ fs.writeFileSync(
810
+ "./package.json",
811
+ JSON.stringify({
812
+ name: "test",
813
+ version: "1.0.0",
814
+ devDependencies: {
815
+ wrangler: "0.0.0",
816
+ "@cloudflare/workers-types": "0.0.0",
817
+ },
818
+ }),
819
+ "utf-8"
820
+ );
821
+ fs.writeFileSync(
822
+ "./tsconfig.json",
823
+ JSON.stringify({ compilerOptions: {} }),
824
+ "utf-8"
825
+ );
826
+
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
+ );
837
+
838
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
839
+ process.chdir("./sub-1/sub-2");
840
+
841
+ await runWrangler("init");
842
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
843
+ expect(fs.existsSync("../../tsconfig.json")).toBe(true);
844
+
845
+ const tsconfigJson = JSON.parse(
846
+ fs.readFileSync("../../tsconfig.json", "utf-8")
847
+ );
848
+ expect(tsconfigJson.compilerOptions).toEqual({});
849
+
850
+ expect(std).toMatchInlineSnapshot(`
851
+ Object {
852
+ "debug": "",
853
+ "err": "",
854
+ "out": "✨ Successfully created wrangler.toml
855
+ ✨ Created src/index.ts
856
+
857
+ To start developing your Worker, run \`npx wrangler dev\`
858
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
859
+ "warn": "",
860
+ }
861
+ `);
862
+ });
863
+ });
864
+
865
+ describe("javascript", () => {
866
+ it("should add missing scripts for a non-ts project with .js extension", async () => {
867
+ mockConfirm(
868
+ {
869
+ text: "Would you like to use git to manage this Worker?",
870
+ result: false,
871
+ },
872
+ {
873
+ text: "No package.json found. Would you like to create one?",
874
+ result: true,
875
+ },
876
+ {
877
+ text: "Would you like to install wrangler into your package.json?",
878
+ result: false,
879
+ },
880
+ {
881
+ text: "Would you like to use TypeScript?",
882
+ result: false,
883
+ },
884
+ {
885
+ text: "Would you like to create a Worker at src/index.js?",
886
+ result: true,
887
+ }
888
+ );
889
+ await runWrangler("init");
890
+
891
+ expect(fs.existsSync("./package.json")).toBe(true);
892
+ const packageJson = JSON.parse(
893
+ fs.readFileSync("./package.json", "utf-8")
894
+ );
895
+
896
+ expect(fs.existsSync("./src/index.js")).toBe(true);
897
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
898
+
899
+ expect(packageJson.scripts.start).toBe("wrangler dev");
900
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
901
+ expect(packageJson.name).toContain("wrangler-tests");
902
+ expect(packageJson.version).toEqual("0.0.0");
903
+ expect(std.out).toMatchInlineSnapshot(`
904
+ "✨ Successfully created wrangler.toml
905
+ ✨ Created package.json
906
+ ✨ Created src/index.js
907
+
908
+ To start developing your Worker, run \`npm start\`
909
+ To publish your Worker to the Internet, run \`npm run publish\`"
910
+ `);
911
+ });
912
+
913
+ it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
914
+ mockConfirm(
915
+ {
916
+ text: "Would you like to use git to manage this Worker?",
917
+ result: false,
918
+ },
919
+ {
920
+ text: "Would you like to install wrangler into your package.json?",
921
+ result: false,
922
+ },
923
+ {
924
+ text: "Would you like to use TypeScript?",
925
+ result: false,
926
+ },
927
+ {
928
+ text: "Would you like to create a Worker at src/index.js?",
929
+ result: true,
930
+ }
931
+ );
932
+ await fsp.writeFile(
933
+ "./package.json",
934
+ JSON.stringify({
935
+ scripts: {
936
+ start: "test-start",
937
+ publish: "test-publish",
938
+ },
939
+ })
940
+ );
941
+ const packageJson = JSON.parse(
942
+ fs.readFileSync("./package.json", "utf-8")
943
+ );
944
+ await runWrangler("init");
945
+
946
+ expect(fs.existsSync("./src/index.js")).toBe(true);
947
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
948
+
949
+ expect(packageJson.scripts.start).toBe("test-start");
950
+ expect(packageJson.scripts.publish).toBe("test-publish");
951
+ expect(std.out).toMatchInlineSnapshot(`
952
+ "✨ Successfully created wrangler.toml
953
+ ✨ Created src/index.js
954
+
955
+ To start developing your Worker, run \`npx wrangler dev\`
956
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
957
+ `);
958
+ });
959
+
960
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
961
+ mockConfirm(
962
+ {
963
+ text: "Would you like to use git to manage this Worker?",
964
+ result: false,
965
+ },
966
+ {
967
+ text: "Would you like to install wrangler into your package.json?",
968
+ result: false,
969
+ },
970
+ {
971
+ text: "Would you like to use TypeScript?",
972
+ result: false,
973
+ }
974
+ );
975
+
976
+ fs.writeFileSync(
977
+ "./package.json",
978
+ JSON.stringify({ name: "test", version: "1.0.0" }),
979
+ "utf-8"
980
+ );
981
+ fs.mkdirSync("./src", { recursive: true });
982
+ const PLACEHOLDER = "/* placeholder text */";
983
+ fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
984
+
985
+ await runWrangler("init");
986
+ expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
987
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
988
+ });
989
+ });
990
+
991
+ describe("worker names", () => {
992
+ it("should create a worker with a given name", async () => {
993
+ await runWrangler("init my-worker -y");
994
+
995
+ const parsed = TOML.parse(
996
+ await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
997
+ );
998
+
999
+ expect(typeof parsed.compatibility_date).toBe("string");
1000
+ expect(parsed.name).toBe("my-worker");
1001
+ });
1002
+
1003
+ it('should create a worker with the name of the current directory if "name" is .', async () => {
1004
+ await runWrangler("init . -y");
1005
+
1006
+ const parsed = TOML.parse(await fsp.readFile("wrangler.toml", "utf-8"));
1007
+
1008
+ expect(typeof parsed.compatibility_date).toBe("string");
1009
+ expect(parsed.name).toBe(path.basename(process.cwd()).toLowerCase());
1010
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1011
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1012
+ });
1013
+
1014
+ it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
1015
+ await runWrangler("init path/to/worker -y");
1016
+
1017
+ const parsed = TOML.parse(
1018
+ await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
1019
+ );
1020
+
1021
+ expect(typeof parsed.compatibility_date).toBe("string");
1022
+ expect(parsed.name).toBe("worker");
1023
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1024
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1025
+ });
1026
+
1027
+ it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
1028
+ await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
1029
+ const parsed = TOML.parse(
1030
+ await fsp.readFile(
1031
+ "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml",
1032
+ "utf-8"
1033
+ )
1034
+ );
1035
+
1036
+ expect(typeof parsed.compatibility_date).toBe("string");
1037
+ expect(parsed.name).toBe("weird_w0rkr_n4m3-js-tsx-tar-gz");
1038
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1039
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1040
+ });
1041
+ });
1042
+ });