wrangler 0.0.0-e61907d → 0.0.0-e64812e

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