wrangler 0.0.0-e64812e → 0.0.0-e8e684c7

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 (214) hide show
  1. package/README.md +27 -3
  2. package/bin/wrangler.js +150 -62
  3. package/kv-asset-handler.js +1 -0
  4. package/miniflare-dist/index.mjs +663 -7
  5. package/package.json +173 -152
  6. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  7. package/src/__tests__/config-cache.test.ts +30 -24
  8. package/src/__tests__/configuration.test.ts +4015 -3363
  9. package/src/__tests__/dev.test.tsx +1290 -952
  10. package/src/__tests__/generate.test.ts +91 -0
  11. package/src/__tests__/guess-worker-format.test.ts +68 -68
  12. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  13. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  14. package/src/__tests__/helpers/mock-bin.ts +20 -20
  15. package/src/__tests__/helpers/mock-cfetch.ts +179 -94
  16. package/src/__tests__/helpers/mock-console.ts +49 -39
  17. package/src/__tests__/helpers/mock-dialogs.ts +94 -71
  18. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  19. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  20. package/src/__tests__/helpers/mock-istty.ts +65 -18
  21. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  22. package/src/__tests__/helpers/mock-kv.ts +26 -26
  23. package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
  24. package/src/__tests__/helpers/mock-process.ts +39 -0
  25. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  26. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  27. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  28. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  29. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  30. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  31. package/src/__tests__/https-options.test.ts +142 -109
  32. package/src/__tests__/index.test.ts +222 -233
  33. package/src/__tests__/init.test.ts +2139 -1603
  34. package/src/__tests__/jest.setup.ts +114 -34
  35. package/src/__tests__/kv.test.ts +1222 -1032
  36. package/src/__tests__/logger.test.ts +100 -74
  37. package/src/__tests__/metrics.test.ts +450 -0
  38. package/src/__tests__/package-manager.test.ts +303 -303
  39. package/src/__tests__/pages.test.ts +1173 -652
  40. package/src/__tests__/parse.test.ts +252 -252
  41. package/src/__tests__/publish.test.ts +6788 -5301
  42. package/src/__tests__/pubsub.test.ts +367 -0
  43. package/src/__tests__/r2.test.ts +328 -225
  44. package/src/__tests__/route.test.ts +18 -18
  45. package/src/__tests__/secret.test.ts +470 -459
  46. package/src/__tests__/tail.test.ts +676 -564
  47. package/src/__tests__/test-old-node-version.js +31 -0
  48. package/src/__tests__/user.test.ts +136 -112
  49. package/src/__tests__/validate-dev-props.test.ts +56 -0
  50. package/src/__tests__/version.test.ts +35 -0
  51. package/src/__tests__/whoami.test.tsx +267 -83
  52. package/src/__tests__/worker-namespace.test.ts +329 -0
  53. package/src/abort.d.ts +1 -1
  54. package/src/api/dev.ts +93 -0
  55. package/src/api/index.ts +1 -0
  56. package/src/bundle-reporter.tsx +29 -0
  57. package/src/bundle.ts +430 -157
  58. package/src/cfetch/index.ts +80 -80
  59. package/src/cfetch/internal.ts +244 -91
  60. package/src/cli.ts +21 -7
  61. package/src/config/config.ts +224 -195
  62. package/src/config/diagnostics.ts +61 -61
  63. package/src/config/environment.ts +422 -357
  64. package/src/config/index.ts +219 -193
  65. package/src/config/validation-helpers.ts +379 -357
  66. package/src/config/validation.ts +1690 -1367
  67. package/src/config-cache.ts +81 -41
  68. package/src/create-worker-preview.ts +208 -136
  69. package/src/create-worker-upload-form.ts +275 -238
  70. package/src/dev/dev-vars.ts +13 -13
  71. package/src/dev/dev.tsx +477 -351
  72. package/src/dev/local.tsx +426 -295
  73. package/src/dev/remote.tsx +406 -211
  74. package/src/dev/use-esbuild.ts +154 -94
  75. package/src/dev/validate-dev-props.ts +31 -0
  76. package/src/dev-registry.tsx +160 -0
  77. package/src/dev.tsx +696 -0
  78. package/src/dialogs.tsx +97 -97
  79. package/src/dispatch-namespace.ts +206 -0
  80. package/src/durable.ts +87 -87
  81. package/src/entry.ts +234 -228
  82. package/src/environment-variables.ts +23 -23
  83. package/src/errors.ts +6 -6
  84. package/src/faye-websocket.d.ts +6 -0
  85. package/src/generate.ts +131 -0
  86. package/src/git-client.ts +42 -0
  87. package/src/global-wrangler-config-path.ts +26 -0
  88. package/src/https-options.ts +84 -80
  89. package/src/index.tsx +2164 -2768
  90. package/src/init.ts +622 -0
  91. package/src/inspect.ts +678 -593
  92. package/src/intl-polyfill.d.ts +123 -123
  93. package/src/is-ci.ts +14 -0
  94. package/src/is-interactive.ts +12 -0
  95. package/src/kv.ts +299 -277
  96. package/src/logger.ts +46 -39
  97. package/src/metrics/index.ts +5 -0
  98. package/src/metrics/metrics-config.ts +239 -0
  99. package/src/metrics/metrics-dispatcher.ts +96 -0
  100. package/src/metrics/metrics-usage-headers.ts +24 -0
  101. package/src/metrics/send-event.ts +92 -0
  102. package/src/miniflare-cli/assets.ts +554 -0
  103. package/src/miniflare-cli/enum-keys.ts +8 -8
  104. package/src/miniflare-cli/index.ts +198 -32
  105. package/src/miniflare-cli/request-context.ts +18 -18
  106. package/src/module-collection.ts +212 -212
  107. package/src/open-in-browser.ts +4 -6
  108. package/src/package-manager.ts +173 -128
  109. package/src/pages/build.tsx +238 -0
  110. package/src/pages/constants.ts +12 -0
  111. package/src/pages/deployments.tsx +106 -0
  112. package/src/pages/dev.tsx +528 -0
  113. package/src/pages/errors.ts +22 -0
  114. package/src/pages/functions/buildPlugin.ts +109 -0
  115. package/src/pages/functions/buildWorker.ts +155 -0
  116. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  117. package/src/pages/functions/filepath-routing.ts +189 -0
  118. package/src/pages/functions/identifiers.ts +78 -0
  119. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  120. package/src/pages/functions/routes-consolidation.ts +73 -0
  121. package/src/pages/functions/routes-transformation.test.ts +271 -0
  122. package/src/pages/functions/routes-transformation.ts +122 -0
  123. package/src/pages/functions/routes.ts +151 -0
  124. package/src/pages/functions.tsx +96 -0
  125. package/src/pages/index.tsx +94 -0
  126. package/src/pages/projects.tsx +169 -0
  127. package/src/pages/publish.tsx +391 -0
  128. package/src/pages/types.ts +49 -0
  129. package/src/pages/upload.tsx +413 -0
  130. package/src/pages/utils.ts +12 -0
  131. package/src/parse.ts +209 -125
  132. package/src/paths.ts +6 -6
  133. package/src/preview.ts +31 -0
  134. package/src/proxy.ts +410 -389
  135. package/src/publish.ts +773 -534
  136. package/src/pubsub/index.ts +286 -0
  137. package/src/pubsub/pubsub-commands.tsx +623 -0
  138. package/src/r2.ts +100 -19
  139. package/src/selfsigned.d.ts +23 -23
  140. package/src/sites.tsx +278 -201
  141. package/src/tail/filters.ts +111 -109
  142. package/src/tail/index.ts +230 -217
  143. package/src/tail/printing.ts +86 -46
  144. package/src/update-check.ts +11 -11
  145. package/src/user/choose-account.tsx +69 -0
  146. package/src/user/env-vars.ts +46 -0
  147. package/src/user/generate-auth-url.ts +33 -0
  148. package/src/user/generate-random-state.ts +16 -0
  149. package/src/user/index.ts +3 -0
  150. package/src/user/user.tsx +1191 -0
  151. package/src/whoami.tsx +141 -42
  152. package/src/worker.ts +123 -99
  153. package/src/zones.ts +167 -0
  154. package/templates/checked-fetch.js +17 -0
  155. package/templates/first-party-worker-module-facade.ts +18 -0
  156. package/templates/format-dev-errors.ts +32 -0
  157. package/templates/new-worker-scheduled.js +3 -3
  158. package/templates/new-worker-scheduled.ts +15 -15
  159. package/templates/new-worker.js +3 -3
  160. package/templates/new-worker.ts +15 -15
  161. package/templates/no-op-worker.js +10 -0
  162. package/templates/pages-shim.ts +9 -0
  163. package/templates/pages-template-plugin.ts +167 -0
  164. package/templates/pages-template-worker.ts +172 -0
  165. package/templates/serve-static-assets.ts +63 -0
  166. package/templates/service-bindings-module-facade.js +54 -0
  167. package/templates/service-bindings-sw-facade.js +42 -0
  168. package/templates/tsconfig.json +95 -95
  169. package/wrangler-dist/cli.d.ts +338 -0
  170. package/wrangler-dist/cli.js +95449 -67524
  171. package/pages/functions/buildPlugin.ts +0 -105
  172. package/pages/functions/buildWorker.ts +0 -151
  173. package/pages/functions/filepath-routing.ts +0 -189
  174. package/pages/functions/identifiers.ts +0 -78
  175. package/pages/functions/routes.ts +0 -156
  176. package/pages/functions/template-plugin.ts +0 -147
  177. package/pages/functions/template-worker.ts +0 -143
  178. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  179. package/src/pages.tsx +0 -2038
  180. package/src/user.tsx +0 -1203
  181. package/templates/static-asset-facade.js +0 -47
  182. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  183. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  184. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  185. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  186. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  187. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  188. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  189. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  190. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  191. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  192. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  193. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  194. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  195. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  196. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  197. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  198. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  199. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  200. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  201. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  202. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  203. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  204. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  205. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  206. package/vendor/wrangler-mime/LICENSE +0 -21
  207. package/vendor/wrangler-mime/Mime.js +0 -97
  208. package/vendor/wrangler-mime/README.md +0 -187
  209. package/vendor/wrangler-mime/cli.js +0 -46
  210. package/vendor/wrangler-mime/index.js +0 -4
  211. package/vendor/wrangler-mime/lite.js +0 -4
  212. package/vendor/wrangler-mime/package.json +0 -52
  213. package/vendor/wrangler-mime/types/other.js +0 -1
  214. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,149 +1,172 @@
1
1
  import * as fs from "node:fs";
2
- import * as fsp from "node:fs/promises";
3
2
  import path from "node:path";
4
3
  import * as TOML from "@iarna/toml";
5
4
  import { execa, execaSync } from "execa";
6
5
  import { parseConfigFileTextToJson } from "typescript";
7
6
  import { version as wranglerVersion } from "../../package.json";
8
7
  import { getPackageManager } from "../package-manager";
8
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
9
+ import { setMockFetchDashScript, unsetAllMocks } from "./helpers/mock-cfetch";
9
10
  import { mockConsoleMethods } from "./helpers/mock-console";
10
11
  import {
11
- mockConfirm,
12
- clearConfirmMocks,
13
- mockSelect,
14
- clearSelectMocks,
12
+ mockConfirm,
13
+ clearConfirmMocks,
14
+ mockSelect,
15
+ clearSelectMocks,
15
16
  } from "./helpers/mock-dialogs";
16
17
  import { runInTempDir } from "./helpers/run-in-tmp";
17
18
  import { runWrangler } from "./helpers/run-wrangler";
18
- import type { RawConfig } from "../config";
19
19
  import type { PackageManager } from "../package-manager";
20
20
 
21
+ /**
22
+ * An expectation matcher for the minimal generated wrangler.toml.
23
+ */
24
+ const MINIMAL_WRANGLER_TOML = {
25
+ compatibility_date: expect.any(String),
26
+ name: expect.stringContaining("wrangler-tests"),
27
+ main: "src/index.ts",
28
+ };
29
+
21
30
  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(`
31
+ let mockPackageManager: PackageManager;
32
+ runInTempDir();
33
+
34
+ beforeEach(() => {
35
+ mockPackageManager = {
36
+ cwd: process.cwd(),
37
+ // @ts-expect-error we're making a fake package manager here
38
+ type: "mockpm",
39
+ addDevDeps: jest.fn(),
40
+ install: jest.fn(),
41
+ };
42
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
43
+ });
44
+
45
+ afterEach(() => {
46
+ clearConfirmMocks();
47
+ clearSelectMocks();
48
+ });
49
+
50
+ const std = mockConsoleMethods();
51
+
52
+ describe("options", () => {
53
+ it("should initialize with no interactive prompts if `--yes` is used", async () => {
54
+ await runWrangler("init --yes");
55
+
56
+ checkFiles({
57
+ items: {
58
+ "./src/index.js": false,
59
+ "./src/index.ts": true,
60
+ "./tsconfig.json": true,
61
+ "./package.json": true,
62
+ "./wrangler.toml": true,
63
+ },
64
+ });
65
+
66
+ expect(std.out).toMatchInlineSnapshot(`
67
+ "✨ Created wrangler.toml
68
+ ✨ Initialized git repository
69
+ ✨ Created package.json
70
+ ✨ Created tsconfig.json
71
+ ✨ Created src/index.ts
72
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
73
+
74
+ To start developing your Worker, run \`npm start\`
75
+ To publish your Worker to the Internet, run \`npm run deploy\`"
76
+ `);
77
+ expect(std.err).toMatchInlineSnapshot(`""`);
78
+ expect(std.warn).toMatchInlineSnapshot(`""`);
79
+ });
80
+
81
+ it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
82
+ await runWrangler("init my-worker --yes");
83
+
84
+ checkFiles({
85
+ items: {
86
+ "./my-worker/src/index.js": false,
87
+ "./my-worker/src/index.ts": true,
88
+ "./my-worker/tsconfig.json": true,
89
+ "./my-worker/package.json": true,
90
+ "./my-worker/wrangler.toml": wranglerToml({
91
+ ...MINIMAL_WRANGLER_TOML,
92
+ name: "my-worker",
93
+ }),
94
+ },
95
+ });
96
+
97
+ expect(std.out).toMatchInlineSnapshot(`
98
+ "✨ Created my-worker/wrangler.toml
99
+ ✨ Initialized git repository at my-worker
100
+ ✨ Created my-worker/package.json
101
+ ✨ Created my-worker/tsconfig.json
102
+ ✨ Created my-worker/src/index.ts
103
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
104
+
105
+ To start developing your Worker, run \`cd my-worker && npm start\`
106
+ To publish your Worker to the Internet, run \`npm run deploy\`"
107
+ `);
108
+ expect(std.err).toMatchInlineSnapshot(`""`);
109
+ expect(std.warn).toMatchInlineSnapshot(`""`);
110
+ });
111
+
112
+ it("should initialize with no interactive prompts if `-y` is used", async () => {
113
+ await runWrangler("init -y");
114
+
115
+ checkFiles({
116
+ items: {
117
+ "./src/index.js": false,
118
+ "./src/index.ts": true,
119
+ "./tsconfig.json": true,
120
+ "./package.json": true,
121
+ "./wrangler.toml": true,
122
+ },
123
+ });
124
+
125
+ expect(std).toMatchInlineSnapshot(`
126
+ Object {
127
+ "debug": "",
128
+ "err": "",
129
+ "out": "✨ Created wrangler.toml
130
+ ✨ Initialized git repository
131
+ ✨ Created package.json
132
+ ✨ Created tsconfig.json
133
+ ✨ Created src/index.ts
134
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
135
+
136
+ To start developing your Worker, run \`npm start\`
137
+ To publish your Worker to the Internet, run \`npm run deploy\`",
138
+ "warn": "",
139
+ }
140
+ `);
141
+ });
142
+
143
+ it("should error if `--type javascript` is used", async () => {
144
+ await expect(
145
+ runWrangler("init --type javascript")
146
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
147
+ `"The --type option is no longer supported."`
148
+ );
149
+ });
150
+
151
+ it("should error if `--type rust` is used", async () => {
152
+ await expect(
153
+ runWrangler("init --type rust")
154
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
155
+ `"The --type option is no longer supported."`
156
+ );
157
+ });
158
+
159
+ it("should error if `--type webpack` is used", async () => {
160
+ await expect(runWrangler("init --type webpack")).rejects
161
+ .toThrowErrorMatchingInlineSnapshot(`
139
162
  "The --type option is no longer supported.
140
163
  If you wish to use webpack then you will need to create a custom build."
141
164
  `);
142
- });
165
+ });
143
166
 
