wrangler 0.0.0-e14e589 → 0.0.0-e2a26a0

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 (61) hide show
  1. package/README.md +51 -0
  2. package/bin/wrangler.js +2 -2
  3. package/package.json +49 -30
  4. package/pages/functions/buildWorker.ts +62 -0
  5. package/pages/functions/filepath-routing.test.ts +47 -0
  6. package/pages/functions/filepath-routing.ts +231 -0
  7. package/pages/functions/identifiers.ts +78 -0
  8. package/pages/functions/routes.ts +160 -0
  9. package/pages/functions/template-worker.ts +136 -0
  10. package/src/__tests__/dev.test.tsx +71 -0
  11. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  12. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  13. package/src/__tests__/helpers/mock-account-id.ts +30 -0
  14. package/src/__tests__/helpers/mock-bin.ts +33 -0
  15. package/src/__tests__/helpers/mock-cfetch.ts +207 -0
  16. package/src/__tests__/helpers/mock-console.ts +56 -0
  17. package/src/__tests__/helpers/mock-dialogs.ts +65 -0
  18. package/src/__tests__/helpers/mock-kv.ts +40 -0
  19. package/src/__tests__/helpers/mock-user.ts +27 -0
  20. package/src/__tests__/helpers/run-in-tmp.ts +29 -0
  21. package/src/__tests__/helpers/run-wrangler.ts +14 -0
  22. package/src/__tests__/index.test.ts +509 -119
  23. package/src/__tests__/jest.setup.ts +37 -0
  24. package/src/__tests__/kv.test.ts +1139 -0
  25. package/src/__tests__/logout.test.ts +50 -0
  26. package/src/__tests__/package-manager.test.ts +206 -0
  27. package/src/__tests__/publish.test.ts +1222 -0
  28. package/src/__tests__/secret.test.ts +210 -0
  29. package/src/__tests__/whoami.test.tsx +86 -0
  30. package/src/api/form_data.ts +85 -76
  31. package/src/api/preview.ts +29 -24
  32. package/src/api/worker.ts +50 -35
  33. package/src/cfetch/index.ts +102 -0
  34. package/src/cfetch/internal.ts +101 -0
  35. package/src/cli.ts +6 -7
  36. package/src/config.ts +630 -85
  37. package/src/dev.tsx +446 -282
  38. package/src/dialogs.tsx +6 -14
  39. package/src/index.tsx +773 -426
  40. package/src/inspect.ts +596 -0
  41. package/src/kv.tsx +118 -45
  42. package/src/module-collection.ts +63 -0
  43. package/src/package-manager.ts +120 -0
  44. package/src/pages.tsx +917 -211
  45. package/src/paths.ts +26 -0
  46. package/src/proxy.ts +370 -0
  47. package/src/publish.ts +220 -88
  48. package/src/sites.tsx +180 -55
  49. package/src/tail.tsx +6 -4
  50. package/src/user.tsx +77 -87
  51. package/src/whoami.tsx +69 -0
  52. package/templates/new-worker.js +15 -0
  53. package/templates/new-worker.ts +15 -0
  54. package/{static-asset-facade.js → templates/static-asset-facade.js} +0 -0
  55. package/wrangler-dist/cli.js +125330 -557
  56. package/wrangler-dist/cli.js.map +3 -3
  57. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  58. package/src/__tests__/mock-cfetch.js +0 -46
  59. package/src/api/inspect.ts +0 -441
  60. package/src/cfetch.ts +0 -72
  61. package/src/util/fetch.ts +0 -74
@@ -1,153 +1,543 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as fsp from "node:fs/promises";
3
- import * as path from "node:path";
4
3
  import * as TOML from "@iarna/toml";
