wrangler 0.0.0-e88a54e → 0.0.0-e95e5a0

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 (88) hide show
  1. package/README.md +25 -14
  2. package/bin/wrangler.js +13 -1
  3. package/miniflare-dist/index.mjs +4828 -21
  4. package/package.json +29 -24
  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 +530 -201
  14. package/src/__tests__/dev.test.tsx +284 -18
  15. package/src/__tests__/guess-worker-format.test.ts +28 -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 +26 -1
  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 +27 -769
  26. package/src/__tests__/init.test.ts +1516 -0
  27. package/src/__tests__/jest.setup.ts +4 -4
  28. package/src/__tests__/kv.test.ts +133 -61
  29. package/src/__tests__/logger.test.ts +115 -0
  30. package/src/__tests__/pages.test.ts +311 -26
  31. package/src/__tests__/publish.test.ts +2789 -618
  32. package/src/__tests__/r2.test.ts +26 -10
  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 +146 -20
  36. package/src/__tests__/user.test.ts +131 -0
  37. package/src/__tests__/whoami.test.tsx +14 -9
  38. package/src/bundle.ts +30 -3
  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 +32 -4
  44. package/src/config/index.ts +4 -3
  45. package/src/config/validation-helpers.ts +96 -7
  46. package/src/config/validation.ts +388 -54
  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 +132 -78
  52. package/src/dev/local.tsx +101 -21
  53. package/src/dev/remote.tsx +23 -13
  54. package/src/dev/use-esbuild.ts +23 -5
  55. package/src/dialogs.tsx +16 -6
  56. package/src/entry.ts +83 -24
  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 +689 -394
  61. package/src/inspect.ts +17 -11
  62. package/src/intl-polyfill.d.ts +139 -0
  63. package/src/logger.ts +66 -0
  64. package/src/miniflare-cli/index.ts +4 -0
  65. package/src/miniflare-cli/request-context.ts +40 -0
  66. package/src/module-collection.ts +7 -7
  67. package/src/open-in-browser.ts +5 -4
  68. package/src/package-manager.ts +9 -8
  69. package/src/pages.tsx +875 -56
  70. package/src/parse.ts +1 -1
  71. package/src/proxy.ts +28 -19
  72. package/src/publish.ts +263 -141
  73. package/src/sites.tsx +27 -14
  74. package/src/tail/index.ts +2 -2
  75. package/src/tail/printing.ts +13 -8
  76. package/src/update-check.ts +19 -0
  77. package/src/user.tsx +116 -41
  78. package/src/whoami.tsx +2 -1
  79. package/src/worker.ts +15 -6
  80. package/templates/gitignore +171 -0
  81. package/templates/static-asset-facade.js +1 -1
  82. package/wrangler-dist/cli.js +21162 -50227
  83. package/miniflare-dist/index.mjs.map +0 -7
  84. package/src/__tests__/dev2.test.tsx +0 -154
  85. package/src/__tests__/logout.test.ts +0 -52
  86. package/src/__tests__/sentry.test.ts +0 -146
  87. package/src/reporting.ts +0 -131
  88. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,1516 @@
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
+ "✨ 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
+ "✨ Created my-worker/wrangler.toml
73
+ ✨ Initialized git repository at my-worker
74
+ ✨ Created my-worker/package.json
75
+ ✨ Created my-worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies
76
+ ✨ Created my-worker/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": "✨ 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
+ expect(std).toMatchInlineSnapshot(`
170
+ Object {
171
+ "debug": "",
172
+ "err": "",
173
+ "out": "✨ Created wrangler.toml",
174
+ "warn": "",
175
+ }
176
+ `);
177
+ });
178
+
179
+ it("should create a wrangler.toml and a directory for a named Worker ", async () => {
180
+ mockConfirm(
181
+ {
182
+ text: "Would you like to use git to manage this Worker?",
183
+ result: false,
184
+ },
185
+ {
186
+ text: "No package.json found. Would you like to create one?",
187
+ result: false,
188
+ }
189
+ );
190
+ await runWrangler("init my-worker");
191
+ const parsed = TOML.parse(
192
+ await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
193
+ );
194
+ expect(typeof parsed.compatibility_date).toBe("string");
195
+ expect(parsed.name).toBe("my-worker");
196
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
197
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
198
+
199
+ expect(std).toMatchInlineSnapshot(`
200
+ Object {
201
+ "debug": "",
202
+ "err": "",
203
+ "out": "✨ Created my-worker/wrangler.toml",
204
+ "warn": "",
205
+ }
206
+ `);
207
+ });
208
+
209
+ it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
210
+ fs.writeFileSync(
211
+ "./wrangler.toml",
212
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
213
+ "utf-8"
214
+ );
215
+ mockConfirm({
216
+ text: "Do you want to continue initializing this project?",
217
+ result: false,
218
+ });
219
+ await runWrangler("init");
220
+ expect(std.warn).toContain("wrangler.toml already exists!");
221
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
222
+ expect(parsed.compatibility_date).toBe("something-else");
223
+
224
+ expect(std).toMatchInlineSnapshot(`
225
+ Object {
226
+ "debug": "",
227
+ "err": "",
228
+ "out": "",
229
+ "warn": "▲ [WARNING] wrangler.toml already exists!
230
+
231
+ ",
232
+ }
233
+ `);
234
+ });
235
+
236
+ it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
237
+ fs.mkdirSync("path/to/worker", { recursive: true });
238
+ fs.writeFileSync(
239
+ "path/to/worker/wrangler.toml",
240
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
241
+ "utf-8"
242
+ );
243
+ mockConfirm({
244
+ text: "Do you want to continue initializing this project?",
245
+ result: false,
246
+ });
247
+ await runWrangler("init path/to/worker");
248
+ expect(std.warn).toContain("wrangler.toml already exists!");
249
+ const parsed = TOML.parse(
250
+ await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
251
+ );
252
+ expect(parsed.compatibility_date).toBe("something-else");
253
+ expect(std).toMatchInlineSnapshot(`
254
+ Object {
255
+ "debug": "",
256
+ "err": "",
257
+ "out": "",
258
+ "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
259
+
260
+ ",
261
+ }
262
+ `);
263
+ });
264
+
265
+ it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
266
+ fs.writeFileSync(
267
+ "./wrangler.toml",
268
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
269
+ "utf-8"
270
+ );
271
+ mockConfirm(
272
+ {
273
+ text: "Would you like to use git to manage this Worker?",
274
+ result: true,
275
+ },
276
+ {
277
+ text: "Do you want to continue initializing this project?",
278
+ result: true,
279
+ },
280
+ {
281
+ text: "No package.json found. Would you like to create one?",
282
+ result: true,
283
+ },
284
+ {
285
+ text: "Would you like to use TypeScript?",
286
+ result: true,
287
+ },
288
+ {
289
+ text: "Would you like to create a Worker at src/index.ts?",
290
+ result: true,
291
+ }
292
+ );
293
+
294
+ await runWrangler("init");
295
+ expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
296
+ `"compatibility_date=\\"something-else\\""`
297
+ );
298
+ });
299
+
300
+ it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
301
+ fs.writeFileSync(
302
+ "./wrangler.toml",
303
+ `compatibility_date="something-else"`,
304
+ "utf-8"
305
+ );
306
+ mockConfirm(
307
+ {
308
+ text: "Do you want to continue initializing this project?",
309
+ result: true,
310
+ },
311
+ {
312
+ text: "Would you like to use git to manage this Worker?",
313
+ result: false,
314
+ },
315
+ {
316
+ text: "No package.json found. Would you like to create one?",
317
+ result: false,
318
+ }
319
+ );
320
+ await runWrangler("init");
321
+ expect(std.warn).toContain("wrangler.toml already exists!");
322
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
323
+ expect(parsed.compatibility_date).toBe("something-else");
324
+ expect(std).toMatchInlineSnapshot(`
325
+ Object {
326
+ "debug": "",
327
+ "err": "",
328
+ "out": "",
329
+ "warn": "▲ [WARNING] wrangler.toml already exists!
330
+
331
+ ",
332
+ }
333
+ `);
334
+ });
335
+ });
336
+
337
+ describe("git init", () => {
338
+ it("should offer to initialize a git repository", async () => {
339
+ mockConfirm(
340
+ {
341
+ text: "Would you like to use git to manage this Worker?",
342
+ result: true,
343
+ },
344
+ {
345
+ text: "No package.json found. Would you like to create one?",
346
+ result: false,
347
+ }
348
+ );
349
+ await runWrangler("init");
350
+ expect(std).toMatchInlineSnapshot(`
351
+ Object {
352
+ "debug": "",
353
+ "err": "",
354
+ "out": "✨ Created wrangler.toml
355
+ ✨ Initialized git repository",
356
+ "warn": "",
357
+ }
358
+ `);
359
+ expect(fs.lstatSync(".git").isDirectory()).toBe(true);
360
+ expect(fs.lstatSync(".gitignore").isFile()).toBe(true);
361
+ });
362
+
363
+ it("should not offer to initialize a git repo if it's already inside one", async () => {
364
+ await execa("git", ["init"]);
365
+ fs.mkdirSync("some-folder");
366
+ process.chdir("some-folder");
367
+ await runWrangler("init -y");
368
+
369
+ // Note the lack of "✨ Initialized git repository" in the log
370
+ expect(std).toMatchInlineSnapshot(`
371
+ Object {
372
+ "debug": "",
373
+ "err": "",
374
+ "out": "✨ Created wrangler.toml
375
+ ✨ Created package.json
376
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
377
+ ✨ Created src/index.ts
378
+
379
+ To start developing your Worker, run \`npm start\`
380
+ To publish your Worker to the Internet, run \`npm run publish\`",
381
+ "warn": "",
382
+ }
383
+ `);
384
+ });
385
+
386
+ it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
387
+ fs.mkdirSync("path/to/worker", { recursive: true });
388
+ await execa("git", ["init"], { cwd: "path/to/worker" });
389
+ expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
390
+
391
+ await runWrangler("init path/to/worker/my-worker -y");
392
+
393
+ // Note the lack of "✨ Initialized git repository" in the log
394
+ expect(std).toMatchInlineSnapshot(`
395
+ Object {
396
+ "debug": "",
397
+ "err": "",
398
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
399
+ ✨ Created path/to/worker/my-worker/package.json
400
+ ✨ Created path/to/worker/my-worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies
401
+ ✨ Created path/to/worker/my-worker/src/index.ts
402
+
403
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
404
+ To publish your Worker to the Internet, run \`npm run publish\`",
405
+ "warn": "",
406
+ }
407
+ `);
408
+ });
409
+
410
+ it.todo(
411
+ "should not offer to initialize a git repo if git is not installed"
412
+ );
413
+ // I... don't know how to test this lol
414
+ });
415
+
416
+ describe("package.json", () => {
417
+ it("should create a package.json if none is found and user confirms", async () => {
418
+ mockConfirm(
419
+ {
420
+ text: "Would you like to use git to manage this Worker?",
421
+ result: false,
422
+ },
423
+ {
424
+ text: "No package.json found. Would you like to create one?",
425
+ result: true,
426
+ },
427
+ {
428
+ text: "Would you like to use TypeScript?",
429
+ result: false,
430
+ },
431
+ {
432
+ text: "Would you like to create a Worker at src/index.js?",
433
+ result: false,
434
+ }
435
+ );
436
+ await runWrangler("init");
437
+ expect(fs.existsSync("./package.json")).toBe(true);
438
+ const packageJson = JSON.parse(
439
+ fs.readFileSync("./package.json", "utf-8")
440
+ );
441
+ expect(packageJson.name).toContain("wrangler-tests");
442
+ expect(packageJson.version).toEqual("0.0.0");
443
+ expect(packageJson.devDependencies).toEqual({
444
+ wrangler: expect.any(String),
445
+ });
446
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
447
+ expect(mockPackageManager.install).toHaveBeenCalled();
448
+ expect(std).toMatchInlineSnapshot(`
449
+ Object {
450
+ "debug": "",
451
+ "err": "",
452
+ "out": "✨ Created wrangler.toml
453
+ ✨ Created package.json",
454
+ "warn": "",
455
+ }
456
+ `);
457
+ });
458
+
459
+ it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
460
+ mockConfirm(
461
+ {
462
+ text: "Would you like to use git to manage this Worker?",
463
+ result: false,
464
+ },
465
+ {
466
+ text: "No package.json found. Would you like to create one?",
467
+ result: true,
468
+ },
469
+ {
470
+ text: "Would you like to use TypeScript?",
471
+ result: false,
472
+ },
473
+ {
474
+ text: "Would you like to create a Worker at my-worker/src/index.js?",
475
+ result: false,
476
+ }
477
+ );
478
+ await runWrangler("init my-worker");
479
+ const packageJson = JSON.parse(
480
+ fs.readFileSync("./my-worker/package.json", "utf-8")
481
+ );
482
+ expect(packageJson.name).toBe("my-worker");
483
+ expect(std).toMatchInlineSnapshot(`
484
+ Object {
485
+ "debug": "",
486
+ "err": "",
487
+ "out": "✨ Created my-worker/wrangler.toml
488
+ ✨ Created my-worker/package.json",
489
+ "warn": "",
490
+ }
491
+ `);
492
+ });
493
+
494
+ it("should not touch an existing package.json in the same directory", async () => {
495
+ mockConfirm(
496
+ {
497
+ text: "Would you like to use git to manage this Worker?",
498
+ result: false,
499
+ },
500
+ {
501
+ text: "Would you like to install wrangler into package.json?",
502
+ result: false,
503
+ },
504
+ {
505
+ text: "Would you like to use TypeScript?",
506
+ result: false,
507
+ },
508
+ {
509
+ text: "Would you like to create a Worker at src/index.js?",
510
+ result: false,
511
+ }
512
+ );
513
+
514
+ fs.writeFileSync(
515
+ "./package.json",
516
+ JSON.stringify({ name: "test", version: "1.0.0" }),
517
+ "utf-8"
518
+ );
519
+
520
+ await runWrangler("init");
521
+ const packageJson = JSON.parse(
522
+ fs.readFileSync("./package.json", "utf-8")
523
+ );
524
+ expect(packageJson.name).toEqual("test");
525
+ expect(packageJson.version).toEqual("1.0.0");
526
+ expect(std).toMatchInlineSnapshot(`
527
+ Object {
528
+ "debug": "",
529
+ "err": "",
530
+ "out": "✨ Created wrangler.toml",
531
+ "warn": "",
532
+ }
533
+ `);
534
+ });
535
+
536
+ it("should not touch an existing package.json in a target directory", async () => {
537
+ mockConfirm(
538
+ {
539
+ text: "Would you like to use git to manage this Worker?",
540
+ result: false,
541
+ },
542
+ {
543
+ text: "Would you like to install wrangler into path/to/worker/package.json?",
544
+ result: false,
545
+ },
546
+ {
547
+ text: "Would you like to use TypeScript?",
548
+ result: false,
549
+ },
550
+ {
551
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
552
+ result: false,
553
+ }
554
+ );
555
+
556
+ fs.mkdirSync("path/to/worker", { recursive: true });
557
+ fs.writeFileSync(
558
+ "path/to/worker/package.json",
559
+ JSON.stringify({ name: "test", version: "1.0.0" }),
560
+ "utf-8"
561
+ );
562
+
563
+ await runWrangler("init path/to/worker/my-worker");
564
+ const packageJson = JSON.parse(
565
+ fs.readFileSync("path/to/worker/package.json", "utf-8")
566
+ );
567
+ expect(packageJson.name).toEqual("test");
568
+ expect(packageJson.version).toEqual("1.0.0");
569
+ expect(std).toMatchInlineSnapshot(`
570
+ Object {
571
+ "debug": "",
572
+ "err": "",
573
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml",
574
+ "warn": "",
575
+ }
576
+ `);
577
+ });
578
+
579
+ it("should offer to install wrangler into an existing package.json", async () => {
580
+ mockConfirm(
581
+ {
582
+ text: "Would you like to use git to manage this Worker?",
583
+ result: false,
584
+ },
585
+ {
586
+ text: "Would you like to install wrangler into package.json?",
587
+ result: true,
588
+ },
589
+ {
590
+ text: "Would you like to use TypeScript?",
591
+ result: false,
592
+ },
593
+ {
594
+ text: "Would you like to create a Worker at src/index.js?",
595
+ result: false,
596
+ }
597
+ );
598
+
599
+ fs.writeFileSync(
600
+ "./package.json",
601
+ JSON.stringify({ name: "test", version: "1.0.0" }),
602
+ "utf-8"
603
+ );
604
+
605
+ await runWrangler("init");
606
+ const packageJson = JSON.parse(
607
+ fs.readFileSync("./package.json", "utf-8")
608
+ );
609
+ expect(packageJson.name).toEqual("test");
610
+ expect(packageJson.version).toEqual("1.0.0");
611
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
612
+ `wrangler@${wranglerVersion}`
613
+ );
614
+ expect(std).toMatchInlineSnapshot(`
615
+ Object {
616
+ "debug": "",
617
+ "err": "",
618
+ "out": "✨ Created wrangler.toml
619
+ ✨ Installed wrangler",
620
+ "warn": "",
621
+ }
622
+ `);
623
+ });
624
+
625
+ it("should offer to install wrangler into a package.json relative to the target directory", async () => {
626
+ mockConfirm(
627
+ {
628
+ text: "Would you like to use git to manage this Worker?",
629
+ result: false,
630
+ },
631
+ {
632
+ text: "Would you like to install wrangler into path/to/worker/package.json?",
633
+ result: true,
634
+ },
635
+ {
636
+ text: "Would you like to use TypeScript?",
637
+ result: false,
638
+ },
639
+ {
640
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
641
+ result: false,
642
+ }
643
+ );
644
+
645
+ fs.mkdirSync("path/to/worker", { recursive: true });
646
+ fs.writeFileSync(
647
+ "path/to/worker/package.json",
648
+ JSON.stringify({ name: "test", version: "1.0.0" }),
649
+ "utf-8"
650
+ );
651
+
652
+ await runWrangler("init path/to/worker/my-worker");
653
+ const packageJson = JSON.parse(
654
+ fs.readFileSync("path/to/worker/package.json", "utf-8")
655
+ );
656
+ expect(packageJson.name).toEqual("test");
657
+ expect(packageJson.version).toEqual("1.0.0");
658
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
659
+ `wrangler@${wranglerVersion}`
660
+ );
661
+ expect(mockPackageManager.cwd).toBe(process.cwd());
662
+ expect(std).toMatchInlineSnapshot(`
663
+ Object {
664
+ "debug": "",
665
+ "err": "",
666
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
667
+ ✨ Installed wrangler",
668
+ "warn": "",
669
+ }
670
+ `);
671
+ });
672
+
673
+ it("should not touch an existing package.json in an ancestor directory", async () => {
674
+ mockConfirm(
675
+ {
676
+ text: "Would you like to use git to manage this Worker?",
677
+ result: false,
678
+ },
679
+ {
680
+ text: "Would you like to install wrangler into ../../package.json?",
681
+ result: false,
682
+ },
683
+ {
684
+ text: "Would you like to use TypeScript?",
685
+ result: false,
686
+ },
687
+ {
688
+ text: "Would you like to create a Worker at src/index.js?",
689
+ result: false,
690
+ }
691
+ );
692
+
693
+ fs.writeFileSync(
694
+ "./package.json",
695
+ JSON.stringify({ name: "test", version: "1.0.0" }),
696
+ "utf-8"
697
+ );
698
+
699
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
700
+ process.chdir("./sub-1/sub-2");
701
+
702
+ await runWrangler("init");
703
+ expect(fs.existsSync("./package.json")).toBe(false);
704
+ expect(fs.existsSync("../../package.json")).toBe(true);
705
+
706
+ const packageJson = JSON.parse(
707
+ fs.readFileSync("../../package.json", "utf-8")
708
+ );
709
+ expect(packageJson).toMatchInlineSnapshot(`
710
+ Object {
711
+ "name": "test",
712
+ "version": "1.0.0",
713
+ }
714
+ `);
715
+ expect(std).toMatchInlineSnapshot(`
716
+ Object {
717
+ "debug": "",
718
+ "err": "",
719
+ "out": "✨ Created wrangler.toml",
720
+ "warn": "",
721
+ }
722
+ `);
723
+ });
724
+ });
725
+
726
+ describe("typescript", () => {
727
+ it("should offer to create a worker in a non-typescript project", async () => {
728
+ mockConfirm(
729
+ {
730
+ text: "Would you like to use git to manage this Worker?",
731
+ result: false,
732
+ },
733
+ {
734
+ text: "Would you like to install wrangler into package.json?",
735
+ result: false,
736
+ },
737
+ {
738
+ text: "Would you like to use TypeScript?",
739
+ result: false,
740
+ },
741
+ {
742
+ text: "Would you like to create a Worker at src/index.js?",
743
+ result: true,
744
+ }
745
+ );
746
+
747
+ fs.writeFileSync(
748
+ "./package.json",
749
+ JSON.stringify({ name: "test", version: "1.0.0" }),
750
+ "utf-8"
751
+ );
752
+
753
+ await runWrangler("init");
754
+ expect(fs.existsSync("./src/index.js")).toBe(true);
755
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
756
+ expect(std).toMatchInlineSnapshot(`
757
+ Object {
758
+ "debug": "",
759
+ "err": "",
760
+ "out": "✨ Created wrangler.toml
761
+ ✨ Created src/index.js
762
+
763
+ To start developing your Worker, run \`npx wrangler dev\`
764
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
765
+ "warn": "",
766
+ }
767
+ `);
768
+ });
769
+
770
+ it("should offer to create a worker in a typescript project", async () => {
771
+ mockConfirm(
772
+ {
773
+ text: "Would you like to use git to manage this Worker?",
774
+ result: false,
775
+ },
776
+ {
777
+ text: "Would you like to install wrangler into package.json?",
778
+ result: false,
779
+ },
780
+ {
781
+ text: "Would you like to use TypeScript?",
782
+ result: true,
783
+ },
784
+ {
785
+ text: "Would you like to create a Worker at src/index.ts?",
786
+ result: true,
787
+ }
788
+ );
789
+
790
+ fs.writeFileSync(
791
+ "./package.json",
792
+ JSON.stringify({ name: "test", version: "1.0.0" }),
793
+ "utf-8"
794
+ );
795
+
796
+ await runWrangler("init");
797
+ expect(fs.existsSync("./src/index.js")).toBe(false);
798
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
799
+ expect(std).toMatchInlineSnapshot(`
800
+ Object {
801
+ "debug": "",
802
+ "err": "",
803
+ "out": "✨ Created wrangler.toml
804
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
805
+ ✨ Created src/index.ts
806
+
807
+ To start developing your Worker, run \`npx wrangler dev\`
808
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
809
+ "warn": "",
810
+ }
811
+ `);
812
+ });
813
+
814
+ it("should add scripts for a typescript project with .ts extension", async () => {
815
+ mockConfirm(
816
+ {
817
+ text: "Would you like to use git to manage this Worker?",
818
+ result: false,
819
+ },
820
+ {
821
+ text: "No package.json found. Would you like to create one?",
822
+ result: true,
823
+ },
824
+ {
825
+ text: "Would you like to install wrangler into package.json?",
826
+ result: false,
827
+ },
828
+ {
829
+ text: "Would you like to use TypeScript?",
830
+ result: true,
831
+ },
832
+ {
833
+ text: "Would you like to create a Worker at src/index.ts?",
834
+ result: true,
835
+ }
836
+ );
837
+ await runWrangler("init");
838
+
839
+ expect(fs.existsSync("./package.json")).toBe(true);
840
+ const packageJson = JSON.parse(
841
+ fs.readFileSync("./package.json", "utf-8")
842
+ );
843
+
844
+ expect(fs.existsSync("./src/index.js")).toBe(false);
845
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
846
+
847
+ expect(packageJson.scripts.start).toBe("wrangler dev");
848
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
849
+ expect(packageJson.name).toContain("wrangler-tests");
850
+ expect(packageJson.version).toEqual("0.0.0");
851
+ expect(std.out).toMatchInlineSnapshot(`
852
+ "✨ Created wrangler.toml
853
+ ✨ Created package.json
854
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
855
+ ✨ Created src/index.ts
856
+
857
+ To start developing your Worker, run \`npm start\`
858
+ To publish your Worker to the Internet, run \`npm run publish\`"
859
+ `);
860
+ });
861
+
862
+ it("should not overwrite package.json scripts for a typescript project", async () => {
863
+ mockConfirm(
864
+ {
865
+ text: "Would you like to use git to manage this Worker?",
866
+ result: false,
867
+ },
868
+ {
869
+ text: "Would you like to install wrangler into package.json?",
870
+ result: false,
871
+ },
872
+ {
873
+ text: "Would you like to use TypeScript?",
874
+ result: true,
875
+ },
876
+ {
877
+ text: "Would you like to create a Worker at src/index.ts?",
878
+ result: true,
879
+ }
880
+ );
881
+ await fsp.writeFile(
882
+ "./package.json",
883
+ JSON.stringify({
884
+ scripts: {
885
+ start: "test-start",
886
+ publish: "test-publish",
887
+ },
888
+ })
889
+ );
890
+ const packageJson = JSON.parse(
891
+ fs.readFileSync("./package.json", "utf-8")
892
+ );
893
+ await runWrangler("init");
894
+
895
+ expect(fs.existsSync("./src/index.js")).toBe(false);
896
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
897
+
898
+ expect(packageJson.scripts.start).toBe("test-start");
899
+ expect(packageJson.scripts.publish).toBe("test-publish");
900
+ expect(std.out).toMatchInlineSnapshot(`
901
+ "✨ Created wrangler.toml
902
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
903
+ ✨ Created src/index.ts
904
+
905
+ To start developing your Worker, run \`npx wrangler dev\`
906
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
907
+ `);
908
+ });
909
+
910
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
911
+ mockConfirm(
912
+ {
913
+ text: "Would you like to use git to manage this Worker?",
914
+ result: false,
915
+ },
916
+ {
917
+ text: "Would you like to install wrangler into package.json?",
918
+ result: false,
919
+ },
920
+ {
921
+ text: "Would you like to use TypeScript?",
922
+ result: true,
923
+ }
924
+ );
925
+
926
+ fs.writeFileSync(
927
+ "./package.json",
928
+ JSON.stringify({ name: "test", version: "1.0.0" }),
929
+ "utf-8"
930
+ );
931
+ fs.mkdirSync("./src", { recursive: true });
932
+ const PLACEHOLDER = "/* placeholder text */";
933
+ fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
934
+
935
+ await runWrangler("init");
936
+ expect(fs.existsSync("./src/index.js")).toBe(false);
937
+ expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
938
+ expect(std).toMatchInlineSnapshot(`
939
+ Object {
940
+ "debug": "",
941
+ "err": "",
942
+ "out": "✨ Created wrangler.toml
943
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies",
944
+ "warn": "",
945
+ }
946
+ `);
947
+ });
948
+
949
+ 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 () => {
950
+ mockConfirm(
951
+ {
952
+ text: "Would you like to use git to manage this Worker?",
953
+ result: false,
954
+ },
955
+ {
956
+ text: "Would you like to install wrangler into package.json?",
957
+ result: false,
958
+ },
959
+ {
960
+ text: "Would you like to use TypeScript?",
961
+ result: true,
962
+ }
963
+ );
964
+
965
+ fs.writeFileSync(
966
+ "./package.json",
967
+ JSON.stringify({ name: "test", version: "1.0.0" }),
968
+ "utf-8"
969
+ );
970
+ fs.mkdirSync("./my-worker/src", { recursive: true });
971
+ const PLACEHOLDER = "/* placeholder text */";
972
+ fs.writeFileSync("./my-worker/src/index.ts", PLACEHOLDER, "utf-8");
973
+
974
+ await runWrangler("init my-worker");
975
+ expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
976
+ expect(fs.readFileSync("./my-worker/src/index.ts", "utf-8")).toBe(
977
+ PLACEHOLDER
978
+ );
979
+ expect(std).toMatchInlineSnapshot(`
980
+ Object {
981
+ "debug": "",
982
+ "err": "",
983
+ "out": "✨ Created my-worker/wrangler.toml
984
+ ✨ Created my-worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies",
985
+ "warn": "",
986
+ }
987
+ `);
988
+ });
989
+
990
+ it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
991
+ mockConfirm(
992
+ {
993
+ text: "Would you like to use git to manage this Worker?",
994
+ result: false,
995
+ },
996
+ {
997
+ text: "No package.json found. Would you like to create one?",
998
+ result: true,
999
+ },
1000
+ {
1001
+ text: "Would you like to use TypeScript?",
1002
+ result: true,
1003
+ },
1004
+ {
1005
+ text: "Would you like to create a Worker at src/index.ts?",
1006
+ result: false,
1007
+ }
1008
+ );
1009
+ await runWrangler("init");
1010
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
1011
+ const { config: tsconfigJson, error: tsConfigParseError } =
1012
+ parseConfigFileTextToJson(
1013
+ "./tsconfig.json",
1014
+ fs.readFileSync("./tsconfig.json", "utf-8")
1015
+ );
1016
+ expect(tsConfigParseError).toBeUndefined();
1017
+ expect(tsconfigJson.compilerOptions.types).toEqual([
1018
+ "@cloudflare/workers-types",
1019
+ ]);
1020
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1021
+ "@cloudflare/workers-types",
1022
+ "typescript"
1023
+ );
1024
+ expect(std).toMatchInlineSnapshot(`
1025
+ Object {
1026
+ "debug": "",
1027
+ "err": "",
1028
+ "out": "✨ Created wrangler.toml
1029
+ ✨ Created package.json
1030
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies",
1031
+ "warn": "",
1032
+ }
1033
+ `);
1034
+ });
1035
+
1036
+ it("should not touch an existing tsconfig.json in the same directory", async () => {
1037
+ fs.writeFileSync(
1038
+ "./package.json",
1039
+ JSON.stringify({
1040
+ name: "test",
1041
+ version: "1.0.0",
1042
+ devDependencies: {
1043
+ wrangler: "0.0.0",
1044
+ "@cloudflare/workers-types": "0.0.0",
1045
+ },
1046
+ }),
1047
+ "utf-8"
1048
+ );
1049
+ fs.writeFileSync(
1050
+ "./tsconfig.json",
1051
+ JSON.stringify({ compilerOptions: {} }),
1052
+ "utf-8"
1053
+ );
1054
+
1055
+ mockConfirm(
1056
+ {
1057
+ text: "Would you like to use git to manage this Worker?",
1058
+ result: false,
1059
+ },
1060
+ {
1061
+ text: "Would you like to create a Worker at src/index.ts?",
1062
+ result: true,
1063
+ }
1064
+ );
1065
+
1066
+ await runWrangler("init");
1067
+ const tsconfigJson = JSON.parse(
1068
+ fs.readFileSync("./tsconfig.json", "utf-8")
1069
+ );
1070
+ expect(tsconfigJson.compilerOptions).toEqual({});
1071
+ expect(std).toMatchInlineSnapshot(`
1072
+ Object {
1073
+ "debug": "",
1074
+ "err": "",
1075
+ "out": "✨ Created wrangler.toml
1076
+ ✨ Created src/index.ts
1077
+
1078
+ To start developing your Worker, run \`npx wrangler dev\`
1079
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1080
+ "warn": "",
1081
+ }
1082
+ `);
1083
+ });
1084
+
1085
+ it("should not touch an existing tsconfig.json in the ancestor of a target directory", async () => {
1086
+ fs.mkdirSync("path/to/worker", { recursive: true });
1087
+ fs.writeFileSync(
1088
+ "path/to/worker/package.json",
1089
+ JSON.stringify({
1090
+ name: "test",
1091
+ version: "1.0.0",
1092
+ devDependencies: {
1093
+ wrangler: "0.0.0",
1094
+ "@cloudflare/workers-types": "0.0.0",
1095
+ },
1096
+ }),
1097
+ "utf-8"
1098
+ );
1099
+ fs.writeFileSync(
1100
+ "path/to/worker/tsconfig.json",
1101
+ JSON.stringify({ compilerOptions: {} }),
1102
+ "utf-8"
1103
+ );
1104
+
1105
+ mockConfirm(
1106
+ {
1107
+ text: "Would you like to use git to manage this Worker?",
1108
+ result: false,
1109
+ },
1110
+ {
1111
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
1112
+ result: true,
1113
+ }
1114
+ );
1115
+
1116
+ await runWrangler("init path/to/worker/my-worker");
1117
+ const tsconfigJson = JSON.parse(
1118
+ fs.readFileSync("path/to/worker/tsconfig.json", "utf-8")
1119
+ );
1120
+ expect(tsconfigJson.compilerOptions).toEqual({});
1121
+ expect(std).toMatchInlineSnapshot(`
1122
+ Object {
1123
+ "debug": "",
1124
+ "err": "",
1125
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1126
+ ✨ Created path/to/worker/my-worker/src/index.ts
1127
+
1128
+ To start developing your Worker, run \`npx wrangler dev\`
1129
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1130
+ "warn": "",
1131
+ }
1132
+ `);
1133
+ });
1134
+
1135
+ it("should offer to install type definitions in an existing typescript project", async () => {
1136
+ mockConfirm(
1137
+ {
1138
+ text: "Would you like to use git to manage this Worker?",
1139
+ result: false,
1140
+ },
1141
+ {
1142
+ text: "Would you like to install wrangler into package.json?",
1143
+ result: false,
1144
+ },
1145
+ {
1146
+ text: "Would you like to install the type definitions for Workers into your package.json?",
1147
+ result: true,
1148
+ },
1149
+ {
1150
+ text: "Would you like to create a Worker at src/index.ts?",
1151
+ result: false,
1152
+ }
1153
+ );
1154
+ fs.writeFileSync(
1155
+ "./package.json",
1156
+ JSON.stringify({
1157
+ name: "test",
1158
+ version: "1.0.0",
1159
+ }),
1160
+ "utf-8"
1161
+ );
1162
+ fs.writeFileSync(
1163
+ "./tsconfig.json",
1164
+ JSON.stringify({ compilerOptions: {} }),
1165
+ "utf-8"
1166
+ );
1167
+
1168
+ await runWrangler("init");
1169
+ const tsconfigJson = JSON.parse(
1170
+ fs.readFileSync("./tsconfig.json", "utf-8")
1171
+ );
1172
+ // unchanged tsconfig
1173
+ expect(tsconfigJson.compilerOptions).toEqual({});
1174
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1175
+ "@cloudflare/workers-types"
1176
+ );
1177
+ expect(std).toMatchInlineSnapshot(`
1178
+ Object {
1179
+ "debug": "",
1180
+ "err": "",
1181
+ "out": "✨ Created wrangler.toml
1182
+ ✨ Installed @cloudflare/workers-types.
1183
+ Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1184
+ "warn": "",
1185
+ }
1186
+ `);
1187
+ });
1188
+
1189
+ it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1190
+ fs.writeFileSync(
1191
+ "./package.json",
1192
+ JSON.stringify({
1193
+ name: "test",
1194
+ version: "1.0.0",
1195
+ devDependencies: {
1196
+ wrangler: "0.0.0",
1197
+ "@cloudflare/workers-types": "0.0.0",
1198
+ },
1199
+ }),
1200
+ "utf-8"
1201
+ );
1202
+ fs.writeFileSync(
1203
+ "./tsconfig.json",
1204
+ JSON.stringify({ compilerOptions: {} }),
1205
+ "utf-8"
1206
+ );
1207
+
1208
+ mockConfirm(
1209
+ {
1210
+ text: "Would you like to use git to manage this Worker?",
1211
+ result: false,
1212
+ },
1213
+ {
1214
+ text: "Would you like to create a Worker at src/index.ts?",
1215
+ result: true,
1216
+ }
1217
+ );
1218
+
1219
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
1220
+ process.chdir("./sub-1/sub-2");
1221
+
1222
+ await runWrangler("init");
1223
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
1224
+ expect(fs.existsSync("../../tsconfig.json")).toBe(true);
1225
+
1226
+ const tsconfigJson = JSON.parse(
1227
+ fs.readFileSync("../../tsconfig.json", "utf-8")
1228
+ );
1229
+ expect(tsconfigJson.compilerOptions).toEqual({});
1230
+
1231
+ expect(std).toMatchInlineSnapshot(`
1232
+ Object {
1233
+ "debug": "",
1234
+ "err": "",
1235
+ "out": "✨ Created wrangler.toml
1236
+ ✨ Created src/index.ts
1237
+
1238
+ To start developing your Worker, run \`npx wrangler dev\`
1239
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1240
+ "warn": "",
1241
+ }
1242
+ `);
1243
+ });
1244
+ });
1245
+
1246
+ describe("javascript", () => {
1247
+ it("should add missing scripts for a non-ts project with .js extension", async () => {
1248
+ mockConfirm(
1249
+ {
1250
+ text: "Would you like to use git to manage this Worker?",
1251
+ result: false,
1252
+ },
1253
+ {
1254
+ text: "No package.json found. Would you like to create one?",
1255
+ result: true,
1256
+ },
1257
+ {
1258
+ text: "Would you like to install wrangler into package.json?",
1259
+ result: false,
1260
+ },
1261
+ {
1262
+ text: "Would you like to use TypeScript?",
1263
+ result: false,
1264
+ },
1265
+ {
1266
+ text: "Would you like to create a Worker at src/index.js?",
1267
+ result: true,
1268
+ }
1269
+ );
1270
+ await runWrangler("init");
1271
+
1272
+ expect(fs.existsSync("./package.json")).toBe(true);
1273
+ const packageJson = JSON.parse(
1274
+ fs.readFileSync("./package.json", "utf-8")
1275
+ );
1276
+
1277
+ expect(fs.existsSync("./src/index.js")).toBe(true);
1278
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
1279
+
1280
+ expect(packageJson.scripts.start).toBe("wrangler dev");
1281
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
1282
+ expect(packageJson.name).toContain("wrangler-tests");
1283
+ expect(packageJson.version).toEqual("0.0.0");
1284
+ expect(std.out).toMatchInlineSnapshot(`
1285
+ "✨ Created wrangler.toml
1286
+ ✨ Created package.json
1287
+ ✨ Created src/index.js
1288
+
1289
+ To start developing your Worker, run \`npm start\`
1290
+ To publish your Worker to the Internet, run \`npm run publish\`"
1291
+ `);
1292
+ });
1293
+
1294
+ it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
1295
+ mockConfirm(
1296
+ {
1297
+ text: "Would you like to use git to manage this Worker?",
1298
+ result: false,
1299
+ },
1300
+ {
1301
+ text: "Would you like to install wrangler into package.json?",
1302
+ result: false,
1303
+ },
1304
+ {
1305
+ text: "Would you like to use TypeScript?",
1306
+ result: false,
1307
+ },
1308
+ {
1309
+ text: "Would you like to create a Worker at src/index.js?",
1310
+ result: true,
1311
+ }
1312
+ );
1313
+ await fsp.writeFile(
1314
+ "./package.json",
1315
+ JSON.stringify({
1316
+ scripts: {
1317
+ start: "test-start",
1318
+ publish: "test-publish",
1319
+ },
1320
+ })
1321
+ );
1322
+ const packageJson = JSON.parse(
1323
+ fs.readFileSync("./package.json", "utf-8")
1324
+ );
1325
+ await runWrangler("init");
1326
+
1327
+ expect(fs.existsSync("./src/index.js")).toBe(true);
1328
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
1329
+
1330
+ expect(packageJson.scripts.start).toBe("test-start");
1331
+ expect(packageJson.scripts.publish).toBe("test-publish");
1332
+ expect(std.out).toMatchInlineSnapshot(`
1333
+ "✨ Created wrangler.toml
1334
+ ✨ Created src/index.js
1335
+
1336
+ To start developing your Worker, run \`npx wrangler dev\`
1337
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1338
+ `);
1339
+ });
1340
+
1341
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
1342
+ mockConfirm(
1343
+ {
1344
+ text: "Would you like to use git to manage this Worker?",
1345
+ result: false,
1346
+ },
1347
+ {
1348
+ text: "Would you like to install wrangler into package.json?",
1349
+ result: false,
1350
+ },
1351
+ {
1352
+ text: "Would you like to use TypeScript?",
1353
+ result: false,
1354
+ }
1355
+ );
1356
+
1357
+ fs.writeFileSync(
1358
+ "./package.json",
1359
+ JSON.stringify({ name: "test", version: "1.0.0" }),
1360
+ "utf-8"
1361
+ );
1362
+ fs.mkdirSync("./src", { recursive: true });
1363
+ const PLACEHOLDER = "/* placeholder text */";
1364
+ fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
1365
+
1366
+ await runWrangler("init");
1367
+ expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
1368
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
1369
+ expect(std).toMatchInlineSnapshot(`
1370
+ Object {
1371
+ "debug": "",
1372
+ "err": "",
1373
+ "out": "✨ Created wrangler.toml",
1374
+ "warn": "",
1375
+ }
1376
+ `);
1377
+ });
1378
+
1379
+ it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1380
+ mockConfirm(
1381
+ {
1382
+ text: "Would you like to use git to manage this Worker?",
1383
+ result: false,
1384
+ },
1385
+ {
1386
+ text: "Would you like to install wrangler into package.json?",
1387
+ result: false,
1388
+ },
1389
+ {
1390
+ text: "Would you like to use TypeScript?",
1391
+ result: false,
1392
+ }
1393
+ );
1394
+
1395
+ fs.writeFileSync(
1396
+ "./package.json",
1397
+ JSON.stringify({ name: "test", version: "1.0.0" }),
1398
+ "utf-8"
1399
+ );
1400
+ fs.mkdirSync("./my-worker/src", { recursive: true });
1401
+ const PLACEHOLDER = "/* placeholder text */";
1402
+ fs.writeFileSync("./my-worker/src/index.js", PLACEHOLDER, "utf-8");
1403
+
1404
+ await runWrangler("init my-worker");
1405
+ expect(fs.readFileSync("./my-worker/src/index.js", "utf-8")).toBe(
1406
+ PLACEHOLDER
1407
+ );
1408
+ expect(fs.existsSync("./my-worker/src/index.ts")).toBe(false);
1409
+ expect(std).toMatchInlineSnapshot(`
1410
+ Object {
1411
+ "debug": "",
1412
+ "err": "",
1413
+ "out": "✨ Created my-worker/wrangler.toml",
1414
+ "warn": "",
1415
+ }
1416
+ `);
1417
+ });
1418
+ });
1419
+
1420
+ describe("worker names", () => {
1421
+ it("should create a worker with a given name", async () => {
1422
+ await runWrangler("init my-worker -y");
1423
+
1424
+ const parsed = TOML.parse(
1425
+ await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
1426
+ );
1427
+
1428
+ expect(typeof parsed.compatibility_date).toBe("string");
1429
+ expect(parsed.name).toBe("my-worker");
1430
+ });
1431
+
1432
+ it('should create a worker with the name of the current directory if "name" is .', async () => {
1433
+ await runWrangler("init . -y");
1434
+
1435
+ const parsed = TOML.parse(await fsp.readFile("wrangler.toml", "utf-8"));
1436
+
1437
+ expect(typeof parsed.compatibility_date).toBe("string");
1438
+ expect(parsed.name).toBe(path.basename(process.cwd()).toLowerCase());
1439
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1440
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1441
+ expect(std).toMatchInlineSnapshot(`
1442
+ Object {
1443
+ "debug": "",
1444
+ "err": "",
1445
+ "out": "✨ Created wrangler.toml
1446
+ ✨ Initialized git repository
1447
+ ✨ Created package.json
1448
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
1449
+ ✨ Created src/index.ts
1450
+
1451
+ To start developing your Worker, run \`npm start\`
1452
+ To publish your Worker to the Internet, run \`npm run publish\`",
1453
+ "warn": "",
1454
+ }
1455
+ `);
1456
+ });
1457
+
1458
+ it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
1459
+ await runWrangler("init path/to/worker -y");
1460
+
1461
+ const parsed = TOML.parse(
1462
+ await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
1463
+ );
1464
+
1465
+ expect(typeof parsed.compatibility_date).toBe("string");
1466
+ expect(parsed.name).toBe("worker");
1467
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1468
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1469
+ expect(std).toMatchInlineSnapshot(`
1470
+ Object {
1471
+ "debug": "",
1472
+ "err": "",
1473
+ "out": "✨ Created path/to/worker/wrangler.toml
1474
+ ✨ Initialized git repository at path/to/worker
1475
+ ✨ Created path/to/worker/package.json
1476
+ ✨ Created path/to/worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies
1477
+ ✨ Created path/to/worker/src/index.ts
1478
+
1479
+ To start developing your Worker, run \`cd path/to/worker && npm start\`
1480
+ To publish your Worker to the Internet, run \`npm run publish\`",
1481
+ "warn": "",
1482
+ }
1483
+ `);
1484
+ });
1485
+
1486
+ it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
1487
+ await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
1488
+ const parsed = TOML.parse(
1489
+ await fsp.readFile(
1490
+ "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml",
1491
+ "utf-8"
1492
+ )
1493
+ );
1494
+
1495
+ expect(typeof parsed.compatibility_date).toBe("string");
1496
+ expect(parsed.name).toBe("weird_w0rkr_n4m3-js-tsx-tar-gz");
1497
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1498
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1499
+ expect(std).toMatchInlineSnapshot(`
1500
+ Object {
1501
+ "debug": "",
1502
+ "err": "",
1503
+ "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1504
+ ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1505
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1506
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json, installed @cloudflare/workers-types into devDependencies
1507
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1508
+
1509
+ To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1510
+ To publish your Worker to the Internet, run \`npm run publish\`",
1511
+ "warn": "",
1512
+ }
1513
+ `);
1514
+ });
1515
+ });
1516
+ });