144
- it("should error if `--site` is used", async () => {
145
- await expect(runWrangler("init --site")).rejects
146
- .toThrowErrorMatchingInlineSnapshot(`
167
+ it("should error if `--site` is used", async () => {
168
+ await expect(runWrangler("init --site")).rejects
169
+ .toThrowErrorMatchingInlineSnapshot(`
147
170
  "The --site option is no longer supported.
148
171
  If you wish to create a brand new Worker Sites project then clone the \`worker-sites-template\` starter repository:
149
172
 
@@ -155,1480 +178,1993 @@ describe("init", () => {
155
178
  Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.
156
179
  Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/."
157
180
  `);
158
- });
159
- });
160
-
161
- describe("wrangler.toml", () => {
162
- it("should create a wrangler.toml", async () => {
163
- mockConfirm(
164
- {
165
- text: "Would you like to use git to manage this Worker?",
166
- result: false,
167
- },
168
- {
169
- text: "No package.json found. Would you like to create one?",
170
- result: false,
171
- }
172
- );
173
- await runWrangler("init");
174
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
175
- expect(typeof parsed.compatibility_date).toBe("string");
176
- expect(parsed.name).toContain("wrangler-tests");
177
- expect(fs.existsSync("./package.json")).toBe(false);
178
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
179
- expect(std).toMatchInlineSnapshot(`
180
- Object {
181
- "debug": "",
182
- "err": "",
183
- "out": "✨ Created wrangler.toml",
184
- "warn": "",
185
- }
186
- `);
187
- });
188
-
189
- it("should create a wrangler.toml and a directory for a named Worker ", async () => {
190
- mockConfirm(
191
- {
192
- text: "Would you like to use git to manage this Worker?",
193
- result: false,
194
- },
195
- {
196
- text: "No package.json found. Would you like to create one?",
197
- result: false,
198
- }
199
- );
200
- await runWrangler("init my-worker");
201
- const parsed = TOML.parse(
202
- await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
203
- );
204
- expect(typeof parsed.compatibility_date).toBe("string");
205
- expect(parsed.name).toBe("my-worker");
206
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
207
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
208
-
209
- expect(std).toMatchInlineSnapshot(`
210
- Object {
211
- "debug": "",
212
- "err": "",
213
- "out": "✨ Created my-worker/wrangler.toml",
214
- "warn": "",
215
- }
216
- `);
217
- });
218
-
219
- it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
220
- fs.writeFileSync(
221
- "./wrangler.toml",
222
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
223
- "utf-8"
224
- );
225
- mockConfirm({
226
- text: "Do you want to continue initializing this project?",
227
- result: false,
228
- });
229
- await runWrangler("init");
230
- expect(std.warn).toContain("wrangler.toml already exists!");
231
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
232
- expect(parsed.compatibility_date).toBe("something-else");
233
-
234
- expect(std).toMatchInlineSnapshot(`
235
- Object {
236
- "debug": "",
237
- "err": "",
238
- "out": "",
239
- "warn": "▲ [WARNING] wrangler.toml already exists!
240
-
241
- ",
242
- }
243
- `);
244
- });
245
-
246
- it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
247
- fs.mkdirSync("path/to/worker", { recursive: true });
248
- fs.writeFileSync(
249
- "path/to/worker/wrangler.toml",
250
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
251
- "utf-8"
252
- );
253
- mockConfirm({
254
- text: "Do you want to continue initializing this project?",
255
- result: false,
256
- });
257
- await runWrangler("init path/to/worker");
258
- expect(std.warn).toContain("wrangler.toml already exists!");
259
- const parsed = TOML.parse(
260
- await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
261
- );
262
- expect(parsed.compatibility_date).toBe("something-else");
263
- expect(std).toMatchInlineSnapshot(`
264
- Object {
265
- "debug": "",
266
- "err": "",
267
- "out": "",
268
- "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
269
-
270
- ",
271
- }
272
- `);
273
- });
274
-
275
- it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
276
- fs.writeFileSync(
277
- "./wrangler.toml",
278
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
279
- "utf-8"
280
- );
281
- mockConfirm(
282
- {
283
- text: "Would you like to use git to manage this Worker?",
284
- result: true,
285
- },
286
- {
287
- text: "Do you want to continue initializing this project?",
288
- result: true,
289
- },
290
- {
291
- text: "No package.json found. Would you like to create one?",
292
- result: true,
293
- },
294
- {
295
- text: "Would you like to use TypeScript?",
296
- result: true,
297
- }
298
- );
299
-
300
- mockSelect({
301
- text: "Would you like to create a Worker at src/index.ts?",
302
- result: "fetch",
303
- });
304
-
305
- await runWrangler("init");
306
- expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
307
- `"compatibility_date=\\"something-else\\""`
308
- );
309
- });
310
-
311
- it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
312
- fs.writeFileSync(
313
- "./wrangler.toml",
314
- `compatibility_date="something-else"`,
315
- "utf-8"
316
- );
317
- mockConfirm(
318
- {
319
- text: "Do you want to continue initializing this project?",
320
- result: true,
321
- },
322
- {
323
- text: "Would you like to use git to manage this Worker?",
324
- result: false,
325
- },
326
- {
327
- text: "No package.json found. Would you like to create one?",
328
- result: false,
329
- }
330
- );
331
- await runWrangler("init");
332
- expect(std.warn).toContain("wrangler.toml already exists!");
333
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
334
- expect(parsed.compatibility_date).toBe("something-else");
335
- expect(std).toMatchInlineSnapshot(`
336
- Object {
337
- "debug": "",
338
- "err": "",
339
- "out": "",
340
- "warn": "▲ [WARNING] wrangler.toml already exists!
341
-
342
- ",
343
- }
344
- `);
345
- });
346
-
347
- it("should not add a Cron Trigger to wrangler.toml when creating a Scheduled Worker if wrangler.toml already exists", async () => {
348
- fs.writeFileSync(
349
- "./wrangler.toml",
350
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
351
- "utf-8"
352
- );
353
- mockConfirm(
354
- {
355
- text: "Would you like to use git to manage this Worker?",
356
- result: true,
357
- },
358
- {
359
- text: "Do you want to continue initializing this project?",
360
- result: true,
361
- },
362
- {
363
- text: "No package.json found. Would you like to create one?",
364
- result: true,
365
- },
366
- {
367
- text: "Would you like to use TypeScript?",
368
- result: true,
369
- }
370
- );
371
-
372
- mockSelect({
373
- text: "Would you like to create a Worker at src/index.ts?",
374
- result: "scheduled",
375
- });
376
-
377
- await runWrangler("init");
378
- expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
379
- `"compatibility_date=\\"something-else\\""`
380
- );
381
- });
382
-
383
- it("should add a Cron Trigger to wrangler.toml when creating a Scheduled Worker, but only if wrangler.toml is being created during init", async () => {
384
- mockConfirm(
385
- {
386
- text: "Would you like to use git to manage this Worker?",
387
- result: true,
388
- },
389
- {
390
- text: "No package.json found. Would you like to create one?",
391
- result: true,
392
- },
393
- {
394
- text: "Would you like to use TypeScript?",
395
- result: true,
396
- }
397
- );
398
-
399
- mockSelect({
400
- text: "Would you like to create a Worker at src/index.ts?",
401
- result: "scheduled",
402
- });
403
-
404
- await runWrangler("init");
405
- const parsed = TOML.parse(
406
- await fsp.readFile("./wrangler.toml", "utf-8")
407
- ) as RawConfig;
408
- expect(typeof parsed.compatibility_date).toBe("string");
409
- expect(parsed.name).toContain("wrangler-tests");
410
- expect(parsed?.triggers?.crons[0]).toEqual("1 * * * *");
411
- });
412
- });
413
-
414
- describe("git init", () => {
415
- it("should offer to initialize a git repository", async () => {
416
- mockConfirm(
417
- {
418
- text: "Would you like to use git to manage this Worker?",
419
- result: true,
420
- },
421
- {
422
- text: "No package.json found. Would you like to create one?",
423
- result: false,
424
- }
425
- );
426
- await runWrangler("init");
427
- expect(std).toMatchInlineSnapshot(`
428
- Object {
429
- "debug": "",
430
- "err": "",
431
- "out": "✨ Created wrangler.toml
432
- ✨ Initialized git repository",
433
- "warn": "",
434
- }
435
- `);
436
- expect(fs.lstatSync(".git").isDirectory()).toBe(true);
437
- expect(fs.lstatSync(".gitignore").isFile()).toBe(true);
438
- });
439
-
440
- it("should not offer to initialize a git repo if it's already inside one", async () => {
441
- await execa("git", ["init"]);
442
- fs.mkdirSync("some-folder");
443
- process.chdir("some-folder");
444
- await runWrangler("init -y");
445
-
446
- // Note the lack of "✨ Initialized git repository" in the log
447
- expect(std).toMatchInlineSnapshot(`
448
- Object {
449
- "debug": "",
450
- "err": "",
451
- "out": "✨ Created wrangler.toml
452
- ✨ Created package.json
453
- ✨ Created tsconfig.json
454
- ✨ Created src/index.ts
455
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
456
-
457
- To start developing your Worker, run \`npm start\`
458
- To publish your Worker to the Internet, run \`npm run deploy\`",
459
- "warn": "",
460
- }
461
- `);
462
- });
463
-
464
- it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
465
- fs.mkdirSync("path/to/worker", { recursive: true });
466
- await execa("git", ["init"], { cwd: "path/to/worker" });
467
- expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
468
-
469
- await runWrangler("init path/to/worker/my-worker -y");
470
-
471
- // Note the lack of "✨ Initialized git repository" in the log
472
- expect(std).toMatchInlineSnapshot(`
473
- Object {
474
- "debug": "",
475
- "err": "",
476
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
477
- ✨ Created path/to/worker/my-worker/package.json
478
- ✨ Created path/to/worker/my-worker/tsconfig.json
479
- ✨ Created path/to/worker/my-worker/src/index.ts
480
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
481
-
482
- To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
483
- To publish your Worker to the Internet, run \`npm run deploy\`",
484
- "warn": "",
485
- }
486
- `);
487
- });
488
-
489
- // I... don't know how to test this lol
490
- it.todo(
491
- "should not offer to initialize a git repo if git is not installed"
492
- );
493
-
494
- it("should initialize git repo with `main` default branch", async () => {
495
- mockConfirm(
496
- {
497
- text: "Would you like to use git to manage this Worker?",
498
- result: true,
499
- },
500
- {
501
- text: "No package.json found. Would you like to create one?",
502
- result: false,
503
- }
504
- );
505
- await runWrangler("init");
506
- expect(std).toMatchInlineSnapshot(`
507
- Object {
508
- "debug": "",
509
- "err": "",
510
- "out": "✨ Created wrangler.toml
511
- ✨ Initialized git repository",
512
- "warn": "",
513
- }
514
- `);
515
-
516
- expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
517
- "refs/heads/main"
518
- );
519
- });
520
- });
521
-
522
- describe("package.json", () => {
523
- it("should create a package.json if none is found and user confirms", async () => {
524
- mockConfirm(
525
- {
526
- text: "Would you like to use git to manage this Worker?",
527
- result: false,
528
- },
529
- {
530
- text: "No package.json found. Would you like to create one?",
531
- result: true,
532
- },
533
- {
534
- text: "Would you like to use TypeScript?",
535
- result: false,
536
- }
537
- );
538
- mockSelect({
539
- text: "Would you like to create a Worker at src/index.js?",
540
- result: "none",
541
- });
542
- await runWrangler("init");
543
- expect(fs.existsSync("./package.json")).toBe(true);
544
- const packageJson = JSON.parse(
545
- fs.readFileSync("./package.json", "utf-8")
546
- );
547
- expect(packageJson.name).toContain("wrangler-tests");
548
- expect(packageJson.version).toEqual("0.0.0");
549
- expect(packageJson.devDependencies).toEqual({
550
- wrangler: expect.any(String),
551
- });
552
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
553
- expect(mockPackageManager.install).toHaveBeenCalled();
554
- expect(std).toMatchInlineSnapshot(`
555
- Object {
556
- "debug": "",
557
- "err": "",
558
- "out": "✨ Created wrangler.toml
559
- ✨ Created package.json",
560
- "warn": "",
561
- }
562
- `);
563
- });
564
-
565
- it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
566
- mockConfirm(
567
- {
568
- text: "Would you like to use git to manage this Worker?",
569
- result: false,
570
- },
571
- {
572
- text: "No package.json found. Would you like to create one?",
573
- result: true,
574
- },
575
- {
576
- text: "Would you like to use TypeScript?",
577
- result: false,
578
- }
579
- );
580
- mockSelect({
581
- text: "Would you like to create a Worker at my-worker/src/index.js?",
582
- result: "none",
583
- });
584
- await runWrangler("init my-worker");
585
- const packageJson = JSON.parse(
586
- fs.readFileSync("./my-worker/package.json", "utf-8")
587
- );
588
- expect(packageJson.name).toBe("my-worker");
589
- expect(std).toMatchInlineSnapshot(`
590
- Object {
591
- "debug": "",
592
- "err": "",
593
- "out": "✨ Created my-worker/wrangler.toml
594
- ✨ Created my-worker/package.json",
595
- "warn": "",
596
- }
597
- `);
598
- });
599
-
600
- it("should not touch an existing package.json in the same directory", async () => {
601
- mockConfirm(
602
- {
603
- text: "Would you like to use git to manage this Worker?",
604
- result: false,
605
- },
606
- {
607
- text: "Would you like to install wrangler into package.json?",
608
- result: false,
609
- },
610
- {
611
- text: "Would you like to use TypeScript?",
612
- result: false,
613
- }
614
- );
615
- mockSelect({
616
- text: "Would you like to create a Worker at src/index.js?",
617
- result: "none",
618
- });
619
-
620
- fs.writeFileSync(
621
- "./package.json",
622
- JSON.stringify({ name: "test", version: "1.0.0" }),
623
- "utf-8"
624
- );
625
-
626
- await runWrangler("init");
627
- const packageJson = JSON.parse(
628
- fs.readFileSync("./package.json", "utf-8")
629
- );
630
- expect(packageJson.name).toEqual("test");
631
- expect(packageJson.version).toEqual("1.0.0");
632
- expect(std).toMatchInlineSnapshot(`
633
- Object {
634
- "debug": "",
635
- "err": "",
636
- "out": "✨ Created wrangler.toml",
637
- "warn": "",
638
- }
639
- `);
640
- });
641
-
642
- it("should not touch an existing package.json in a target directory", async () => {
643
- mockConfirm(
644
- {
645
- text: "Would you like to use git to manage this Worker?",
646
- result: false,
647
- },
648
- {
649
- text: "Would you like to install wrangler into path/to/worker/package.json?",
650
- result: false,
651
- },
652
- {
653
- text: "Would you like to use TypeScript?",
654
- result: false,
655
- }
656
- );
657
-
658
- mockSelect({
659
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
660
- result: "none",
661
- });
662
-
663
- fs.mkdirSync("path/to/worker", { recursive: true });
664
- fs.writeFileSync(
665
- "path/to/worker/package.json",
666
- JSON.stringify({ name: "test", version: "1.0.0" }),
667
- "utf-8"
668
- );
669
-
670
- await runWrangler("init path/to/worker/my-worker");
671
- const packageJson = JSON.parse(
672
- fs.readFileSync("path/to/worker/package.json", "utf-8")
673
- );
674
- expect(packageJson.name).toEqual("test");
675
- expect(packageJson.version).toEqual("1.0.0");
676
- expect(std).toMatchInlineSnapshot(`
677
- Object {
678
- "debug": "",
679
- "err": "",
680
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml",
681
- "warn": "",
682
- }
683
- `);
684
- });
685
-
686
- it("should offer to install wrangler into an existing package.json", async () => {
687
- mockConfirm(
688
- {
689
- text: "Would you like to use git to manage this Worker?",
690
- result: false,
691
- },
692
- {
693
- text: "Would you like to install wrangler into package.json?",
694
- result: true,
695
- },
696
- {
697
- text: "Would you like to use TypeScript?",
698
- result: false,
699
- }
700
- );
701
-
702
- mockSelect({
703
- text: "Would you like to create a Worker at src/index.js?",
704
- result: "none",
705
- });
706
-
707
- fs.writeFileSync(
708
- "./package.json",
709
- JSON.stringify({ name: "test", version: "1.0.0" }),
710
- "utf-8"
711
- );
712
-
713
- await runWrangler("init");
714
- const packageJson = JSON.parse(
715
- fs.readFileSync("./package.json", "utf-8")
716
- );
717
- expect(packageJson.name).toEqual("test");
718
- expect(packageJson.version).toEqual("1.0.0");
719
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
720
- `wrangler@${wranglerVersion}`
721
- );
722
- expect(std).toMatchInlineSnapshot(`
723
- Object {
724
- "debug": "",
725
- "err": "",
726
- "out": "✨ Created wrangler.toml
727
- ✨ Installed wrangler into devDependencies",
728
- "warn": "",
729
- }
730
- `);
731
- });
732
-
733
- it("should offer to install wrangler into a package.json relative to the target directory", async () => {
734
- mockConfirm(
735
- {
736
- text: "Would you like to use git to manage this Worker?",
737
- result: false,
738
- },
739
- {
740
- text: "Would you like to install wrangler into path/to/worker/package.json?",
741
- result: true,
742
- },
743
- {
744
- text: "Would you like to use TypeScript?",
745
- result: false,
746
- }
747
- );
748
-
749
- mockSelect({
750
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
751
- result: "none",
752
- });
753
-
754
- fs.mkdirSync("path/to/worker", { recursive: true });
755
- fs.writeFileSync(
756
- "path/to/worker/package.json",
757
- JSON.stringify({ name: "test", version: "1.0.0" }),
758
- "utf-8"
759
- );
760
-
761
- await runWrangler("init path/to/worker/my-worker");
762
- const packageJson = JSON.parse(
763
- fs.readFileSync("path/to/worker/package.json", "utf-8")
764
- );
765
- expect(packageJson.name).toEqual("test");
766
- expect(packageJson.version).toEqual("1.0.0");
767
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
768
- `wrangler@${wranglerVersion}`
769
- );
770
- expect(mockPackageManager.cwd).toBe(process.cwd());
771
- expect(std).toMatchInlineSnapshot(`
772
- Object {
773
- "debug": "",
774
- "err": "",
775
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
776
- ✨ Installed wrangler into devDependencies",
777
- "warn": "",
778
- }
779
- `);
780
- });
781
-
782
- it("should not touch an existing package.json in an ancestor directory", async () => {
783
- mockConfirm(
784
- {
785
- text: "Would you like to use git to manage this Worker?",
786
- result: false,
787
- },
788
- {
789
- text: "Would you like to install wrangler into ../../package.json?",
790
- result: false,
791
- },
792
- {
793
- text: "Would you like to use TypeScript?",
794
- result: false,
795
- }
796
- );
797
-
798
- mockSelect({
799
- text: "Would you like to create a Worker at src/index.js?",
800
- result: "none",
801
- });
802
-
803
- fs.writeFileSync(
804
- "./package.json",
805
- JSON.stringify({ name: "test", version: "1.0.0" }),
806
- "utf-8"
807
- );
808
-
809
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
810
- process.chdir("./sub-1/sub-2");
811
-
812
- await runWrangler("init");
813
- expect(fs.existsSync("./package.json")).toBe(false);
814
- expect(fs.existsSync("../../package.json")).toBe(true);
815
-
816
- const packageJson = JSON.parse(
817
- fs.readFileSync("../../package.json", "utf-8")
818
- );
819
- expect(packageJson).toMatchInlineSnapshot(`
820
- Object {
821
- "name": "test",
822
- "version": "1.0.0",
823
- }
824
- `);
825
- expect(std).toMatchInlineSnapshot(`
826
- Object {
827
- "debug": "",
828
- "err": "",
829
- "out": "✨ Created wrangler.toml",
830
- "warn": "",
831
- }
832
- `);
833
- });
834
- });
835
-
836
- describe("typescript", () => {
837
- it("should offer to create a worker in a non-typescript project", async () => {
838
- mockConfirm(
839
- {
840
- text: "Would you like to use git to manage this Worker?",
841
- result: false,
842
- },
843
- {
844
- text: "Would you like to install wrangler into package.json?",
845
- result: false,
846
- },
847
- {
848
- text: "Would you like to use TypeScript?",
849
- result: false,
850
- }
851
- );
852
-
853
- mockSelect({
854
- text: "Would you like to create a Worker at src/index.js?",
855
- result: "fetch",
856
- });
857
-
858
- fs.writeFileSync(
859
- "./package.json",
860
- JSON.stringify({ name: "test", version: "1.0.0" }),
861
- "utf-8"
862
- );
863
-
864
- await runWrangler("init");
865
- expect(fs.existsSync("./src/index.js")).toBe(true);
866
- expect(fs.existsSync("./src/index.ts")).toBe(false);
867
- expect(std).toMatchInlineSnapshot(`
868
- Object {
869
- "debug": "",
870
- "err": "",
871
- "out": "✨ Created wrangler.toml
872
- ✨ Created src/index.js
873
-
874
- To start developing your Worker, run \`npx wrangler dev\`
875
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
876
- "warn": "",
877
- }
878
- `);
879
- });
880
-
881
- it("should offer to create a worker in a typescript project", async () => {
882
- mockConfirm(
883
- {
884
- text: "Would you like to use git to manage this Worker?",
885
- result: false,
886
- },
887
- {
888
- text: "Would you like to install wrangler into package.json?",
889
- result: false,
890
- },
891
- {
892
- text: "Would you like to use TypeScript?",
893
- result: true,
894
- }
895
- );
896
-
897
- mockSelect({
898
- text: "Would you like to create a Worker at src/index.ts?",
899
- result: "fetch",
900
- });
901
-
902
- fs.writeFileSync(
903
- "./package.json",
904
- JSON.stringify({ name: "test", version: "1.0.0" }),
905
- "utf-8"
906
- );
907
-
908
- await runWrangler("init");
909
- expect(fs.existsSync("./src/index.js")).toBe(false);
910
- expect(fs.existsSync("./src/index.ts")).toBe(true);
911
- expect(std).toMatchInlineSnapshot(`
912
- Object {
913
- "debug": "",
914
- "err": "",
915
- "out": "✨ Created wrangler.toml
916
- ✨ Created tsconfig.json
917
- ✨ Created src/index.ts
918
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
919
-
920
- To start developing your Worker, run \`npx wrangler dev\`
921
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
922
- "warn": "",
923
- }
924
- `);
925
- });
926
-
927
- it("should add scripts for a typescript project with .ts extension", async () => {
928
- mockConfirm(
929
- {
930
- text: "Would you like to use git to manage this Worker?",
931
- result: false,
932
- },
933
- {
934
- text: "No package.json found. Would you like to create one?",
935
- result: true,
936
- },
937
- {
938
- text: "Would you like to install wrangler into package.json?",
939
- result: false,
940
- },
941
- {
942
- text: "Would you like to use TypeScript?",
943
- result: true,
944
- }
945
- );
946
- mockSelect({
947
- text: "Would you like to create a Worker at src/index.ts?",
948
- result: "fetch",
949
- });
950
- await runWrangler("init");
951
-
952
- expect(fs.existsSync("./package.json")).toBe(true);
953
- const packageJson = JSON.parse(
954
- fs.readFileSync("./package.json", "utf-8")
955
- );
956
-
957
- expect(fs.existsSync("./src/index.js")).toBe(false);
958
- expect(fs.existsSync("./src/index.ts")).toBe(true);
959
-
960
- expect(packageJson.scripts.start).toBe("wrangler dev");
961
- expect(packageJson.scripts.deploy).toBe("wrangler publish");
962
- expect(packageJson.name).toContain("wrangler-tests");
963
- expect(packageJson.version).toEqual("0.0.0");
964
- expect(std.out).toMatchInlineSnapshot(`
965
- "✨ Created wrangler.toml
966
- ✨ Created package.json
967
- ✨ Created tsconfig.json
968
- ✨ Created src/index.ts
969
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
970
-
971
- To start developing your Worker, run \`npm start\`
972
- To publish your Worker to the Internet, run \`npm run deploy\`"
973
- `);
974
- });
975
-
976
- it("should not overwrite package.json scripts for a typescript project", async () => {
977
- mockConfirm(
978
- {
979
- text: "Would you like to use git to manage this Worker?",
980
- result: false,
981
- },
982
- {
983
- text: "Would you like to install wrangler into package.json?",
984
- result: false,
985
- },
986
- {
987
- text: "Would you like to use TypeScript?",
988
- result: true,
989
- }
990
- );
991
- mockSelect({
992
- text: "Would you like to create a Worker at src/index.ts?",
993
- result: "fetch",
994
- });
995
- await fsp.writeFile(
996
- "./package.json",
997
- JSON.stringify({
998
- scripts: {
999
- start: "test-start",
1000
- deploy: "test-publish",
1001
- },
1002
- })
1003
- );
1004
- const packageJson = JSON.parse(
1005
- fs.readFileSync("./package.json", "utf-8")
1006
- );
1007
- await runWrangler("init");
1008
-
1009
- expect(fs.existsSync("./src/index.js")).toBe(false);
1010
- expect(fs.existsSync("./src/index.ts")).toBe(true);
1011
-
1012
- expect(packageJson.scripts.start).toBe("test-start");
1013
- expect(packageJson.scripts.deploy).toBe("test-publish");
1014
- expect(std.out).toMatchInlineSnapshot(`
1015
- "✨ Created wrangler.toml
1016
- ✨ Created tsconfig.json
1017
- ✨ Created src/index.ts
1018
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1019
-
1020
- To start developing your Worker, run \`npx wrangler dev\`
1021
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
1022
- `);
1023
- });
1024
-
1025
- it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
1026
- mockConfirm(
1027
- {
1028
- text: "Would you like to use git to manage this Worker?",
1029
- result: false,
1030
- },
1031
- {
1032
- text: "Would you like to install wrangler into package.json?",
1033
- result: false,
1034
- },
1035
- {
1036
- text: "Would you like to use TypeScript?",
1037
- result: true,
1038
- }
1039
- );
1040
-
1041
- fs.writeFileSync(
1042
- "./package.json",
1043
- JSON.stringify({ name: "test", version: "1.0.0" }),
1044
- "utf-8"
1045
- );
1046
- fs.mkdirSync("./src", { recursive: true });
1047
- const PLACEHOLDER = "/* placeholder text */";
1048
- fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
1049
-
1050
- await runWrangler("init");
1051
- expect(fs.existsSync("./src/index.js")).toBe(false);
1052
- expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
1053
- expect(std).toMatchInlineSnapshot(`
1054
- Object {
1055
- "debug": "",
1056
- "err": "",
1057
- "out": "✨ Created wrangler.toml
1058
- ✨ Created tsconfig.json
1059
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1060
- "warn": "",
1061
- }
1062
- `);
1063
- });
1064
-
1065
- it("should not offer to create a worker in a ts project for a named worker if a file already exists at the location", async () => {
1066
- mockConfirm(
1067
- {
1068
- text: "Would you like to use git to manage this Worker?",
1069
- result: false,
1070
- },
1071
- {
1072
- text: "Would you like to install wrangler into package.json?",
1073
- result: false,
1074
- },
1075
- {
1076
- text: "Would you like to use TypeScript?",
1077
- result: true,
1078
- }
1079
- );
1080
-
1081
- fs.writeFileSync(
1082
- "./package.json",
1083
- JSON.stringify({ name: "test", version: "1.0.0" }),
1084
- "utf-8"
1085
- );
1086
- fs.mkdirSync("./my-worker/src", { recursive: true });
1087
- const PLACEHOLDER = "/* placeholder text */";
1088
- fs.writeFileSync("./my-worker/src/index.ts", PLACEHOLDER, "utf-8");
1089
-
1090
- await runWrangler("init my-worker");
1091
- expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
1092
- expect(fs.readFileSync("./my-worker/src/index.ts", "utf-8")).toBe(
1093
- PLACEHOLDER
1094
- );
1095
- expect(std).toMatchInlineSnapshot(`
1096
- Object {
1097
- "debug": "",
1098
- "err": "",
1099
- "out": "✨ Created my-worker/wrangler.toml
1100
- ✨ Created my-worker/tsconfig.json
1101
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1102
- "warn": "",
1103
- }
1104
- `);
1105
- });
1106
-
1107
- it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
1108
- mockConfirm(
1109
- {
1110
- text: "Would you like to use git to manage this Worker?",
1111
- result: false,
1112
- },
1113
- {
1114
- text: "No package.json found. Would you like to create one?",
1115
- result: true,
1116
- },
1117
- {
1118
- text: "Would you like to use TypeScript?",
1119
- result: true,
1120
- }
1121
- );
1122
- mockSelect({
1123
- text: "Would you like to create a Worker at src/index.ts?",
1124
- result: "none",
1125
- });
1126
- await runWrangler("init");
1127
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
1128
- const { config: tsconfigJson, error: tsConfigParseError } =
1129
- parseConfigFileTextToJson(
1130
- "./tsconfig.json",
1131
- fs.readFileSync("./tsconfig.json", "utf-8")
1132
- );
1133
- expect(tsConfigParseError).toBeUndefined();
1134
- expect(tsconfigJson.compilerOptions.types).toEqual([
1135
- "@cloudflare/workers-types",
1136
- ]);
1137
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1138
- "@cloudflare/workers-types",
1139
- "typescript"
1140
- );
1141
- expect(std).toMatchInlineSnapshot(`
1142
- Object {
1143
- "debug": "",
1144
- "err": "",
1145
- "out": "✨ Created wrangler.toml
1146
- ✨ Created package.json
1147
- ✨ Created tsconfig.json
1148
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1149
- "warn": "",
1150
- }
1151
- `);
1152
- });
1153
-
1154
- it("should not touch an existing tsconfig.json in the same directory", async () => {
1155
- fs.writeFileSync(
1156
- "./package.json",
1157
- JSON.stringify({
1158
- name: "test",
1159
- version: "1.0.0",
1160
- devDependencies: {
1161
- wrangler: "0.0.0",
1162
- "@cloudflare/workers-types": "0.0.0",
1163
- },
1164
- }),
1165
- "utf-8"
1166
- );
1167
- fs.writeFileSync(
1168
- "./tsconfig.json",
1169
- JSON.stringify({ compilerOptions: {} }),
1170
- "utf-8"
1171
- );
1172
-
1173
- mockConfirm({
1174
- text: "Would you like to use git to manage this Worker?",
1175
- result: false,
1176
- });
1177
-
1178
- mockSelect({
1179
- text: "Would you like to create a Worker at src/index.ts?",
1180
- result: "fetch",
1181
- });
1182
-
1183
- await runWrangler("init");
1184
- const tsconfigJson = JSON.parse(
1185
- fs.readFileSync("./tsconfig.json", "utf-8")
1186
- );
1187
- expect(tsconfigJson.compilerOptions).toEqual({});
1188
- expect(std).toMatchInlineSnapshot(`
1189
- Object {
1190
- "debug": "",
1191
- "err": "",
1192
- "out": "✨ Created wrangler.toml
1193
- ✨ Created src/index.ts
1194
-
1195
- To start developing your Worker, run \`npx wrangler dev\`
1196
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1197
- "warn": "",
1198
- }
1199
- `);
1200
- });
1201
-
1202
- it("should not touch an existing tsconfig.json in the ancestor of a target directory", async () => {
1203
- fs.mkdirSync("path/to/worker", { recursive: true });
1204
- fs.writeFileSync(
1205
- "path/to/worker/package.json",
1206
- JSON.stringify({
1207
- name: "test",
1208
- version: "1.0.0",
1209
- devDependencies: {
1210
- wrangler: "0.0.0",
1211
- "@cloudflare/workers-types": "0.0.0",
1212
- },
1213
- }),
1214
- "utf-8"
1215
- );
1216
- fs.writeFileSync(
1217
- "path/to/worker/tsconfig.json",
1218
- JSON.stringify({ compilerOptions: {} }),
1219
- "utf-8"
1220
- );
1221
-
1222
- mockConfirm({
1223
- text: "Would you like to use git to manage this Worker?",
1224
- result: false,
1225
- });
1226
-
1227
- mockSelect({
1228
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
1229
- result: "fetch",
1230
- });
1231
-
1232
- await runWrangler("init path/to/worker/my-worker");
1233
- const tsconfigJson = JSON.parse(
1234
- fs.readFileSync("path/to/worker/tsconfig.json", "utf-8")
1235
- );
1236
- expect(tsconfigJson.compilerOptions).toEqual({});
1237
- expect(std).toMatchInlineSnapshot(`
1238
- Object {
1239
- "debug": "",
1240
- "err": "",
1241
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1242
- ✨ Created path/to/worker/my-worker/src/index.ts
1243
-
1244
- To start developing your Worker, run \`npx wrangler dev\`
1245
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1246
- "warn": "",
1247
- }
1248
- `);
1249
- });
1250
-
1251
- it("should offer to install type definitions in an existing typescript project", async () => {
1252
- mockConfirm(
1253
- {
1254
- text: "Would you like to use git to manage this Worker?",
1255
- result: false,
1256
- },
1257
- {
1258
- text: "Would you like to install wrangler into package.json?",
1259
- result: false,
1260
- },
1261
- {
1262
- text: "Would you like to install the type definitions for Workers into your package.json?",
1263
- result: true,
1264
- }
1265
- );
1266
- mockSelect({
1267
- text: "Would you like to create a Worker at src/index.ts?",
1268
- result: "none",
1269
- });
1270
- fs.writeFileSync(
1271
- "./package.json",
1272
- JSON.stringify({
1273
- name: "test",
1274
- version: "1.0.0",
1275
- }),
1276
- "utf-8"
1277
- );
1278
- fs.writeFileSync(
1279
- "./tsconfig.json",
1280
- JSON.stringify({ compilerOptions: {} }),
1281
- "utf-8"
1282
- );
1283
-
1284
- await runWrangler("init");
1285
- const tsconfigJson = JSON.parse(
1286
- fs.readFileSync("./tsconfig.json", "utf-8")
1287
- );
1288
- // unchanged tsconfig
1289
- expect(tsconfigJson.compilerOptions).toEqual({});
1290
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1291
- "@cloudflare/workers-types"
1292
- );
1293
- expect(std).toMatchInlineSnapshot(`
1294
- Object {
1295
- "debug": "",
1296
- "err": "",
1297
- "out": "✨ Created wrangler.toml
1298
- ✨ Installed @cloudflare/workers-types into devDependencies
1299
- 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1300
- "warn": "",
1301
- }
1302
- `);
1303
- });
1304
-
1305
- it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1306
- fs.writeFileSync(
1307
- "./package.json",
1308
- JSON.stringify({
1309
- name: "test",
1310
- version: "1.0.0",
1311
- devDependencies: {
1312
- wrangler: "0.0.0",
1313
- "@cloudflare/workers-types": "0.0.0",
1314
- },
1315
- }),
1316
- "utf-8"
1317
- );
1318
- fs.writeFileSync(
1319
- "./tsconfig.json",
1320
- JSON.stringify({ compilerOptions: {} }),
1321
- "utf-8"
1322
- );
1323
-
1324
- mockConfirm({
1325
- text: "Would you like to use git to manage this Worker?",
1326
- result: false,
1327
- });
1328
-
1329
- mockSelect({
1330
- text: "Would you like to create a Worker at src/index.ts?",
1331
- result: "fetch",
1332
- });
1333
-
1334
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
1335
- process.chdir("./sub-1/sub-2");
1336
-
1337
- await runWrangler("init");
1338
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
1339
- expect(fs.existsSync("../../tsconfig.json")).toBe(true);
1340
-
1341
- const tsconfigJson = JSON.parse(
1342
- fs.readFileSync("../../tsconfig.json", "utf-8")
1343
- );
1344
- expect(tsconfigJson.compilerOptions).toEqual({});
1345
-
1346
- expect(std).toMatchInlineSnapshot(`
1347
- Object {
1348
- "debug": "",
1349
- "err": "",
1350
- "out": "✨ Created wrangler.toml
1351
- ✨ Created src/index.ts
1352
-
1353
- To start developing your Worker, run \`npx wrangler dev\`
1354
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1355
- "warn": "",
1356
- }
1357
- `);
1358
- });
1359
- });
1360
-
1361
- describe("javascript", () => {
1362
- it("should add missing scripts for a non-ts project with .js extension", async () => {
1363
- mockConfirm(
1364
- {
1365
- text: "Would you like to use git to manage this Worker?",
1366
- result: false,
1367
- },
1368
- {
1369
- text: "No package.json found. Would you like to create one?",
1370
- result: true,
1371
- },
1372
- {
1373
- text: "Would you like to install wrangler into package.json?",
1374
- result: false,
1375
- },
1376
- {
1377
- text: "Would you like to use TypeScript?",
1378
- result: false,
1379
- }
1380
- );
1381
- mockSelect({
1382
- text: "Would you like to create a Worker at src/index.js?",
1383
- result: "fetch",
1384
- });
1385
- await runWrangler("init");
1386
-
1387
- expect(fs.existsSync("./package.json")).toBe(true);
1388
- const packageJson = JSON.parse(
1389
- fs.readFileSync("./package.json", "utf-8")
1390
- );
1391
-
1392
- expect(fs.existsSync("./src/index.js")).toBe(true);
1393
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1394
-
1395
- expect(packageJson.scripts.start).toBe("wrangler dev");
1396
- expect(packageJson.scripts.deploy).toBe("wrangler publish");
1397
- expect(packageJson.name).toContain("wrangler-tests");
1398
- expect(packageJson.version).toEqual("0.0.0");
1399
- expect(std.out).toMatchInlineSnapshot(`
1400
- "✨ Created wrangler.toml
1401
- ✨ Created package.json
1402
- ✨ Created src/index.js
1403
-
1404
- To start developing your Worker, run \`npm start\`
1405
- To publish your Worker to the Internet, run \`npm run deploy\`"
1406
- `);
1407
- });
1408
-
1409
- it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
1410
- mockConfirm(
1411
- {
1412
- text: "Would you like to use git to manage this Worker?",
1413
- result: false,
1414
- },
1415
- {
1416
- text: "Would you like to install wrangler into package.json?",
1417
- result: false,
1418
- },
1419
- {
1420
- text: "Would you like to use TypeScript?",
1421
- result: false,
1422
- }
1423
- );
1424
- mockSelect({
1425
- text: "Would you like to create a Worker at src/index.js?",
1426
- result: "fetch",
1427
- });
1428
- await fsp.writeFile(
1429
- "./package.json",
1430
- JSON.stringify({
1431
- scripts: {
1432
- start: "test-start",
1433
- deploy: "test-publish",
1434
- },
1435
- })
1436
- );
1437
- const packageJson = JSON.parse(
1438
- fs.readFileSync("./package.json", "utf-8")
1439
- );
1440
- await runWrangler("init");
1441
-
1442
- expect(fs.existsSync("./src/index.js")).toBe(true);
1443
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1444
-
1445
- expect(packageJson.scripts.start).toBe("test-start");
1446
- expect(packageJson.scripts.deploy).toBe("test-publish");
1447
- expect(std.out).toMatchInlineSnapshot(`
1448
- "✨ Created wrangler.toml
1449
- ✨ Created src/index.js
1450
-
1451
- To start developing your Worker, run \`npx wrangler dev\`
1452
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
1453
- `);
1454
- });
1455
-
1456
- it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
1457
- mockConfirm(
1458
- {
1459
- text: "Would you like to use git to manage this Worker?",
1460
- result: false,
1461
- },
1462
- {
1463
- text: "Would you like to install wrangler into package.json?",
1464
- result: false,
1465
- },
1466
- {
1467
- text: "Would you like to use TypeScript?",
1468
- result: false,
1469
- }
1470
- );
1471
-
1472
- fs.writeFileSync(
1473
- "./package.json",
1474
- JSON.stringify({ name: "test", version: "1.0.0" }),
1475
- "utf-8"
1476
- );
1477
- fs.mkdirSync("./src", { recursive: true });
1478
- const PLACEHOLDER = "/* placeholder text */";
1479
- fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
1480
-
1481
- await runWrangler("init");
1482
- expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
1483
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1484
- expect(std).toMatchInlineSnapshot(`
1485
- Object {
1486
- "debug": "",
1487
- "err": "",
1488
- "out": "✨ Created wrangler.toml",
1489
- "warn": "",
1490
- }
1491
- `);
1492
- });
1493
-
1494
- it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1495
- mockConfirm(
1496
- {
1497
- text: "Would you like to use git to manage this Worker?",
1498
- result: false,
1499
- },
1500
- {
1501
- text: "Would you like to install wrangler into package.json?",
1502
- result: false,
1503
- },
1504
- {
1505
- text: "Would you like to use TypeScript?",
1506
- result: false,
1507
- }
1508
- );
1509
-
1510
- fs.writeFileSync(
1511
- "./package.json",
1512
- JSON.stringify({ name: "test", version: "1.0.0" }),
1513
- "utf-8"
1514
- );
1515
- fs.mkdirSync("./my-worker/src", { recursive: true });
1516
- const PLACEHOLDER = "/* placeholder text */";
1517
- fs.writeFileSync("./my-worker/src/index.js", PLACEHOLDER, "utf-8");
1518
-
1519
- await runWrangler("init my-worker");
1520
- expect(fs.readFileSync("./my-worker/src/index.js", "utf-8")).toBe(
1521
- PLACEHOLDER
1522
- );
1523
- expect(fs.existsSync("./my-worker/src/index.ts")).toBe(false);
1524
- expect(std).toMatchInlineSnapshot(`
1525
- Object {
1526
- "debug": "",
1527
- "err": "",
1528
- "out": "✨ Created my-worker/wrangler.toml",
1529
- "warn": "",
1530
- }
1531
- `);
1532
- });
1533
- });
1534
-
1535
- describe("worker names", () => {
1536
- it("should create a worker with a given name", async () => {
1537
- await runWrangler("init my-worker -y");
1538
-
1539
- const parsed = TOML.parse(
1540
- await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
1541
- );
1542
-
1543
- expect(typeof parsed.compatibility_date).toBe("string");
1544
- expect(parsed.name).toBe("my-worker");
1545
- });
1546
-
1547
- it('should create a worker with the name of the current directory if "name" is .', async () => {
1548
- await runWrangler("init . -y");
1549
-
1550
- const parsed = TOML.parse(await fsp.readFile("wrangler.toml", "utf-8"));
1551
-
1552
- expect(typeof parsed.compatibility_date).toBe("string");
1553
- expect(parsed.name).toBe(path.basename(process.cwd()).toLowerCase());
1554
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1555
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1556
- expect(std).toMatchInlineSnapshot(`
1557
- Object {
1558
- "debug": "",
1559
- "err": "",
1560
- "out": "✨ Created wrangler.toml
1561
- ✨ Initialized git repository
1562
- ✨ Created package.json
1563
- ✨ Created tsconfig.json
1564
- ✨ Created src/index.ts
1565
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1566
-
1567
- To start developing your Worker, run \`npm start\`
1568
- To publish your Worker to the Internet, run \`npm run deploy\`",
1569
- "warn": "",
1570
- }
1571
- `);
1572
- });
1573
-
1574
- it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
1575
- await runWrangler("init path/to/worker -y");
1576
-
1577
- const parsed = TOML.parse(
1578
- await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
1579
- );
1580
-
1581
- expect(typeof parsed.compatibility_date).toBe("string");
1582
- expect(parsed.name).toBe("worker");
1583
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1584
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1585
- expect(std).toMatchInlineSnapshot(`
1586
- Object {
1587
- "debug": "",
1588
- "err": "",
1589
- "out": "✨ Created path/to/worker/wrangler.toml
1590
- ✨ Initialized git repository at path/to/worker
1591
- ✨ Created path/to/worker/package.json
1592
- ✨ Created path/to/worker/tsconfig.json
1593
- ✨ Created path/to/worker/src/index.ts
1594
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1595
-
1596
- To start developing your Worker, run \`cd path/to/worker && npm start\`
1597
- To publish your Worker to the Internet, run \`npm run deploy\`",
1598
- "warn": "",
1599
- }
1600
- `);
1601
- });
1602
-
1603
- it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
1604
- await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
1605
- const parsed = TOML.parse(
1606
- await fsp.readFile(
1607
- "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml",
1608
- "utf-8"
1609
- )
1610
- );
1611
-
1612
- expect(typeof parsed.compatibility_date).toBe("string");
1613
- expect(parsed.name).toBe("weird_w0rkr_n4m3-js-tsx-tar-gz");
1614
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1615
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1616
- expect(std).toMatchInlineSnapshot(`
1617
- Object {
1618
- "debug": "",
1619
- "err": "",
1620
- "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1621
- ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1622
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1623
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
1624
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1625
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1626
-
1627
- To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1628
- To publish your Worker to the Internet, run \`npm run deploy\`",
1629
- "warn": "",
1630
- }
1631
- `);
1632
- });
1633
- });
181
+ });
182
+ });
183
+
184
+ describe("wrangler.toml", () => {
185
+ it("should create a wrangler.toml", async () => {
186
+ mockConfirm(
187
+ {
188
+ text: "Would you like to use git to manage this Worker?",
189
+ result: false,
190
+ },
191
+ {
192
+ text: "No package.json found. Would you like to create one?",
193
+ result: false,
194
+ }
195
+ );
196
+ await runWrangler("init");
197
+ checkFiles({
198
+ items: {
199
+ "wrangler.toml": wranglerToml({
200
+ compatibility_date: expect.any(String),
201
+ name: expect.stringContaining("wrangler-tests"),
202
+ }),
203
+ "package.json": false,
204
+ "tsconfig.json": false,
205
+ },
206
+ });
207
+
208
+ expect(std).toMatchInlineSnapshot(`
209
+ Object {
210
+ "debug": "",
211
+ "err": "",
212
+ "out": "✨ Created wrangler.toml",
213
+ "warn": "",
214
+ }
215
+ `);
216
+ });
217
+
218
+ it("should create a wrangler.toml and a directory for a named Worker ", async () => {
219
+ mockConfirm(
220
+ {
221
+ text: "Would you like to use git to manage this Worker?",
222
+ result: false,
223
+ },
224
+ {
225
+ text: "No package.json found. Would you like to create one?",
226
+ result: false,
227
+ }
228
+ );
229
+ await runWrangler("init my-worker");
230
+
231
+ checkFiles({
232
+ items: {
233
+ "my-worker/wrangler.toml": wranglerToml({
234
+ compatibility_date: expect.any(String),
235
+ name: "my-worker",
236
+ }),
237
+ "my-worker/package.json": false,
238
+ "my-worker/tsconfig.json": false,
239
+ },
240
+ });
241
+
242
+ expect(std).toMatchInlineSnapshot(`
243
+ Object {
244
+ "debug": "",
245
+ "err": "",
246
+ "out": "✨ Created my-worker/wrangler.toml",
247
+ "warn": "",
248
+ }
249
+ `);
250
+ });
251
+
252
+ it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
253
+ writeFiles({
254
+ items: {
255
+ "wrangler.toml": wranglerToml({
256
+ // use a fake value to make sure the file is not overwritten
257
+ compatibility_date: "something-else",
258
+ }),
259
+ },
260
+ });
261
+
262
+ mockConfirm({
263
+ text: "Do you want to continue initializing this project?",
264
+ result: false,
265
+ });
266
+
267
+ await runWrangler("init");
268
+ expect(std.warn).toContain("wrangler.toml already exists!");
269
+
270
+ checkFiles({
271
+ items: {
272
+ "wrangler.toml": wranglerToml({
273
+ compatibility_date: "something-else",
274
+ }),
275
+ "package.json": false,
276
+ "tsconfig.json": false,
277
+ },
278
+ });
279
+
280
+ expect(std).toMatchInlineSnapshot(`
281
+ Object {
282
+ "debug": "",
283
+ "err": "",
284
+ "out": "",
285
+ "warn": "▲ [WARNING] wrangler.toml already exists!
286
+
287
+ ",
288
+ }
289
+ `);
290
+ });
291
+
292
+ it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
293
+ writeFiles({
294
+ items: {
295
+ "path/to/worker/wrangler.toml": wranglerToml({
296
+ compatibility_date: "something-else",
297
+ }),
298
+ },
299
+ });
300
+ mockConfirm({
301
+ text: "Do you want to continue initializing this project?",
302
+ result: false,
303
+ });
304
+
305
+ await runWrangler("init path/to/worker");
306
+
307
+ expect(std.warn).toContain("wrangler.toml already exists!");
308
+ checkFiles({
309
+ items: {
310
+ "path/to/worker/wrangler.toml": wranglerToml({
311
+ compatibility_date: "something-else",
312
+ }),
313
+ "package.json": false,
314
+ "tsconfig.json": false,
315
+ },
316
+ });
317
+ expect(std).toMatchInlineSnapshot(`
318
+ Object {
319
+ "debug": "",
320
+ "err": "",
321
+ "out": "",
322
+ "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
323
+
324
+ ",
325
+ }
326
+ `);
327
+ });
328
+
329
+ it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
330
+ writeFiles({
331
+ items: {
332
+ "wrangler.toml": wranglerToml({
333
+ compatibility_date: "something-else",
334
+ }),
335
+ },
336
+ });
337
+ mockConfirm(
338
+ {
339
+ text: "Would you like to use git to manage this Worker?",
340
+ result: true,
341
+ },
342
+ {
343
+ text: "Do you want to continue initializing this project?",
344
+ result: true,
345
+ },
346
+ {
347
+ text: "No package.json found. Would you like to create one?",
348
+ result: true,
349
+ },
350
+ {
351
+ text: "Would you like to use TypeScript?",
352
+ result: true,
353
+ }
354
+ );
355
+
356
+ mockSelect({
357
+ text: "Would you like to create a Worker at src/index.ts?",
358
+ result: "fetch",
359
+ });
360
+
361
+ await runWrangler("init");
362
+
363
+ checkFiles({
364
+ items: {
365
+ "wrangler.toml": wranglerToml({
366
+ compatibility_date: "something-else",
367
+ }),
368
+ },
369
+ });
370
+ });
371
+
372
+ it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
373
+ writeFiles({
374
+ items: {
375
+ "wrangler.toml": wranglerToml({
376
+ compatibility_date: "something-else",
377
+ }),
378
+ },
379
+ });
380
+ mockConfirm(
381
+ {
382
+ text: "Do you want to continue initializing this project?",
383
+ result: true,
384
+ },
385
+ {
386
+ text: "Would you like to use git to manage this Worker?",
387
+ result: false,
388
+ },
389
+ {
390
+ text: "No package.json found. Would you like to create one?",
391
+ result: false,
392
+ }
393
+ );
394
+
395
+ await runWrangler("init");
396
+
397
+ expect(std.warn).toContain("wrangler.toml already exists!");
398
+ checkFiles({
399
+ items: {
400
+ "wrangler.toml": wranglerToml({
401
+ compatibility_date: "something-else",
402
+ }),
403
+ },
404
+ });
405
+ expect(std).toMatchInlineSnapshot(`
406
+ Object {
407
+ "debug": "",
408
+ "err": "",
409
+ "out": "",
410
+ "warn": "▲ [WARNING] wrangler.toml already exists!
411
+
412
+ ",
413
+ }
414
+ `);
415
+ });
416
+
417
+ it("should not add a Cron Trigger to wrangler.toml when creating a Scheduled Worker if wrangler.toml already exists", async () => {
418
+ writeFiles({
419
+ items: {
420
+ "wrangler.toml": wranglerToml({
421
+ compatibility_date: "something-else",
422
+ }),
423
+ },
424
+ });
425
+ mockConfirm(
426
+ {
427
+ text: "Would you like to use git to manage this Worker?",
428
+ result: true,
429
+ },
430
+ {
431
+ text: "Do you want to continue initializing this project?",
432
+ result: true,
433
+ },
434
+ {
435
+ text: "No package.json found. Would you like to create one?",
436
+ result: true,
437
+ },
438
+ {
439
+ text: "Would you like to use TypeScript?",
440
+ result: true,
441
+ }
442
+ );
443
+ mockSelect({
444
+ text: "Would you like to create a Worker at src/index.ts?",
445
+ result: "scheduled",
446
+ });
447
+
448
+ await runWrangler("init");
449
+
450
+ checkFiles({
451
+ items: {
452
+ "wrangler.toml": wranglerToml({
453
+ compatibility_date: "something-else",
454
+ }),
455
+ },
456
+ });
457
+ });
458
+
459
+ 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 () => {
460
+ mockConfirm(
461
+ {
462
+ text: "Would you like to use git to manage this Worker?",
463
+ result: true,
464
+ },
465
+ {
466
+ text: "No package.json found. Would you like to create one?",
467
+ result: true,
468
+ },
469
+ {
470
+ text: "Would you like to use TypeScript?",
471
+ result: true,
472
+ }
473
+ );
474
+ mockSelect({
475
+ text: "Would you like to create a Worker at src/index.ts?",
476
+ result: "scheduled",
477
+ });
478
+
479
+ await runWrangler("init");
480
+
481
+ checkFiles({
482
+ items: {
483
+ "wrangler.toml": wranglerToml({
484
+ ...MINIMAL_WRANGLER_TOML,
485
+ triggers: { crons: ["1 * * * *"] },
486
+ }),
487
+ },
488
+ });
489
+ });
490
+ });
491
+
492
+ describe("git init", () => {
493
+ it("should offer to initialize a git repository", async () => {
494
+ mockConfirm(
495
+ {
496
+ text: "Would you like to use git to manage this Worker?",
497
+ result: true,
498
+ },
499
+ {
500
+ text: "No package.json found. Would you like to create one?",
501
+ result: false,
502
+ }
503
+ );
504
+
505
+ await runWrangler("init");
506
+
507
+ checkFiles({
508
+ items: {
509
+ "wrangler.toml": wranglerToml({
510
+ compatibility_date: expect.any(String),
511
+ name: expect.stringContaining("wrangler-tests"),
512
+ }),
513
+ ".git": { items: {} },
514
+ ".gitignore": true,
515
+ },
516
+ });
517
+ expect(std).toMatchInlineSnapshot(`
518
+ Object {
519
+ "debug": "",
520
+ "err": "",
521
+ "out": "✨ Created wrangler.toml
522
+ ✨ Initialized git repository",
523
+ "warn": "",
524
+ }
525
+ `);
526
+ expect((await execa("git", ["branch", "--show-current"])).stdout).toEqual(
527
+ "main"
528
+ );
529
+ });
530
+
531
+ it("should not offer to initialize a git repo if it's already inside one", async () => {
532
+ await execa("git", ["init"]);
533
+ setWorkingDirectory("some-folder");
534
+
535
+ await runWrangler("init -y");
536
+
537
+ checkFiles({
538
+ items: {
539
+ "wrangler.toml": wranglerToml({
540
+ ...MINIMAL_WRANGLER_TOML,
541
+ name: "some-folder",
542
+ }),
543
+ ".git": { items: {} },
544
+ ".gitignore": false,
545
+ },
546
+ });
547
+
548
+ // Note the lack of "✨ Initialized git repository" in the log
549
+ expect(std).toMatchInlineSnapshot(`
550
+ Object {
551
+ "debug": "",
552
+ "err": "",
553
+ "out": "✨ Created wrangler.toml
554
+ ✨ Created package.json
555
+ ✨ Created tsconfig.json
556
+ ✨ Created src/index.ts
557
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
558
+
559
+ To start developing your Worker, run \`npm start\`
560
+ To publish your Worker to the Internet, run \`npm run deploy\`",
561
+ "warn": "",
562
+ }
563
+ `);
564
+ });
565
+
566
+ it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
567
+ fs.mkdirSync("path/to/worker", { recursive: true });
568
+ await execa("git", ["init"], { cwd: "path/to/worker" });
569
+ expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
570
+
571
+ await runWrangler("init path/to/worker/my-worker -y");
572
+
573
+ // Note the lack of "✨ Initialized git repository" in the log
574
+ expect(std).toMatchInlineSnapshot(`
575
+ Object {
576
+ "debug": "",
577
+ "err": "",
578
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
579
+ ✨ Created path/to/worker/my-worker/package.json
580
+ ✨ Created path/to/worker/my-worker/tsconfig.json
581
+ ✨ Created path/to/worker/my-worker/src/index.ts
582
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
583
+
584
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
585
+ To publish your Worker to the Internet, run \`npm run deploy\`",
586
+ "warn": "",
587
+ }
588
+ `);
589
+ });
590
+
591
+ // I... don't know how to test this lol
592
+ it.todo(
593
+ "should not offer to initialize a git repo if git is not installed"
594
+ );
595
+
596
+ it("should initialize git repo with `main` default branch", async () => {
597
+ mockConfirm(
598
+ {
599
+ text: "Would you like to use git to manage this Worker?",
600
+ result: true,
601
+ },
602
+ {
603
+ text: "No package.json found. Would you like to create one?",
604
+ result: false,
605
+ }
606
+ );
607
+ await runWrangler("init");
608
+ expect(std).toMatchInlineSnapshot(`
609
+ Object {
610
+ "debug": "",
611
+ "err": "",
612
+ "out": "✨ Created wrangler.toml
613
+ ✨ Initialized git repository",
614
+ "warn": "",
615
+ }
616
+ `);
617
+
618
+ expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
619
+ "refs/heads/main"
620
+ );
621
+ });
622
+ });
623
+
624
+ describe("package.json", () => {
625
+ it("should create a package.json if none is found and user confirms", async () => {
626
+ mockConfirm(
627
+ {
628
+ text: "Would you like to use git to manage this Worker?",
629
+ result: false,
630
+ },
631
+ {
632
+ text: "No package.json found. Would you like to create one?",
633
+ result: true,
634
+ },
635
+ {
636
+ text: "Would you like to use TypeScript?",
637
+ result: false,
638
+ }
639
+ );
640
+ mockSelect({
641
+ text: "Would you like to create a Worker at src/index.js?",
642
+ result: "none",
643
+ });
644
+
645
+ await runWrangler("init");
646
+
647
+ checkFiles({
648
+ items: {
649
+ "package.json": {
650
+ contents: expect.objectContaining({
651
+ name: expect.stringContaining("wrangler-tests"),
652
+ version: "0.0.0",
653
+ devDependencies: {
654
+ wrangler: expect.any(String),
655
+ },
656
+ }),
657
+ },
658
+ "tsconfig.json": false,
659
+ },
660
+ });
661
+ expect(mockPackageManager.install).toHaveBeenCalled();
662
+ expect(std).toMatchInlineSnapshot(`
663
+ Object {
664
+ "debug": "",
665
+ "err": "",
666
+ "out": "✨ Created wrangler.toml
667
+ ✨ Created package.json",
668
+ "warn": "",
669
+ }
670
+ `);
671
+ });
672
+
673
+ it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
674
+ mockConfirm(
675
+ {
676
+ text: "Would you like to use git to manage this Worker?",
677
+ result: false,
678
+ },
679
+ {
680
+ text: "No package.json found. Would you like to create one?",
681
+ result: true,
682
+ },
683
+ {
684
+ text: "Would you like to use TypeScript?",
685
+ result: false,
686
+ }
687
+ );
688
+ mockSelect({
689
+ text: "Would you like to create a Worker at my-worker/src/index.js?",
690
+ result: "none",
691
+ });
692
+
693
+ await runWrangler("init my-worker");
694
+
695
+ checkFiles({
696
+ items: {
697
+ "my-worker/package.json": {
698
+ contents: expect.objectContaining({
699
+ name: "my-worker",
700
+ version: "0.0.0",
701
+ devDependencies: {
702
+ wrangler: expect.any(String),
703
+ },
704
+ }),
705
+ },
706
+ "my-worker/tsconfig.json": false,
707
+ },
708
+ });
709
+ expect(std).toMatchInlineSnapshot(`
710
+ Object {
711
+ "debug": "",
712
+ "err": "",
713
+ "out": "✨ Created my-worker/wrangler.toml
714
+ ✨ Created my-worker/package.json",
715
+ "warn": "",
716
+ }
717
+ `);
718
+ });
719
+
720
+ it("should not touch an existing package.json in the same directory", async () => {
721
+ mockConfirm(
722
+ {
723
+ text: "Would you like to use git to manage this Worker?",
724
+ result: false,
725
+ },
726
+ {
727
+ text: "Would you like to install wrangler into package.json?",
728
+ result: false,
729
+ },
730
+ {
731
+ text: "Would you like to use TypeScript?",
732
+ result: false,
733
+ }
734
+ );
735
+ mockSelect({
736
+ text: "Would you like to create a Worker at src/index.js?",
737
+ result: "none",
738
+ });
739
+ writeFiles({
740
+ items: {
741
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
742
+ },
743
+ });
744
+
745
+ await runWrangler("init");
746
+
747
+ checkFiles({
748
+ items: {
749
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
750
+ },
751
+ });
752
+ expect(std).toMatchInlineSnapshot(`
753
+ Object {
754
+ "debug": "",
755
+ "err": "",
756
+ "out": "✨ Created wrangler.toml",
757
+ "warn": "",
758
+ }
759
+ `);
760
+ });
761
+
762
+ it("should not touch an existing package.json in an ancestor directory, when a name is passed", async () => {
763
+ mockConfirm(
764
+ {
765
+ text: "Would you like to use git to manage this Worker?",
766
+ result: false,
767
+ },
768
+ {
769
+ text: "No package.json found. Would you like to create one?",
770
+ result: true,
771
+ },
772
+ {
773
+ text: "Would you like to install wrangler into path/to/worker/my-worker/package.json?",
774
+ result: false,
775
+ },
776
+ {
777
+ text: "Would you like to use TypeScript?",
778
+ result: false,
779
+ }
780
+ );
781
+ mockSelect({
782
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
783
+ result: "none",
784
+ });
785
+ writeFiles({
786
+ items: {
787
+ "path/to/worker/package.json": {
788
+ contents: { name: "test", version: "1.0.0" },
789
+ },
790
+ },
791
+ });
792
+
793
+ await runWrangler("init path/to/worker/my-worker");
794
+
795
+ checkFiles({
796
+ items: {
797
+ "path/to/worker/package.json": {
798
+ contents: { name: "test", version: "1.0.0" },
799
+ },
800
+ },
801
+ });
802
+ expect(std).toMatchInlineSnapshot(`
803
+ Object {
804
+ "debug": "",
805
+ "err": "",
806
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
807
+ ✨ Created path/to/worker/my-worker/package.json",
808
+ "warn": "",
809
+ }
810
+ `);
811
+ });
812
+
813
+ it("should offer to install wrangler into an existing package.json", async () => {
814
+ mockConfirm(
815
+ {
816
+ text: "Would you like to use git to manage this Worker?",
817
+ result: false,
818
+ },
819
+ {
820
+ text: "Would you like to install wrangler into package.json?",
821
+ result: true,
822
+ },
823
+ {
824
+ text: "Would you like to use TypeScript?",
825
+ result: false,
826
+ }
827
+ );
828
+ mockSelect({
829
+ text: "Would you like to create a Worker at src/index.js?",
830
+ result: "none",
831
+ });
832
+ writeFiles({
833
+ items: {
834
+ "package.json": {
835
+ contents: { name: "test", version: "1.0.0" },
836
+ },
837
+ },
838
+ });
839
+
840
+ await runWrangler("init");
841
+
842
+ checkFiles({
843
+ items: {
844
+ "package.json": {
845
+ contents: { name: "test", version: "1.0.0" },
846
+ },
847
+ },
848
+ });
849
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
850
+ `wrangler@${wranglerVersion}`
851
+ );
852
+ expect(std).toMatchInlineSnapshot(`
853
+ Object {
854
+ "debug": "",
855
+ "err": "",
856
+ "out": "✨ Created wrangler.toml
857
+ ✨ Installed wrangler into devDependencies",
858
+ "warn": "",
859
+ }
860
+ `);
861
+ });
862
+
863
+ it("should offer to install wrangler into a package.json relative to the target directory, if no name is provided", async () => {
864
+ mockConfirm(
865
+ {
866
+ text: "Would you like to use git to manage this Worker?",
867
+ result: false,
868
+ },
869
+ {
870
+ text: "Would you like to install wrangler into ../package.json?",
871
+ result: true,
872
+ },
873
+ {
874
+ text: "Would you like to use TypeScript?",
875
+ result: false,
876
+ }
877
+ );
878
+ mockSelect({
879
+ text: "Would you like to create a Worker at src/index.js?",
880
+ result: "none",
881
+ });
882
+ writeFiles({
883
+ items: {
884
+ "path/to/worker/package.json": {
885
+ contents: { name: "test", version: "1.0.0" },
886
+ },
887
+ },
888
+ });
889
+ setWorkingDirectory("path/to/worker/my-worker");
890
+
891
+ await runWrangler("init");
892
+
893
+ setWorkingDirectory("../../../..");
894
+ checkFiles({
895
+ items: {
896
+ "path/to/worker/package.json": {
897
+ contents: { name: "test", version: "1.0.0" },
898
+ },
899
+ "path/to/worker/my-worker/package.json": false,
900
+ },
901
+ });
902
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
903
+ `wrangler@${wranglerVersion}`
904
+ );
905
+ expect(mockPackageManager.cwd).toBe(process.cwd());
906
+ expect(std).toMatchInlineSnapshot(`
907
+ Object {
908
+ "debug": "",
909
+ "err": "",
910
+ "out": "✨ Created wrangler.toml
911
+ ✨ Installed wrangler into devDependencies",
912
+ "warn": "",
913
+ }
914
+ `);
915
+ });
916
+
917
+ it("should not touch an existing package.json in an ancestor directory", async () => {
918
+ mockConfirm(
919
+ {
920
+ text: "Would you like to use git to manage this Worker?",
921
+ result: false,
922
+ },
923
+ {
924
+ text: "Would you like to install wrangler into ../../package.json?",
925
+ result: false,
926
+ },
927
+ {
928
+ text: "Would you like to use TypeScript?",
929
+ result: false,
930
+ }
931
+ );
932
+ mockSelect({
933
+ text: "Would you like to create a Worker at src/index.js?",
934
+ result: "none",
935
+ });
936
+ writeFiles({
937
+ items: {
938
+ "package.json": {
939
+ contents: { name: "test", version: "1.0.0" },
940
+ },
941
+ },
942
+ });
943
+ setWorkingDirectory("./sub-1/sub-2");
944
+
945
+ await runWrangler("init");
946
+
947
+ checkFiles({
948
+ items: {
949
+ "package.json": false,
950
+ "../../package.json": {
951
+ contents: { name: "test", version: "1.0.0" },
952
+ },
953
+ },
954
+ });
955
+ expect(std).toMatchInlineSnapshot(`
956
+ Object {
957
+ "debug": "",
958
+ "err": "",
959
+ "out": "✨ Created wrangler.toml",
960
+ "warn": "",
961
+ }
962
+ `);
963
+ });
964
+ });
965
+
966
+ describe("typescript", () => {
967
+ it("should offer to create a worker in a non-typescript project", async () => {
968
+ mockConfirm(
969
+ {
970
+ text: "Would you like to use git to manage this Worker?",
971
+ result: false,
972
+ },
973
+ {
974
+ text: "Would you like to install wrangler into package.json?",
975
+ result: false,
976
+ },
977
+ {
978
+ text: "Would you like to use TypeScript?",
979
+ result: false,
980
+ }
981
+ );
982
+ mockSelect({
983
+ text: "Would you like to create a Worker at src/index.js?",
984
+ result: "fetch",
985
+ });
986
+ writeFiles({
987
+ items: {
988
+ "package.json": {
989
+ contents: { name: "test", version: "1.0.0" },
990
+ },
991
+ },
992
+ });
993
+
994
+ await runWrangler("init");
995
+
996
+ checkFiles({
997
+ items: {
998
+ "wrangler.toml": wranglerToml({
999
+ ...MINIMAL_WRANGLER_TOML,
1000
+ main: "src/index.js",
1001
+ }),
1002
+ "src/index.js": true,
1003
+ "src/index.ts": false,
1004
+ },
1005
+ });
1006
+ expect(std).toMatchInlineSnapshot(`
1007
+ Object {
1008
+ "debug": "",
1009
+ "err": "",
1010
+ "out": "✨ Created wrangler.toml
1011
+ ✨ Created src/index.js
1012
+
1013
+ To start developing your Worker, run \`npx wrangler dev\`
1014
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1015
+ "warn": "",
1016
+ }
1017
+ `);
1018
+ });
1019
+
1020
+ it("should offer to create a worker in a typescript project", async () => {
1021
+ mockConfirm(
1022
+ {
1023
+ text: "Would you like to use git to manage this Worker?",
1024
+ result: false,
1025
+ },
1026
+ {
1027
+ text: "Would you like to install wrangler into package.json?",
1028
+ result: false,
1029
+ },
1030
+ {
1031
+ text: "Would you like to use TypeScript?",
1032
+ result: true,
1033
+ }
1034
+ );
1035
+ mockSelect({
1036
+ text: "Would you like to create a Worker at src/index.ts?",
1037
+ result: "fetch",
1038
+ });
1039
+ writeFiles({
1040
+ items: {
1041
+ "package.json": {
1042
+ contents: { name: "test", version: "1.0.0" },
1043
+ },
1044
+ },
1045
+ });
1046
+
1047
+ await runWrangler("init");
1048
+
1049
+ checkFiles({
1050
+ items: {
1051
+ "src/index.js": false,
1052
+ "src/index.ts": true,
1053
+ },
1054
+ });
1055
+ expect(std).toMatchInlineSnapshot(`
1056
+ Object {
1057
+ "debug": "",
1058
+ "err": "",
1059
+ "out": "✨ Created wrangler.toml
1060
+ ✨ Created tsconfig.json
1061
+ ✨ Created src/index.ts
1062
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1063
+
1064
+ To start developing your Worker, run \`npx wrangler dev\`
1065
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1066
+ "warn": "",
1067
+ }
1068
+ `);
1069
+ });
1070
+
1071
+ it("should add scripts for a typescript project with .ts extension", async () => {
1072
+ mockConfirm(
1073
+ {
1074
+ text: "Would you like to use git to manage this Worker?",
1075
+ result: false,
1076
+ },
1077
+ {
1078
+ text: "No package.json found. Would you like to create one?",
1079
+ result: true,
1080
+ },
1081
+ {
1082
+ text: "Would you like to install wrangler into package.json?",
1083
+ result: false,
1084
+ },
1085
+ {
1086
+ text: "Would you like to use TypeScript?",
1087
+ result: true,
1088
+ }
1089
+ );
1090
+ mockSelect({
1091
+ text: "Would you like to create a Worker at src/index.ts?",
1092
+ result: "fetch",
1093
+ });
1094
+
1095
+ await runWrangler("init");
1096
+
1097
+ checkFiles({
1098
+ items: {
1099
+ "package.json": {
1100
+ contents: expect.objectContaining({
1101
+ name: expect.stringContaining("wrangler-tests"),
1102
+ version: "0.0.0",
1103
+ scripts: {
1104
+ start: "wrangler dev",
1105
+ deploy: "wrangler publish",
1106
+ },
1107
+ }),
1108
+ },
1109
+ "src/index.js": false,
1110
+ "src/index.ts": true,
1111
+ },
1112
+ });
1113
+ expect(std.out).toMatchInlineSnapshot(`
1114
+ "✨ Created wrangler.toml
1115
+ ✨ Created package.json
1116
+ ✨ Created tsconfig.json
1117
+ ✨ Created src/index.ts
1118
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1119
+
1120
+ To start developing your Worker, run \`npm start\`
1121
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1122
+ `);
1123
+ });
1124
+
1125
+ it("should not overwrite package.json scripts for a typescript project", async () => {
1126
+ mockConfirm(
1127
+ {
1128
+ text: "Would you like to use git to manage this Worker?",
1129
+ result: false,
1130
+ },
1131
+ {
1132
+ text: "Would you like to install wrangler into package.json?",
1133
+ result: false,
1134
+ },
1135
+ {
1136
+ text: "Would you like to use TypeScript?",
1137
+ result: true,
1138
+ }
1139
+ );
1140
+ mockSelect({
1141
+ text: "Would you like to create a Worker at src/index.ts?",
1142
+ result: "fetch",
1143
+ });
1144
+ writeFiles({
1145
+ items: {
1146
+ "package.json": {
1147
+ contents: {
1148
+ scripts: {
1149
+ start: "test-start",
1150
+ deploy: "test-publish",
1151
+ },
1152
+ },
1153
+ },
1154
+ },
1155
+ });
1156
+
1157
+ await runWrangler("init");
1158
+
1159
+ checkFiles({
1160
+ items: {
1161
+ "package.json": {
1162
+ contents: {
1163
+ scripts: {
1164
+ start: "test-start",
1165
+ deploy: "test-publish",
1166
+ },
1167
+ },
1168
+ },
1169
+ "src/index.js": false,
1170
+ "src/index.ts": true,
1171
+ },
1172
+ });
1173
+ expect(std.out).toMatchInlineSnapshot(`
1174
+ "✨ Created wrangler.toml
1175
+ ✨ Created tsconfig.json
1176
+ ✨ Created src/index.ts
1177
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1178
+
1179
+ To start developing your Worker, run \`npx wrangler dev\`
1180
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1181
+ `);
1182
+ });
1183
+
1184
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
1185
+ mockConfirm(
1186
+ {
1187
+ text: "Would you like to use git to manage this Worker?",
1188
+ result: false,
1189
+ },
1190
+ {
1191
+ text: "Would you like to install wrangler into package.json?",
1192
+ result: false,
1193
+ },
1194
+ {
1195
+ text: "Would you like to use TypeScript?",
1196
+ result: true,
1197
+ }
1198
+ );
1199
+ const PLACEHOLDER = "/* placeholder text */";
1200
+ writeFiles({
1201
+ items: {
1202
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1203
+ "src/index.ts": { contents: PLACEHOLDER },
1204
+ },
1205
+ });
1206
+
1207
+ await runWrangler("init");
1208
+
1209
+ checkFiles({
1210
+ items: {
1211
+ "src/index.js": false,
1212
+ "src/index.ts": { contents: PLACEHOLDER },
1213
+ },
1214
+ });
1215
+ expect(std).toMatchInlineSnapshot(`
1216
+ Object {
1217
+ "debug": "",
1218
+ "err": "",
1219
+ "out": "✨ Created wrangler.toml
1220
+ ✨ Created tsconfig.json
1221
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1222
+ "warn": "",
1223
+ }
1224
+ `);
1225
+ });
1226
+
1227
+ 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 () => {
1228
+ mockConfirm(
1229
+ {
1230
+ text: "Would you like to use git to manage this Worker?",
1231
+ result: false,
1232
+ },
1233
+ {
1234
+ text: "No package.json found. Would you like to create one?",
1235
+ result: true,
1236
+ },
1237
+ {
1238
+ text: "Would you like to install wrangler into package.json?",
1239
+ result: false,
1240
+ },
1241
+ {
1242
+ text: "Would you like to use TypeScript?",
1243
+ result: true,
1244
+ }
1245
+ );
1246
+ const PLACEHOLDER = "/* placeholder text */";
1247
+ writeFiles({
1248
+ items: {
1249
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1250
+ "my-worker/src/index.ts": { contents: PLACEHOLDER },
1251
+ },
1252
+ });
1253
+
1254
+ await runWrangler("init my-worker");
1255
+
1256
+ checkFiles({
1257
+ items: {
1258
+ "my-worker/src/index.js": false,
1259
+ "my-worker/src/index.ts": { contents: PLACEHOLDER },
1260
+ },
1261
+ });
1262
+ expect(std).toMatchInlineSnapshot(`
1263
+ Object {
1264
+ "debug": "",
1265
+ "err": "",
1266
+ "out": "✨ Created my-worker/wrangler.toml
1267
+ ✨ Created my-worker/package.json
1268
+ ✨ Created my-worker/tsconfig.json
1269
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1270
+ "warn": "",
1271
+ }
1272
+ `);
1273
+ });
1274
+
1275
+ it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
1276
+ mockConfirm(
1277
+ {
1278
+ text: "Would you like to use git to manage this Worker?",
1279
+ result: false,
1280
+ },
1281
+ {
1282
+ text: "No package.json found. Would you like to create one?",
1283
+ result: true,
1284
+ },
1285
+ {
1286
+ text: "Would you like to use TypeScript?",
1287
+ result: true,
1288
+ }
1289
+ );
1290
+ mockSelect({
1291
+ text: "Would you like to create a Worker at src/index.ts?",
1292
+ result: "none",
1293
+ });
1294
+
1295
+ await runWrangler("init");
1296
+
1297
+ checkFiles({
1298
+ items: {
1299
+ "tsconfig.json": {
1300
+ contents: {
1301
+ config: {
1302
+ compilerOptions: expect.objectContaining({
1303
+ types: ["@cloudflare/workers-types"],
1304
+ }),
1305
+ },
1306
+ error: undefined,
1307
+ },
1308
+ },
1309
+ },
1310
+ });
1311
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1312
+ "@cloudflare/workers-types",
1313
+ "typescript"
1314
+ );
1315
+ expect(std).toMatchInlineSnapshot(`
1316
+ Object {
1317
+ "debug": "",
1318
+ "err": "",
1319
+ "out": "✨ Created wrangler.toml
1320
+ ✨ Created package.json
1321
+ ✨ Created tsconfig.json
1322
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1323
+ "warn": "",
1324
+ }
1325
+ `);
1326
+ });
1327
+
1328
+ it("should not touch an existing tsconfig.json in the same directory", async () => {
1329
+ mockConfirm({
1330
+ text: "Would you like to use git to manage this Worker?",
1331
+ result: false,
1332
+ });
1333
+ mockSelect({
1334
+ text: "Would you like to create a Worker at src/index.ts?",
1335
+ result: "fetch",
1336
+ });
1337
+ writeFiles({
1338
+ items: {
1339
+ "package.json": {
1340
+ contents: {
1341
+ name: "test",
1342
+ version: "1.0.0",
1343
+ devDependencies: {
1344
+ wrangler: "0.0.0",
1345
+ "@cloudflare/workers-types": "0.0.0",
1346
+ },
1347
+ },
1348
+ },
1349
+ "tsconfig.json": { contents: { compilerOptions: {} } },
1350
+ },
1351
+ });
1352
+
1353
+ await runWrangler("init");
1354
+
1355
+ checkFiles({
1356
+ items: {
1357
+ "tsconfig.json": {
1358
+ contents: { config: { compilerOptions: {} }, error: undefined },
1359
+ },
1360
+ },
1361
+ });
1362
+ expect(std).toMatchInlineSnapshot(`
1363
+ Object {
1364
+ "debug": "",
1365
+ "err": "",
1366
+ "out": "✨ Created wrangler.toml
1367
+ ✨ Created src/index.ts
1368
+
1369
+ To start developing your Worker, run \`npx wrangler dev\`
1370
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1371
+ "warn": "",
1372
+ }
1373
+ `);
1374
+ });
1375
+
1376
+ it("should not touch an existing tsconfig.json in the ancestor of a target directory, if a name is passed", async () => {
1377
+ mockConfirm(
1378
+ {
1379
+ text: "Would you like to use git to manage this Worker?",
1380
+ result: false,
1381
+ },
1382
+ {
1383
+ text: "No package.json found. Would you like to create one?",
1384
+ result: true,
1385
+ },
1386
+ {
1387
+ text: "Would you like to use TypeScript?",
1388
+ result: true,
1389
+ }
1390
+ );
1391
+ mockSelect({
1392
+ text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
1393
+ result: "fetch",
1394
+ });
1395
+ writeFiles({
1396
+ items: {
1397
+ "path/to/worker/package.json": {
1398
+ contents: {
1399
+ name: "test",
1400
+ version: "1.0.0",
1401
+ devDependencies: {
1402
+ wrangler: "0.0.0",
1403
+ "@cloudflare/workers-types": "0.0.0",
1404
+ },
1405
+ },
1406
+ },
1407
+ "path/to/worker/tsconfig.json": { contents: { compilerOptions: {} } },
1408
+ },
1409
+ });
1410
+
1411
+ await runWrangler("init path/to/worker/my-worker");
1412
+
1413
+ checkFiles({
1414
+ items: {
1415
+ "path/to/worker/tsconfig.json": {
1416
+ contents: { config: { compilerOptions: {}, error: undefined } },
1417
+ },
1418
+ },
1419
+ });
1420
+ expect(std).toMatchInlineSnapshot(`
1421
+ Object {
1422
+ "debug": "",
1423
+ "err": "",
1424
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1425
+ ✨ Created path/to/worker/my-worker/package.json
1426
+ ✨ Created path/to/worker/my-worker/tsconfig.json
1427
+ ✨ Created path/to/worker/my-worker/src/index.ts
1428
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1429
+
1430
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
1431
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1432
+ "warn": "",
1433
+ }
1434
+ `);
1435
+ });
1436
+
1437
+ it("should offer to install type definitions in an existing typescript project", async () => {
1438
+ mockConfirm(
1439
+ {
1440
+ text: "Would you like to use git to manage this Worker?",
1441
+ result: false,
1442
+ },
1443
+ {
1444
+ text: "Would you like to install wrangler into package.json?",
1445
+ result: false,
1446
+ },
1447
+ {
1448
+ text: "Would you like to install the type definitions for Workers into your package.json?",
1449
+ result: true,
1450
+ }
1451
+ );
1452
+ mockSelect({
1453
+ text: "Would you like to create a Worker at src/index.ts?",
1454
+ result: "none",
1455
+ });
1456
+ writeFiles({
1457
+ items: {
1458
+ "./package.json": {
1459
+ contents: {
1460
+ name: "test",
1461
+ version: "1.0.0",
1462
+ },
1463
+ },
1464
+ "./tsconfig.json": { contents: { compilerOptions: {} } },
1465
+ },
1466
+ });
1467
+
1468
+ await runWrangler("init");
1469
+
1470
+ checkFiles({
1471
+ items: {
1472
+ // unchanged tsconfig
1473
+ "tsconfig.json": { contents: { config: { compilerOptions: {} } } },
1474
+ },
1475
+ });
1476
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1477
+ "@cloudflare/workers-types"
1478
+ );
1479
+ expect(std).toMatchInlineSnapshot(`
1480
+ Object {
1481
+ "debug": "",
1482
+ "err": "",
1483
+ "out": "✨ Created wrangler.toml
1484
+ ✨ Installed @cloudflare/workers-types into devDependencies
1485
+ 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1486
+ "warn": "",
1487
+ }
1488
+ `);
1489
+ });
1490
+
1491
+ it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1492
+ mockConfirm({
1493
+ text: "Would you like to use git to manage this Worker?",
1494
+ result: false,
1495
+ });
1496
+ mockSelect({
1497
+ text: "Would you like to create a Worker at src/index.ts?",
1498
+ result: "fetch",
1499
+ });
1500
+ writeFiles({
1501
+ items: {
1502
+ "package.json": {
1503
+ contents: {
1504
+ name: "test",
1505
+ version: "1.0.0",
1506
+ devDependencies: {
1507
+ wrangler: "0.0.0",
1508
+ "@cloudflare/workers-types": "0.0.0",
1509
+ },
1510
+ },
1511
+ },
1512
+ "tsconfig.json": { contents: { compilerOptions: {} } },
1513
+ },
1514
+ });
1515
+ setWorkingDirectory("./sub-1/sub-2");
1516
+
1517
+ await runWrangler("init");
1518
+
1519
+ checkFiles({
1520
+ items: {
1521
+ "tsconfig.json": false,
1522
+ "../../tsconfig.json": {
1523
+ contents: { config: { compilerOptions: {} } },
1524
+ },
1525
+ },
1526
+ });
1527
+ expect(std).toMatchInlineSnapshot(`
1528
+ Object {
1529
+ "debug": "",
1530
+ "err": "",
1531
+ "out": "✨ Created wrangler.toml
1532
+ ✨ Created src/index.ts
1533
+
1534
+ To start developing your Worker, run \`npx wrangler dev\`
1535
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1536
+ "warn": "",
1537
+ }
1538
+ `);
1539
+ });
1540
+ });
1541
+
1542
+ describe("javascript", () => {
1543
+ it("should add missing scripts for a non-ts project with .js extension", async () => {
1544
+ mockConfirm(
1545
+ {
1546
+ text: "Would you like to use git to manage this Worker?",
1547
+ result: false,
1548
+ },
1549
+ {
1550
+ text: "No package.json found. Would you like to create one?",
1551
+ result: true,
1552
+ },
1553
+ {
1554
+ text: "Would you like to install wrangler into package.json?",
1555
+ result: false,
1556
+ },
1557
+ {
1558
+ text: "Would you like to use TypeScript?",
1559
+ result: false,
1560
+ }
1561
+ );
1562
+ mockSelect({
1563
+ text: "Would you like to create a Worker at src/index.js?",
1564
+ result: "fetch",
1565
+ });
1566
+
1567
+ await runWrangler("init");
1568
+
1569
+ checkFiles({
1570
+ items: {
1571
+ "src/index.js": true,
1572
+ "src/index.ts": false,
1573
+ "package.json": {
1574
+ contents: expect.objectContaining({
1575
+ name: expect.stringContaining("wrangler-tests"),
1576
+ version: "0.0.0",
1577
+ scripts: {
1578
+ start: "wrangler dev",
1579
+ deploy: "wrangler publish",
1580
+ },
1581
+ }),
1582
+ },
1583
+ },
1584
+ });
1585
+ expect(std.out).toMatchInlineSnapshot(`
1586
+ "✨ Created wrangler.toml
1587
+ ✨ Created package.json
1588
+ ✨ Created src/index.js
1589
+
1590
+ To start developing your Worker, run \`npm start\`
1591
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1592
+ `);
1593
+ });
1594
+
1595
+ it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
1596
+ mockConfirm(
1597
+ {
1598
+ text: "Would you like to use git to manage this Worker?",
1599
+ result: false,
1600
+ },
1601
+ {
1602
+ text: "Would you like to install wrangler into package.json?",
1603
+ result: false,
1604
+ },
1605
+ {
1606
+ text: "Would you like to use TypeScript?",
1607
+ result: false,
1608
+ }
1609
+ );
1610
+ mockSelect({
1611
+ text: "Would you like to create a Worker at src/index.js?",
1612
+ result: "fetch",
1613
+ });
1614
+ writeFiles({
1615
+ items: {
1616
+ "package.json": {
1617
+ contents: {
1618
+ scripts: {
1619
+ start: "test-start",
1620
+ deploy: "test-publish",
1621
+ },
1622
+ },
1623
+ },
1624
+ },
1625
+ });
1626
+
1627
+ await runWrangler("init");
1628
+
1629
+ checkFiles({
1630
+ items: {
1631
+ "src/index.js": true,
1632
+ "src/index.ts": false,
1633
+ "package.json": {
1634
+ contents: expect.objectContaining({
1635
+ scripts: {
1636
+ start: "test-start",
1637
+ deploy: "test-publish",
1638
+ },
1639
+ }),
1640
+ },
1641
+ },
1642
+ });
1643
+ expect(std.out).toMatchInlineSnapshot(`
1644
+ "✨ Created wrangler.toml
1645
+ ✨ Created src/index.js
1646
+
1647
+ To start developing your Worker, run \`npx wrangler dev\`
1648
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1649
+ `);
1650
+ });
1651
+
1652
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
1653
+ mockConfirm(
1654
+ {
1655
+ text: "Would you like to use git to manage this Worker?",
1656
+ result: false,
1657
+ },
1658
+ {
1659
+ text: "Would you like to install wrangler into package.json?",
1660
+ result: false,
1661
+ },
1662
+ {
1663
+ text: "Would you like to use TypeScript?",
1664
+ result: false,
1665
+ }
1666
+ );
1667
+ const PLACEHOLDER = "/* placeholder text */";
1668
+ writeFiles({
1669
+ items: {
1670
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1671
+ "src/index.js": { contents: PLACEHOLDER },
1672
+ },
1673
+ });
1674
+
1675
+ await runWrangler("init");
1676
+
1677
+ checkFiles({
1678
+ items: {
1679
+ "src/index.js": { contents: PLACEHOLDER },
1680
+ "src/index.ts": false,
1681
+ },
1682
+ });
1683
+ expect(std).toMatchInlineSnapshot(`
1684
+ Object {
1685
+ "debug": "",
1686
+ "err": "",
1687
+ "out": "✨ Created wrangler.toml",
1688
+ "warn": "",
1689
+ }
1690
+ `);
1691
+ });
1692
+
1693
+ it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1694
+ mockConfirm(
1695
+ {
1696
+ text: "Would you like to use git to manage this Worker?",
1697
+ result: false,
1698
+ },
1699
+ {
1700
+ text: "Would you like to install wrangler into my-worker/package.json?",
1701
+ result: false,
1702
+ },
1703
+ {
1704
+ text: "Would you like to use TypeScript?",
1705
+ result: false,
1706
+ }
1707
+ );
1708
+ const PLACEHOLDER = "/* placeholder text */";
1709
+ writeFiles({
1710
+ items: {
1711
+ "my-worker/package.json": {
1712
+ contents: { name: "test", version: "1.0.0" },
1713
+ },
1714
+ "my-worker/src/index.js": { contents: PLACEHOLDER },
1715
+ },
1716
+ });
1717
+
1718
+ await runWrangler("init my-worker");
1719
+
1720
+ checkFiles({
1721
+ items: {
1722
+ "my-worker/src/index.js": { contents: PLACEHOLDER },
1723
+ "my-worker/src/index.ts": false,
1724
+ },
1725
+ });
1726
+ expect(std).toMatchInlineSnapshot(`
1727
+ Object {
1728
+ "debug": "",
1729
+ "err": "",
1730
+ "out": "✨ Created my-worker/wrangler.toml",
1731
+ "warn": "",
1732
+ }
1733
+ `);
1734
+ });
1735
+ });
1736
+
1737
+ describe("worker names", () => {
1738
+ it("should create a worker with a given name", async () => {
1739
+ await runWrangler("init my-worker -y");
1740
+
1741
+ checkFiles({
1742
+ items: {
1743
+ "my-worker/wrangler.toml": wranglerToml({
1744
+ ...MINIMAL_WRANGLER_TOML,
1745
+ name: "my-worker",
1746
+ }),
1747
+ },
1748
+ });
1749
+ });
1750
+
1751
+ it('should create a worker with the name of the current directory if "name" is .', async () => {
1752
+ await runWrangler("init . -y");
1753
+
1754
+ const workerName = path.basename(process.cwd()).toLowerCase();
1755
+ checkFiles({
1756
+ items: {
1757
+ "wrangler.toml": wranglerToml({
1758
+ ...MINIMAL_WRANGLER_TOML,
1759
+ name: workerName,
1760
+ }),
1761
+ },
1762
+ });
1763
+ expect(std).toMatchInlineSnapshot(`
1764
+ Object {
1765
+ "debug": "",
1766
+ "err": "",
1767
+ "out": "✨ Created wrangler.toml
1768
+ ✨ Initialized git repository
1769
+ ✨ Created package.json
1770
+ ✨ Created tsconfig.json
1771
+ ✨ Created src/index.ts
1772
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1773
+
1774
+ To start developing your Worker, run \`npm start\`
1775
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1776
+ "warn": "",
1777
+ }
1778
+ `);
1779
+ });
1780
+
1781
+ it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
1782
+ await runWrangler("init path/to/worker -y");
1783
+
1784
+ checkFiles({
1785
+ items: {
1786
+ "path/to/worker/wrangler.toml": wranglerToml({
1787
+ ...MINIMAL_WRANGLER_TOML,
1788
+ name: "worker",
1789
+ }),
1790
+ },
1791
+ });
1792
+ expect(std).toMatchInlineSnapshot(`
1793
+ Object {
1794
+ "debug": "",
1795
+ "err": "",
1796
+ "out": "✨ Created path/to/worker/wrangler.toml
1797
+ ✨ Initialized git repository at path/to/worker
1798
+ ✨ Created path/to/worker/package.json
1799
+ ✨ Created path/to/worker/tsconfig.json
1800
+ ✨ Created path/to/worker/src/index.ts
1801
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1802
+
1803
+ To start developing your Worker, run \`cd path/to/worker && npm start\`
1804
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1805
+ "warn": "",
1806
+ }
1807
+ `);
1808
+ });
1809
+
1810
+ it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
1811
+ await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
1812
+
1813
+ checkFiles({
1814
+ items: {
1815
+ "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml": wranglerToml({
1816
+ ...MINIMAL_WRANGLER_TOML,
1817
+ name: "weird_w0rkr_n4m3-js-tsx-tar-gz",
1818
+ }),
1819
+ },
1820
+ });
1821
+ expect(std).toMatchInlineSnapshot(`
1822
+ Object {
1823
+ "debug": "",
1824
+ "err": "",
1825
+ "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1826
+ ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1827
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1828
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
1829
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1830
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1831
+
1832
+ To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1833
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1834
+ "warn": "",
1835
+ }
1836
+ `);
1837
+ });
1838
+
1839
+ it("should ignore ancestor files (such as wrangler.toml, package.json and tsconfig.json) if a name/path is given", async () => {
1840
+ mockConfirm(
1841
+ {
1842
+ text: "Would you like to use git to manage this Worker?",
1843
+ result: false,
1844
+ },
1845
+ {
1846
+ text: "Would you like to use TypeScript?",
1847
+ result: true,
1848
+ },
1849
+ {
1850
+ text: "No package.json found. Would you like to create one?",
1851
+ result: true,
1852
+ },
1853
+ {
1854
+ text: "Would you like to install the type definitions for Workers into your package.json?",
1855
+ result: true,
1856
+ }
1857
+ );
1858
+ mockSelect({
1859
+ text: "Would you like to create a Worker at sub/folder/worker/src/index.ts?",
1860
+ result: "fetch",
1861
+ });
1862
+ writeFiles({
1863
+ items: {
1864
+ "package.json": { contents: { name: "top-level" } },
1865
+ "tsconfig.json": { contents: { compilerOptions: {} } },
1866
+ "wrangler.toml": wranglerToml({
1867
+ name: "top-level",
1868
+ compatibility_date: "some-date",
1869
+ }),
1870
+ },
1871
+ });
1872
+
1873
+ await runWrangler("init sub/folder/worker");
1874
+
1875
+ // Ancestor files are untouched.
1876
+ checkFiles({
1877
+ items: {
1878
+ "package.json": { contents: { name: "top-level" } },
1879
+ "tsconfig.json": {
1880
+ contents: { config: { compilerOptions: {} }, error: undefined },
1881
+ },
1882
+ "wrangler.toml": wranglerToml({
1883
+ name: "top-level",
1884
+ compatibility_date: "some-date",
1885
+ }),
1886
+ },
1887
+ });
1888
+ // New initialized Worker has its own files.
1889
+ checkFiles({
1890
+ items: {
1891
+ "sub/folder/worker/package.json": {
1892
+ contents: expect.objectContaining({
1893
+ name: "worker",
1894
+ }),
1895
+ },
1896
+ "sub/folder/worker/tsconfig.json": true,
1897
+ "sub/folder/worker/wrangler.toml": wranglerToml({
1898
+ ...MINIMAL_WRANGLER_TOML,
1899
+ name: "worker",
1900
+ }),
1901
+ },
1902
+ });
1903
+ });
1904
+ });
1905
+
1906
+ describe("from dashboard", () => {
1907
+ mockApiToken();
1908
+ mockAccountId({ accountId: "LCARS" });
1909
+ afterEach(() => {
1910
+ unsetAllMocks();
1911
+ });
1912
+ const mockDashboardScript = `
1913
+ export default {
1914
+ async fetch(request, env, ctx) {
1915
+ return new Response("Hello World!");
1916
+ },
1917
+ };
1918
+ `;
1919
+
1920
+ //TODO: Tests for a case when a worker name doesn't exist - JACOB & CASS
1921
+ it("should download source script from dashboard w/ positional <name> in TypeScript project", async () => {
1922
+ setMockFetchDashScript({
1923
+ accountId: "LCARS",
1924
+ fromDashScriptName: "memory-crystal",
1925
+ mockResponse: mockDashboardScript,
1926
+ });
1927
+ mockConfirm(
1928
+ {
1929
+ text: "Would you like to use git to manage this Worker?",
1930
+ result: false,
1931
+ },
1932
+ {
1933
+ text: "Would you like to use TypeScript?",
1934
+ result: true,
1935
+ },
1936
+ {
1937
+ text: "No package.json found. Would you like to create one?",
1938
+ result: true,
1939
+ },
1940
+ {
1941
+ text: "Would you like to install the type definitions for Workers into your package.json?",
1942
+ result: true,
1943
+ }
1944
+ );
1945
+
1946
+ await runWrangler(
1947
+ "init isolinear-optical-chip --from-dash memory-crystal"
1948
+ );
1949
+
1950
+ checkFiles({
1951
+ items: {
1952
+ "isolinear-optical-chip/src/index.js": false,
1953
+ "isolinear-optical-chip/src/index.ts": {
1954
+ contents: mockDashboardScript,
1955
+ },
1956
+ "isolinear-optical-chip/package.json": {
1957
+ contents: expect.objectContaining({
1958
+ name: "isolinear-optical-chip",
1959
+ }),
1960
+ },
1961
+ "isolinear-optical-chip/tsconfig.json": true,
1962
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
1963
+ ...MINIMAL_WRANGLER_TOML,
1964
+ name: "isolinear-optical-chip",
1965
+ }),
1966
+ },
1967
+ });
1968
+ });
1969
+
1970
+ it("should download source script from dashboard w/ out positional <name>", async () => {
1971
+ setMockFetchDashScript({
1972
+ accountId: "LCARS",
1973
+ fromDashScriptName: "isolinear-optical-chip",
1974
+ mockResponse: mockDashboardScript,
1975
+ });
1976
+ mockConfirm(
1977
+ {
1978
+ text: "Would you like to use git to manage this Worker?",
1979
+ result: false,
1980
+ },
1981
+ {
1982
+ text: "Would you like to use TypeScript?",
1983
+ result: true,
1984
+ },
1985
+ {
1986
+ text: "No package.json found. Would you like to create one?",
1987
+ result: true,
1988
+ },
1989
+ {
1990
+ text: "Would you like to install the type definitions for Workers into your package.json?",
1991
+ result: true,
1992
+ }
1993
+ );
1994
+
1995
+ await runWrangler("init --from-dash isolinear-optical-chip");
1996
+
1997
+ checkFiles({
1998
+ items: {
1999
+ "isolinear-optical-chip/src/index.js": false,
2000
+ "isolinear-optical-chip/src/index.ts": {
2001
+ contents: mockDashboardScript,
2002
+ },
2003
+ "isolinear-optical-chip/package.json": {
2004
+ contents: expect.objectContaining({
2005
+ name: "isolinear-optical-chip",
2006
+ }),
2007
+ },
2008
+ "isolinear-optical-chip/tsconfig.json": true,
2009
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2010
+ ...MINIMAL_WRANGLER_TOML,
2011
+ name: "isolinear-optical-chip",
2012
+ }),
2013
+ },
2014
+ });
2015
+ });
2016
+
2017
+ it("should download source script from dashboard as plain JavaScript", async () => {
2018
+ setMockFetchDashScript({
2019
+ accountId: "LCARS",
2020
+ fromDashScriptName: "isolinear-optical-chip",
2021
+ mockResponse: mockDashboardScript,
2022
+ });
2023
+ mockConfirm(
2024
+ {
2025
+ text: "Would you like to use git to manage this Worker?",
2026
+ result: false,
2027
+ },
2028
+ {
2029
+ text: "Would you like to use TypeScript?",
2030
+ result: false,
2031
+ },
2032
+ {
2033
+ text: "No package.json found. Would you like to create one?",
2034
+ result: true,
2035
+ },
2036
+ {
2037
+ text: "Would you like to install the type definitions for Workers into your package.json?",
2038
+ result: true,
2039
+ }
2040
+ );
2041
+
2042
+ await runWrangler("init --from-dash isolinear-optical-chip");
2043
+
2044
+ checkFiles({
2045
+ items: {
2046
+ "isolinear-optical-chip/src/index.js": {
2047
+ contents: mockDashboardScript,
2048
+ },
2049
+ "isolinear-optical-chip/src/index.ts": false,
2050
+ "isolinear-optical-chip/package.json": {
2051
+ contents: expect.objectContaining({
2052
+ name: "isolinear-optical-chip",
2053
+ }),
2054
+ },
2055
+ "isolinear-optical-chip/tsconfig.json": false,
2056
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2057
+ ...MINIMAL_WRANGLER_TOML,
2058
+ name: "isolinear-optical-chip",
2059
+ }),
2060
+ },
2061
+ });
2062
+ });
2063
+
2064
+ it("should not continue if no worker name is provided", async () => {
2065
+ await expect(
2066
+ runWrangler("init --from-dash")
2067
+ ).rejects.toMatchInlineSnapshot(
2068
+ `[YError: Not enough arguments following: from-dash]`
2069
+ );
2070
+ checkFiles({
2071
+ items: {
2072
+ "isolinear-optical-chip/src/index.js": false,
2073
+ "isolinear-optical-chip/src/index.ts": false,
2074
+ "isolinear-optical-chip/package.json": false,
2075
+ "isolinear-optical-chip/tsconfig.json": false,
2076
+ "isolinear-optical-chip/wrangler.toml": false,
2077
+ },
2078
+ });
2079
+ });
2080
+ });
1634
2081
  });
