wrangler 0.0.0-e61fba8 → 0.0.0-e62b097bb

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