wrangler 0.0.0-ddde8d4 → 0.0.0-e043479

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 (46) hide show
  1. package/package.json +7 -5
  2. package/src/__tests__/configuration.test.ts +2186 -0
  3. package/src/__tests__/dev.test.tsx +218 -75
  4. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  5. package/src/__tests__/helpers/mock-account-id.ts +18 -4
  6. package/src/__tests__/helpers/mock-console.ts +17 -9
  7. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  8. package/src/__tests__/helpers/write-wrangler-toml.ts +2 -5
  9. package/src/__tests__/index.test.ts +125 -49
  10. package/src/__tests__/jest.setup.ts +16 -4
  11. package/src/__tests__/kv.test.ts +399 -132
  12. package/src/__tests__/parse.test.ts +322 -0
  13. package/src/__tests__/publish.test.ts +791 -382
  14. package/src/__tests__/r2.test.ts +16 -24
  15. package/src/__tests__/secret.test.ts +299 -84
  16. package/src/__tests__/tail.test.ts +309 -176
  17. package/src/api/form_data.ts +43 -7
  18. package/src/api/preview.ts +36 -17
  19. package/src/api/worker.ts +18 -6
  20. package/src/bundle.ts +21 -15
  21. package/src/cfetch/index.ts +7 -5
  22. package/src/cfetch/internal.ts +24 -7
  23. package/src/config/README.md +107 -0
  24. package/src/config/config.ts +270 -0
  25. package/src/config/diagnostics.ts +80 -0
  26. package/src/config/environment.ts +305 -0
  27. package/src/config/index.ts +52 -0
  28. package/src/config/validation-helpers.ts +370 -0
  29. package/src/config/validation.ts +1019 -0
  30. package/src/dev.tsx +123 -49
  31. package/src/entry.ts +57 -0
  32. package/src/guess-worker-format.ts +1 -1
  33. package/src/index.tsx +631 -869
  34. package/src/kv.tsx +101 -44
  35. package/src/module-collection.ts +133 -47
  36. package/src/package-manager.ts +21 -15
  37. package/src/parse.ts +183 -0
  38. package/src/proxy.ts +18 -10
  39. package/src/publish.ts +70 -65
  40. package/src/sites.tsx +5 -11
  41. package/src/tail/index.ts +17 -7
  42. package/src/user.tsx +19 -5
  43. package/templates/tsconfig.json +105 -0
  44. package/wrangler-dist/cli.js +6177 -2424
  45. package/wrangler-dist/cli.js.map +3 -3
  46. package/src/config.ts +0 -671
@@ -1,6 +1,7 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as fsp from "node:fs/promises";
3
3
  import * as TOML from "@iarna/toml";
4
+ import { parseConfigFileTextToJson } from "typescript";
4
5
  import { version as wranglerVersion } from "../../package.json";
5
6
  import { getPackageManager } from "../package-manager";
6
7
  import { mockConsoleMethods } from "./helpers/mock-console";
