wrangler 0.0.0-e928f94 → 0.0.0-e978986

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