2082
+
2083
+ /**
2084
+ * Change the current working directory, ensuring that this exists.
2085
+ */
2086
+ function setWorkingDirectory(directory: string) {
2087
+ fs.mkdirSync(directory, { recursive: true });
2088
+ process.chdir(directory);
2089
+ }
2090
+
2091
+ /**
2092
+ * Write the given folder/files to disk at the given `cwd`.
2093
+ */
2094
+ function writeFiles(folder: TestFolder, cwd = process.cwd()) {
2095
+ for (const name in folder.items) {
2096
+ const item = folder.items[name];
2097
+ const itemPath = path.resolve(cwd, name);
2098
+ if (typeof item !== "boolean") {
2099
+ if ("contents" in item) {
2100
+ fs.mkdirSync(path.dirname(itemPath), { recursive: true });
2101
+ fs.writeFileSync(itemPath, stringify(name, item.contents));
2102
+ } else {
2103
+ fs.mkdirSync(itemPath, { recursive: true });
2104
+ writeFiles(item, itemPath);
2105
+ }
2106
+ } else {
2107
+ throw new Error("Cannot write boolean flags to disk: " + itemPath);
2108
+ }
2109
+ }
2110
+ }
2111
+
2112
+ /**
2113
+ * Check that the given test folders/files match what is in on disk.
2114
+ */
2115
+ function checkFiles(folder: TestFolder, cwd = process.cwd()) {
2116
+ for (const name in folder.items) {
2117
+ const item = folder.items[name];
2118
+ const itemPath = path.resolve(cwd, name);
2119
+ if (typeof item === "boolean") {
2120
+ if (fs.existsSync(itemPath) !== item) {
2121
+ throw new Error(`Expected ${itemPath} ${item ? "" : "not "}to exist.`);
2122
+ }
2123
+ } else if ("contents" in item) {
2124
+ const actualContents = parse(name, fs.readFileSync(itemPath, "utf-8"));
2125
+ expect(actualContents).toEqual(item.contents);
2126
+ } else if ("items" in item) {
2127
+ checkFiles(item, itemPath);
2128
+ } else {
2129
+ throw new Error("Unexpected TestFile object.");
2130
+ }
2131
+ }
2132
+ }
2133
+
2134
+ function stringify(name: string, value: unknown) {
2135
+ if (name.endsWith(".toml")) {
2136
+ return TOML.stringify(value as TOML.JsonMap);
2137
+ }
2138
+ if (name.endsWith(".json")) {
2139
+ return JSON.stringify(value);
2140
+ }
2141
+ return `${value}`;
2142
+ }
2143
+
2144
+ function parse(name: string, value: string): unknown {
2145
+ if (name.endsWith(".toml")) {
2146
+ return TOML.parse(value);
2147
+ }
2148
+ if (name.endsWith("tsconfig.json")) {
2149
+ return parseConfigFileTextToJson(name, value);
2150
+ }
2151
+ if (name.endsWith(".json")) {
2152
+ return JSON.parse(value);
2153
+ }
2154
+ return value;
2155
+ }
2156
+
2157
+ function wranglerToml(options: TOML.JsonMap = {}): TestFile {
2158
+ return {
2159
+ contents: options,
2160
+ };
2161
+ }
2162
+
2163
+ interface TestFile {
2164
+ contents: unknown;
2165
+ }
2166
+ interface TestFolder {
2167
+ items: {
2168
+ [id: string]: TestFile | TestFolder | boolean;
2169
+ };
2170
+ }