5
- import { main } from "../index";
6
- // @ts-expect-error we're mocking cfetch, so of course setMock isn't a thing
7
- import { setMock, unsetAllMocks } from "../cfetch";
8
-
9
- jest.mock("../cfetch", () => {
10
- return jest.requireActual("./mock-cfetch");
11
- });
12
-
13
- async function w(cmd: void | string, options?: { tap: boolean }) {
14
- const tapped = options?.tap ? tap() : undefined;
15
- await main([...(cmd ? cmd.split(" ") : [])]);
16
- tapped?.off();
17
- return { stdout: tapped?.out, stderr: tapped?.err };
18
- }
19
-
20
- function tap() {
21
- const oldLog = console.log;
22
- const oldError = console.error;
23
-
24
- const toReturn = {
25
- off: () => {
26
- console.log = oldLog;
27
- console.error = oldError;
28
- },
29
- out: "",
30
- err: "",
31
- };
32
-
33
- console.log = (...args) => {
34
- toReturn.out += args.join("");
35
- oldLog.apply(console, args);
36
- // console.trace(...args); // use this if you want to find the true source of your console.log
37
- };
38
- console.error = (...args) => {
39
- toReturn.err += args.join("");
40
- oldError.apply(console, args);
41
- };
42
-
43
- return toReturn;
44
- }
4
+ import { version as wranglerVersion } from "../../package.json";
5
+ import { getPackageManager } from "../package-manager";
6
+ import { mockConsoleMethods } from "./helpers/mock-console";
7
+ import { mockConfirm } from "./helpers/mock-dialogs";
8
+ import { runInTempDir } from "./helpers/run-in-tmp";
9
+ import { runWrangler } from "./helpers/run-wrangler";
10
+ import type { PackageManager } from "../package-manager";
45
11
 
