wrangler 0.0.0-e3cab74 → 0.0.0-e452a35

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