@@ -15,6 +16,9 @@ describe("wrangler", () => {
15
16
 
16
17
  beforeEach(() => {
17
18
  mockPackageManager = {
19
+ cwd: process.cwd(),
20
+ // @ts-expect-error we're making a fake package manager here
21
+ type: "mockpm",
18
22
  addDevDeps: jest.fn(),
19
23
  install: jest.fn(),
20
24
  };
@@ -44,12 +48,10 @@ describe("wrangler", () => {
44
48
  wrangler r2 📦 Interact with an R2 store
45
49
 
46
50
  Flags:
47
- -c, --config Path to .toml configuration file [string]
48
- -h, --help Show help [boolean]
49
- -v, --version Show version number [boolean]
50
-
51
- Options:
52
- -l, --local Run on my machine [boolean] [default: false]"
51
+ -c, --config Path to .toml configuration file [string]
52
+ -h, --help Show help [boolean]
53
+ -v, --version Show version number [boolean]
54
+ --legacy-env Use legacy environments [boolean]"
53
55
  `);
54
56
 
55
57
  expect(std.err).toMatchInlineSnapshot(`""`);
@@ -82,12 +84,10 @@ describe("wrangler", () => {
82
84
  wrangler r2 📦 Interact with an R2 store
83
85
 
84
86
  Flags:
85
- -c, --config Path to .toml configuration file [string]
86
- -h, --help Show help [boolean]
87
- -v, --version Show version number [boolean]
88
-
89
- Options:
90
- -l, --local Run on my machine [boolean] [default: false]
87
+ -c, --config Path to .toml configuration file [string]
88
+ -h, --help Show help [boolean]
89
+ -v, --version Show version number [boolean]
90
+ --legacy-env Use legacy environments [boolean]
91
91
 
92
92
  Unknown command: invalid-command."
93
93
  `);
@@ -108,17 +108,19 @@ describe("wrangler", () => {
108
108
  expect(fs.existsSync("./tsconfig.json")).toBe(false);
109
109
  });
110
110
 
111
- it("should create a named Worker wrangler.toml", async () => {
111
+ it("should create a wrangler.toml and a directory for a named Worker ", async () => {
112
112
  mockConfirm({
113
113
  text: "No package.json found. Would you like to create one?",
114
114
  result: false,
115
115
  });
116
116
  await runWrangler("init my-worker");
117
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
117
+ const parsed = TOML.parse(
118
+ await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
119
+ );
118
120
  expect(typeof parsed.compatibility_date).toBe("string");
119
121
  expect(parsed.name).toBe("my-worker");
120
- expect(fs.existsSync("./package.json")).toBe(false);
121
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
122
+ expect(fs.existsSync("./my-worker/package.json")).toBe(false);
123
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
122
124
  });
123
125
 
124
126
  it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
@@ -137,6 +139,37 @@ describe("wrangler", () => {
137
139
  expect(parsed.compatibility_date).toBe("something-else");
138
140
  });
139
141
 
142
+ it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
143
+ fs.writeFileSync(
144
+ "./wrangler.toml",
145
+ 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
146
+ "utf-8"
147
+ );
148
+ mockConfirm(
149
+ {
150
+ text: "Do you want to continue initializing this project?",
151
+ result: true,
152
+ },
153
+ {
154
+ text: "No package.json found. Would you like to create one?",
155
+ result: true,
156
+ },
157
+ {
158
+ text: "Would you like to use TypeScript?",
159
+ result: true,
160
+ },
161
+ {
162
+ text: "Would you like to create a Worker at src/index.ts?",
163
+ result: true,
164
+ }
165
+ );
166
+
167
+ await runWrangler("init");
168
+ expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
169
+ `"compatibility_date=\\"something-else\\""`
170
+ );
171
+ });
172
+
140
173
  it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
141
174
  fs.writeFileSync(
142
175
  "./wrangler.toml",
@@ -205,7 +238,7 @@ describe("wrangler", () => {
205
238
  );
206
239
  await runWrangler("init my-worker");
207
240
  const packageJson = JSON.parse(
208
- fs.readFileSync("./package.json", "utf-8")
241
+ fs.readFileSync("./my-worker/package.json", "utf-8")
209
242
  );
210
243
  expect(packageJson.name).toBe("my-worker");
211
244
  });
@@ -396,18 +429,19 @@ describe("wrangler", () => {
396
429
  expect(fs.existsSync("./src/index.js")).toBe(false);
397
430
  expect(fs.existsSync("./src/index.ts")).toBe(true);
398
431
 
399
- expect(packageJson.scripts.start).toBe("wrangler dev src/index.ts");
400
- expect(packageJson.scripts.publish).toBe("wrangler publish src/index.ts");
432
+ expect(packageJson.scripts.start).toBe("wrangler dev");
433
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
401
434
  expect(packageJson.name).toContain("wrangler-tests");
402
435
  expect(packageJson.version).toEqual("0.0.0");
403
436
  expect(std.out).toMatchInlineSnapshot(`
404
- "✨ Successfully created wrangler.toml
405
- ✨ Created package.json
406
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
407
- To start developing on your worker, run npm start.
408
- To publish your worker on to the internet, run npm run publish.
409
- ✨ Created src/index.ts"
410
- `);
437
+ "✨ Successfully created wrangler.toml
438
+ ✨ Created package.json
439
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
440
+ ✨ Created src/index.ts
441
+
442
+ To start developing your Worker, run \`npm start\`
443
+ To publish your Worker to the Internet, run \`npm run publish\`"
444
+ `);
411
445
  });
412
446
 
413
447
  it("should not overwrite package.json scripts for a typescript project", async () => {
@@ -445,12 +479,13 @@ describe("wrangler", () => {
445
479
  expect(packageJson.scripts.start).toBe("test-start");
446
480
  expect(packageJson.scripts.publish).toBe("test-publish");
447
481
  expect(std.out).toMatchInlineSnapshot(`
448
- "✨ Successfully created wrangler.toml
449
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
450
- To start developing on your worker, npx wrangler dev src/index.ts
451
- To publish your worker on to the internet, npx wrangler publish src/index.ts
452
- ✨ Created src/index.ts"
453
- `);
482
+ "✨ Successfully created wrangler.toml
483
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
484
+ ✨ Created src/index.ts
485
+
486
+ To start developing your Worker, run \`npx wrangler dev\`
487
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
488
+ `);
454
489
  });
455
490
 
456
491
  it("should add missing scripts for a non-ts project with .js extension", async () => {
@@ -482,17 +517,18 @@ describe("wrangler", () => {
482
517
  expect(fs.existsSync("./src/index.js")).toBe(true);
483
518
  expect(fs.existsSync("./src/index.ts")).toBe(false);
484
519
 
485
- expect(packageJson.scripts.start).toBe("wrangler dev src/index.js");
486
- expect(packageJson.scripts.publish).toBe("wrangler publish src/index.js");
520
+ expect(packageJson.scripts.start).toBe("wrangler dev");
521
+ expect(packageJson.scripts.publish).toBe("wrangler publish");
487
522
  expect(packageJson.name).toContain("wrangler-tests");
488
523
  expect(packageJson.version).toEqual("0.0.0");
489
524
  expect(std.out).toMatchInlineSnapshot(`
490
- "✨ Successfully created wrangler.toml
491
- ✨ Created package.json
492
- To start developing on your worker, run npm start.
493
- To publish your worker on to the internet, run npm run publish.
494
- ✨ Created src/index.js"
495
- `);
525
+ "✨ Successfully created wrangler.toml
526
+ ✨ Created package.json
527
+ ✨ Created src/index.js
528
+
529
+ To start developing your Worker, run \`npm start\`
530
+ To publish your Worker to the Internet, run \`npm run publish\`"
531
+ `);
496
532
  });
497
533
 
498
534
  it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
@@ -530,11 +566,12 @@ describe("wrangler", () => {
530
566
  expect(packageJson.scripts.start).toBe("test-start");
531
567
  expect(packageJson.scripts.publish).toBe("test-publish");
532
568
  expect(std.out).toMatchInlineSnapshot(`
533
- "✨ Successfully created wrangler.toml
534
- To start developing on your worker, npx wrangler dev src/index.js
535
- To publish your worker on to the internet, npx wrangler publish src/index.js
536
- ✨ Created src/index.js"
537
- `);
569
+ "✨ Successfully created wrangler.toml
570
+ ✨ Created src/index.js
571
+
572
+ To start developing your Worker, run \`npx wrangler dev\`
573
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
574
+ `);
538
575
  });
539
576
 
540
577
  it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
@@ -606,9 +643,12 @@ describe("wrangler", () => {
606
643
  );
607
644
  await runWrangler("init");
608
645
  expect(fs.existsSync("./tsconfig.json")).toBe(true);
609
- const tsconfigJson = JSON.parse(
610
- fs.readFileSync("./tsconfig.json", "utf-8")
611
- );
646
+ const { config: tsconfigJson, error: tsConfigParseError } =
647
+ parseConfigFileTextToJson(
648
+ "./tsconfig.json",
649
+ fs.readFileSync("./tsconfig.json", "utf-8")
650
+ );
651
+ expect(tsConfigParseError).toBeUndefined();
612
652
  expect(tsconfigJson.compilerOptions.types).toEqual([
613
653
  "@cloudflare/workers-types",
614
654
  ]);
@@ -724,9 +764,45 @@ describe("wrangler", () => {
724
764
  expect(fs.existsSync("./tsconfig.json")).toBe(true);
725
765
  expect(fs.existsSync("./package.json")).toBe(true);
726
766
  expect(fs.existsSync("./wrangler.toml")).toBe(true);
767
+ expect(std.out).toMatchInlineSnapshot(`
768
+ "✨ Successfully created wrangler.toml
769
+ ✨ Created package.json
770
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
771
+ ✨ Created src/index.ts
772
+
773
+ To start developing your Worker, run \`npm start\`
774
+ To publish your Worker to the Internet, run \`npm run publish\`"
775
+ `);
776
+ expect(std.err).toMatchInlineSnapshot(`""`);
777
+ expect(std.warn).toMatchInlineSnapshot(`""`);
778
+ });
779
+
780
+ it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
781
+ await runWrangler("init my-worker --yes");
782
+
783
+ expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
784
+ expect(fs.existsSync("./my-worker/src/index.ts")).toBe(true);
785
+ expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(true);
786
+ expect(fs.existsSync("./my-worker/package.json")).toBe(true);
787
+ expect(fs.existsSync("./my-worker/wrangler.toml")).toBe(true);
788
+ const parsedWranglerToml = TOML.parse(
789
+ fs.readFileSync("./my-worker/wrangler.toml", "utf-8")
790
+ );
791
+ expect(parsedWranglerToml.main).toEqual("src/index.ts");
792
+ expect(std.out).toMatchInlineSnapshot(`
793
+ "✨ Successfully created wrangler.toml
794
+ ✨ Created package.json
795
+ ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
796
+ ✨ Created src/index.ts
797
+
798
+ To start developing your Worker, run \`cd my-worker && npm start\`
799
+ To publish your Worker to the Internet, run \`npm run publish\`"
800
+ `);
801
+ expect(std.err).toMatchInlineSnapshot(`""`);
802
+ expect(std.warn).toMatchInlineSnapshot(`""`);
727
803
  });
728
804
 
729
- it("should initialize with no interactive prompts if `--y` is used", async () => {
805
+ it("should initialize with no interactive prompts if `-y` is used", async () => {
730
806
  await runWrangler("init -y");
731
807
 
732
808
  expect(fs.existsSync("./src/index.js")).toBe(false);
@@ -43,15 +43,27 @@ jest.mock("../cfetch/internal");
43
43
  jest.mock("../dialogs");
44
44
 
45
45
  // By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
46
- (confirm as jest.Mock).mockImplementation(() => {
46
+ (confirm as jest.Mock).mockImplementation((text: string) => {
47
47
  throw new Error(
48
- "Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more."
48
+ `Unexpected call to \`confirm("${text}")\`.\nYou should use \`mockConfirm()\` to mock calls to \`confirm()\` with expectations. Search the codebase for \`mockConfirm\` to learn more.`
49
49
  );
50
50
  });
51
51
 
52
52
  // By default (if not configured by mockPrompt()) calls to `prompt()` should throw.
53
- (prompt as jest.Mock).mockImplementation(() => {
53
+ (prompt as jest.Mock).mockImplementation((text: string) => {
54
54
  throw new Error(
55
- "Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more."
55
+ `Unexpected call to \`prompt(${text}, ...)\`.\nYou should use \`mockPrompt()\` to mock calls to \`prompt()\` with expectations. Search the codebase for \`mockPrompt\` to learn more.`
56
56
  );
57
57
  });
58
+
59
+ jest.mock("../dev", () => {
60
+ const { useApp } = jest.requireActual("ink");
61
+ const { useEffect } = jest.requireActual("react");
62
+ return jest.fn().mockImplementation(() => {
63
+ const { exit } = useApp();
64
+ useEffect(() => {
65
+ exit();
66
+ });
67
+ return null;
68
+ });
69
+ });