46
12
  describe("wrangler", () => {
47
- it("should run", async () => {
48
- const { stdout } = await w(undefined, { tap: true });
49
-
50
- expect(stdout).toMatchInlineSnapshot(`
51
- "wrangler
52
-
53
- Commands:
54
- wrangler init [name] 📥 Create a wrangler.toml configuration file
55
- wrangler dev <filename> 👂 Start a local server for developing your worker
56
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
57
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
58
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
59
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
60
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
61
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
62
- wrangler pages ⚡️ Configure Cloudflare Pages
63
-
64
- Flags:
65
- --config Path to .toml configuration file [string]
66
- --help Show help [boolean]
67
- --version Show version number [boolean]
68
-
69
- Options:
70
- --local Run on my machine [boolean] [default: false]"
71
- `);
13
+ let mockPackageManager: PackageManager;
14
+ runInTempDir();
15
+
16
+ beforeEach(() => {
17
+ mockPackageManager = {
18
+ addDevDeps: jest.fn(),
19
+ install: jest.fn(),
20
+ };
21
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
72
22
  });
73
23
 
74
- describe("init", () => {
75
- const ogcwd = process.cwd();
24
+ const std = mockConsoleMethods();
25
+
26
+ describe("no command", () => {
27
+ it("should display a list of available commands", async () => {
28
+ await runWrangler();
29
+
30
+ expect(std.out).toMatchInlineSnapshot(`
31
+ "wrangler
32
+
33
+ Commands:
34
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
35
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
36
+ wrangler dev <filename> 👂 Start a local server for developing your worker
37
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
38
+ wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
39
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
40
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
41
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
42
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
43
+ wrangler pages ⚡️ Configure Cloudflare Pages
76
44
 
77
- beforeEach(() => {
78
- process.chdir(path.join(__dirname, "fixtures", "init"));
45
+ Flags:
46
+ -c, --config Path to .toml configuration file [string]
47
+ -h, --help Show help [boolean]
48
+ -v, --version Show version number [boolean]
49
+
50
+ Options:
51
+ -l, --local Run on my machine [boolean] [default: false]"
52
+ `);
53
+
54
+ expect(std.err).toMatchInlineSnapshot(`""`);
79
55
  });
56
+ });
57
+
58
+ describe("invalid command", () => {
59
+ it("should display an error", async () => {
60
+ await expect(
61
+ runWrangler("invalid-command")
62
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
63
+ `"Unknown command: invalid-command."`
64
+ );
65
+
66
+ expect(std.out).toMatchInlineSnapshot(`""`);
67
+ expect(std.err).toMatchInlineSnapshot(`
68
+ "wrangler
69
+
70
+ Commands:
71
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
72
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
73
+ wrangler dev <filename> 👂 Start a local server for developing your worker
74
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
75
+ wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
76
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
77
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
78
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
79
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
80
+ wrangler pages ⚡️ Configure Cloudflare Pages
80
81
 
81
- afterEach(async () => {
82
- await fsp.rm("./wrangler.toml");
83
- process.chdir(ogcwd);
82
+ Flags:
83
+ -c, --config Path to .toml configuration file [string]
84
+ -h, --help Show help [boolean]
85
+ -v, --version Show version number [boolean]
86
+
87
+ Options:
88
+ -l, --local Run on my machine [boolean] [default: false]
89
+
90
+ Unknown command: invalid-command."
91
+ `);
84
92
  });
93
+ });
85
94
 
95
+ describe("init", () => {
86
96
  it("should create a wrangler.toml", async () => {
87
- await w("init");
97
+ mockConfirm({
98
+ text: "No package.json found. Would you like to create one?",
99
+ result: false,
100
+ });
101
+ await runWrangler("init");
88
102
  const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
89
103
  expect(typeof parsed.compatibility_date).toBe("string");
104
+ expect(fs.existsSync("./package.json")).toBe(false);
105
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
90
106
  });
91
107
 
92
- it("should error when wrangler.toml already exists", async () => {
93
- fs.closeSync(fs.openSync("./wrangler.toml", "w"));
94
- const { stderr } = await w("init", { tap: true });
95
- expect(stderr.endsWith("wrangler.toml already exists.")).toBe(true);
108
+ it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
109
+ fs.writeFileSync(
110
+ "./wrangler.toml",
111
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
112
+ "utf-8"
113
+ );
114
+ mockConfirm({
115
+ text: "Do you want to continue initializing this project?",
116
+ result: false,
117
+ });
118
+ await runWrangler("init");
119
+ expect(std.warn).toContain("wrangler.toml file already exists!");
120
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
121
+ expect(parsed.compatibility_date).toBe("something-else");
122
+ });
123
+
124
+ it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
125
+ fs.writeFileSync(
126
+ "./wrangler.toml",
127
+ `compatibility_date="something-else"`,
128
+ "utf-8"
129
+ );
130
+ mockConfirm(
131
+ {
132
+ text: "Do you want to continue initializing this project?",
133
+ result: true,
134
+ },
135
+ {
136
+ text: "No package.json found. Would you like to create one?",
137
+ result: false,
138
+ }
139
+ );
140
+ await runWrangler("init");
141
+ expect(std.warn).toContain("wrangler.toml file already exists!");
142
+ const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
143
+ expect(parsed.compatibility_date).toBe("something-else");
96
144
  });
97
- });
98
145
 
99
- describe("kv:namespace", () => {
100
- afterAll(() => {
101
- unsetAllMocks();
102
- });
103
- let KVNamespaces: { title: string; id: string }[] = [];
104
- it("can create a namespace", async () => {
105
- setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
106
- expect(init.method === "POST");
107
- const body = JSON.parse(init.body);
108
- expect(body.title).toBe("worker-UnitTestNamespace");
109
- KVNamespaces.push({ title: body.title, id: "some-namespace-id" });
110
- return { id: "some-namespace-id" };
146
+ it("should create a package.json if none is found and user confirms", async () => {
147
+ mockConfirm(
148
+ {
149
+ text: "No package.json found. Would you like to create one?",
150
+ result: true,
151
+ },
152
+ {
153
+ text: "Would you like to use TypeScript?",
154
+ result: false,
155
+ },
156
+ {
157
+ text: "Would you like to create a Worker at src/index.js?",
158
+ result: false,
159
+ }
160
+ );
161
+ await runWrangler("init");
162
+ expect(fs.existsSync("./package.json")).toBe(true);
163
+ const packageJson = JSON.parse(
164
+ fs.readFileSync("./package.json", "utf-8")
165
+ );
166
+ expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
167
+ expect(packageJson.version).toEqual("0.0.0");
168
+ expect(packageJson.devDependencies).toEqual({
169
+ wrangler: expect.any(String),
111
170
  });
171
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
172
+ expect(mockPackageManager.install).toHaveBeenCalled();
173
+ });
174
+
175
+ it("should not touch an existing package.json in the same directory", async () => {
176
+ mockConfirm(
177
+ {
178
+ text: "Would you like to install wrangler into your package.json?",
179
+ result: false,
180
+ },
181
+ {
182
+ text: "Would you like to use TypeScript?",
183
+ result: false,
184
+ },
185
+ {
186
+ text: "Would you like to create a Worker at src/index.js?",
187
+ result: false,
188
+ }
189
+ );
190
+
191
+ fs.writeFileSync(
192
+ "./package.json",
193
+ JSON.stringify({ name: "test", version: "1.0.0" }),
194
+ "utf-8"
195
+ );
196
+
197
+ await runWrangler("init");
198
+ const packageJson = JSON.parse(
199
+ fs.readFileSync("./package.json", "utf-8")
200
+ );
201
+ expect(packageJson.name).toEqual("test");
202
+ expect(packageJson.version).toEqual("1.0.0");
203
+ });
204
+
205
+ it("should offer to install wrangler into an existing package.json", async () => {
206
+ mockConfirm(
207
+ {
208
+ text: "Would you like to install wrangler into your package.json?",
209
+ result: true,
210
+ },
211
+ {
212
+ text: "Would you like to use TypeScript?",
213
+ result: false,
214
+ },
215
+ {
216
+ text: "Would you like to create a Worker at src/index.js?",
217
+ result: false,
218
+ }
219
+ );
220
+
221
+ fs.writeFileSync(
222
+ "./package.json",
223
+ JSON.stringify({ name: "test", version: "1.0.0" }),
224
+ "utf-8"
225
+ );
226
+
227
+ await runWrangler("init");
228
+ const packageJson = JSON.parse(
229
+ fs.readFileSync("./package.json", "utf-8")
230
+ );
231
+ expect(packageJson.name).toEqual("test");
232
+ expect(packageJson.version).toEqual("1.0.0");
233
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
234
+ `wrangler@${wranglerVersion}`
235
+ );
236
+ });
237
+
238
+ it("should not touch an existing package.json in an ancestor directory", async () => {
239
+ mockConfirm(
240
+ {
241
+ text: "Would you like to install wrangler into your package.json?",
242
+ result: false,
243
+ },
244
+ {
245
+ text: "Would you like to use TypeScript?",
246
+ result: false,
247
+ },
248
+ {
249
+ text: "Would you like to create a Worker at src/index.js?",
250
+ result: false,
251
+ }
252
+ );
253
+
254
+ fs.writeFileSync(
255
+ "./package.json",
256
+ JSON.stringify({ name: "test", version: "1.0.0" }),
257
+ "utf-8"
258
+ );
259
+
260
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
261
+ process.chdir("./sub-1/sub-2");
262
+
263
+ await runWrangler("init");
264
+ expect(fs.existsSync("./package.json")).toBe(false);
265
+ expect(fs.existsSync("../../package.json")).toBe(true);
266
+
267
+ const packageJson = JSON.parse(
268
+ fs.readFileSync("../../package.json", "utf-8")
269
+ );
270
+ expect(packageJson).toMatchInlineSnapshot(`
271
+ Object {
272
+ "name": "test",
273
+ "version": "1.0.0",
274
+ }
275
+ `);
276
+ });
277
+
278
+ it("should offer to create a worker in a non-typescript project", async () => {
279
+ mockConfirm(
280
+ {
281
+ text: "Would you like to install wrangler into your package.json?",
282
+ result: false,
283
+ },
284
+ {
285
+ text: "Would you like to use TypeScript?",
286
+ result: false,
287
+ },
288
+ {
289
+ text: "Would you like to create a Worker at src/index.js?",
290
+ result: true,
291
+ }
292
+ );
293
+
294
+ fs.writeFileSync(
295
+ "./package.json",
296
+ JSON.stringify({ name: "test", version: "1.0.0" }),
297
+ "utf-8"
298
+ );
299
+
300
+ await runWrangler("init");
301
+ expect(fs.existsSync("./src/index.js")).toBe(true);
302
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
303
+ });
304
+
305
+ it("should offer to create a worker in a typescript project", async () => {
306
+ mockConfirm(
307
+ {
308
+ text: "Would you like to install wrangler into your package.json?",
309
+ result: false,
310
+ },
311
+ {
312
+ text: "Would you like to use TypeScript?",
313
+ result: true,
314
+ },
315
+ {
316
+ text: "Would you like to create a Worker at src/index.ts?",
317
+ result: true,
318
+ }
319
+ );
320
+
321
+ fs.writeFileSync(
322
+ "./package.json",
323
+ JSON.stringify({ name: "test", version: "1.0.0" }),
324
+ "utf-8"
325
+ );
112
326
 
113
- await w('kv:namespace create "UnitTestNamespace"');
114
- expect(
115
- KVNamespaces.find((ns) => ns.title === `worker-UnitTestNamespace`)
116
- ).toBeTruthy();
327
+ await runWrangler("init");
328
+ expect(fs.existsSync("./src/index.js")).toBe(false);
329
+ expect(fs.existsSync("./src/index.ts")).toBe(true);
117
330
  });
118
331
 
119
- let createdNamespace: { id: string; title: string };
120
- it("can list namespaces", async () => {
121
- setMock(
122
- "/accounts/:accountId/storage/kv/namespaces\\?:qs",
123
- (uri, init) => {
124
- expect(init).toBe(undefined);
125
- return KVNamespaces;
332
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
333
+ mockConfirm(
334
+ {
335
+ text: "Would you like to install wrangler into your package.json?",
336
+ result: false,
337
+ },
338
+ {
339
+ text: "Would you like to use TypeScript?",
340
+ result: false,
126
341
  }
127
342
  );
128
- const { stdout } = await w("kv:namespace list", { tap: true });
129
- const namespaces = JSON.parse(stdout);
130
- createdNamespace = namespaces.find(
131
- (ns) => ns.title === "worker-UnitTestNamespace"
343
+
344
+ fs.writeFileSync(
345
+ "./package.json",
346
+ JSON.stringify({ name: "test", version: "1.0.0" }),
347
+ "utf-8"
132
348
  );
133
- expect(createdNamespace.title).toBe("worker-UnitTestNamespace");
349
+ fs.mkdirSync("./src", { recursive: true });
350
+ const PLACEHOLDER = "/* placeholder text */";
351
+ fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
352
+
353
+ await runWrangler("init");
354
+ expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
355
+ expect(fs.existsSync("./src/index.ts")).toBe(false);
134
356
  });
135
357
 
136
- it("can delete a namespace", async () => {
137
- const namespaceIdToDelete = createdNamespace.id;
138
- setMock(
139
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId",
140
- (uri, init) => {
141
- expect(init.method).toBe("DELETE");
142
- KVNamespaces = KVNamespaces.filter(
143
- (ns) => ns.id !== namespaceIdToDelete
144
- );
358
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
359
+ mockConfirm(
360
+ {
361
+ text: "Would you like to install wrangler into your package.json?",
362
+ result: false,
363
+ },
364
+ {
365
+ text: "Would you like to use TypeScript?",
366
+ result: true,
145
367
  }
146
368
  );
147
- await w(`kv:namespace delete --namespace-id ${namespaceIdToDelete}`);
148
- expect(KVNamespaces.find((ns) => ns.id === namespaceIdToDelete)).toBe(
149
- undefined
369
+
370
+ fs.writeFileSync(
371
+ "./package.json",
372
+ JSON.stringify({ name: "test", version: "1.0.0" }),
373
+ "utf-8"
374
+ );
375
+ fs.mkdirSync("./src", { recursive: true });
376
+ const PLACEHOLDER = "/* placeholder text */";
377
+ fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
378
+
379
+ await runWrangler("init");
380
+ expect(fs.existsSync("./src/index.js")).toBe(false);
381
+ expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
382
+ });
383
+
384
+ it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
385
+ mockConfirm(
386
+ {
387
+ text: "No package.json found. Would you like to create one?",
388
+ result: true,
389
+ },
390
+ {
391
+ text: "Would you like to use TypeScript?",
392
+ result: true,
393
+ },
394
+ {
395
+ text: "Would you like to create a Worker at src/index.ts?",
396
+ result: false,
397
+ }
150
398
  );
399
+ await runWrangler("init");
400
+ expect(fs.existsSync("./tsconfig.json")).toBe(true);
401
+ const tsconfigJson = JSON.parse(
402
+ fs.readFileSync("./tsconfig.json", "utf-8")
403
+ );
404
+ expect(tsconfigJson.compilerOptions.types).toEqual([
405
+ "@cloudflare/workers-types",
406
+ ]);
407
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
408
+ "@cloudflare/workers-types",
409
+ "typescript"
410
+ );
411
+ });
412
+
413
+ it("should not touch an existing tsconfig.json in the same directory", async () => {
414
+ fs.writeFileSync(
415
+ "./package.json",
416
+ JSON.stringify({
417
+ name: "test",
418
+ version: "1.0.0",
419
+ devDependencies: {
420
+ wrangler: "0.0.0",
421
+ "@cloudflare/workers-types": "0.0.0",
422
+ },
423
+ }),
424
+ "utf-8"
425
+ );
426
+ fs.writeFileSync(
427
+ "./tsconfig.json",
428
+ JSON.stringify({ compilerOptions: {} }),
429
+ "utf-8"
430
+ );
431
+
432
+ await runWrangler("init");
433
+ const tsconfigJson = JSON.parse(
434
+ fs.readFileSync("./tsconfig.json", "utf-8")
435
+ );
436
+ expect(tsconfigJson.compilerOptions).toEqual({});
437
+ });
438
+
439
+ it("should offer to install type definitions in an existing typescript project", async () => {
440
+ mockConfirm(
441
+ {
442
+ text: "Would you like to install wrangler into your package.json?",
443
+ result: false,
444
+ },
445
+ {
446
+ text: "Would you like to install the type definitions for Workers into your package.json?",
447
+ result: true,
448
+ },
449
+ {
450
+ text: "Would you like to create a Worker at src/index.ts?",
451
+ result: false,
452
+ }
453
+ );
454
+ fs.writeFileSync(
455
+ "./package.json",
456
+ JSON.stringify({
457
+ name: "test",
458
+ version: "1.0.0",
459
+ }),
460
+ "utf-8"
461
+ );
462
+ fs.writeFileSync(
463
+ "./tsconfig.json",
464
+ JSON.stringify({ compilerOptions: {} }),
465
+ "utf-8"
466
+ );
467
+
468
+ await runWrangler("init");
469
+ const tsconfigJson = JSON.parse(
470
+ fs.readFileSync("./tsconfig.json", "utf-8")
471
+ );
472
+ // unchanged tsconfig
473
+ expect(tsconfigJson.compilerOptions).toEqual({});
474
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
475
+ "@cloudflare/workers-types"
476
+ );
477
+ });
478
+
479
+ it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
480
+ fs.writeFileSync(
481
+ "./package.json",
482
+ JSON.stringify({
483
+ name: "test",
484
+ version: "1.0.0",
485
+ devDependencies: {
486
+ wrangler: "0.0.0",
487
+ "@cloudflare/workers-types": "0.0.0",
488
+ },
489
+ }),
490
+ "utf-8"
491
+ );
492
+ fs.writeFileSync(
493
+ "./tsconfig.json",
494
+ JSON.stringify({ compilerOptions: {} }),
495
+ "utf-8"
496
+ );
497
+
498
+ fs.mkdirSync("./sub-1/sub-2", { recursive: true });
499
+ process.chdir("./sub-1/sub-2");
500
+
501
+ await runWrangler("init");
502
+ expect(fs.existsSync("./tsconfig.json")).toBe(false);
503
+ expect(fs.existsSync("../../tsconfig.json")).toBe(true);
504
+
505
+ const tsconfigJson = JSON.parse(
506
+ fs.readFileSync("../../tsconfig.json", "utf-8")
507
+ );
508
+ expect(tsconfigJson.compilerOptions).toEqual({});
509
+ });
510
+
511
+ it("should error if `--type` is used", async () => {
512
+ await expect(
513
+ runWrangler("init --type")
514
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
515
+ `"The --type option is no longer supported."`
516
+ );
517
+ });
518
+
519
+ it("should error if `--type javascript` is used", async () => {
520
+ await expect(
521
+ runWrangler("init --type javascript")
522
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
523
+ `"The --type option is no longer supported."`
524
+ );
525
+ });
526
+
527
+ it("should error if `--type rust` is used", async () => {
528
+ await expect(
529
+ runWrangler("init --type rust")
530
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
531
+ `"The --type option is no longer supported."`
532
+ );
533
+ });
534
+
535
+ it("should error if `--type webpack` is used", async () => {
536
+ await expect(runWrangler("init --type webpack")).rejects
537
+ .toThrowErrorMatchingInlineSnapshot(`
538
+ "The --type option is no longer supported.
539
+ If you wish to use webpack then you will need to create a custom build."
540
+ `);
151
541
  });
152
542
  });
153
543
  });