wrangler 0.0.0-e88a54e → 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 (222) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +51 -14
  3. package/bin/wrangler.js +161 -29
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +5484 -21
  6. package/package.json +173 -143
  7. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  8. package/src/__tests__/config-cache.test.ts +42 -0
  9. package/src/__tests__/configuration.test.ts +4016 -2786
  10. package/src/__tests__/dev.test.tsx +1305 -483
  11. package/src/__tests__/generate.test.ts +91 -0
  12. package/src/__tests__/guess-worker-format.test.ts +75 -48
  13. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  14. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  15. package/src/__tests__/helpers/mock-bin.ts +20 -20
  16. package/src/__tests__/helpers/mock-cfetch.ts +182 -93
  17. package/src/__tests__/helpers/mock-console.ts +59 -32
  18. package/src/__tests__/helpers/mock-dialogs.ts +121 -33
  19. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  20. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  21. package/src/__tests__/helpers/mock-istty.ts +74 -0
  22. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  23. package/src/__tests__/helpers/mock-kv.ts +26 -26
  24. package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
  25. package/src/__tests__/helpers/mock-process.ts +39 -0
  26. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  27. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  28. package/src/__tests__/helpers/run-in-tmp.ts +28 -22
  29. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  30. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  31. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  32. package/src/__tests__/https-options.test.ts +144 -108
  33. package/src/__tests__/index.test.ts +229 -975
  34. package/src/__tests__/init.test.ts +2170 -0
  35. package/src/__tests__/jest.setup.ts +122 -34
  36. package/src/__tests__/kv.test.ts +1376 -1087
  37. package/src/__tests__/logger.test.ts +141 -0
  38. package/src/__tests__/metrics.test.ts +450 -0
  39. package/src/__tests__/package-manager.test.ts +312 -165
  40. package/src/__tests__/pages.test.ts +1270 -39
  41. package/src/__tests__/parse.test.ts +253 -245
  42. package/src/__tests__/publish.test.ts +6878 -2627
  43. package/src/__tests__/pubsub.test.ts +367 -0
  44. package/src/__tests__/r2.test.ts +328 -190
  45. package/src/__tests__/route.test.ts +22 -22
  46. package/src/__tests__/secret.test.ts +474 -407
  47. package/src/__tests__/tail.test.ts +679 -441
  48. package/src/__tests__/test-old-node-version.js +31 -0
  49. package/src/__tests__/user.test.ts +155 -0
  50. package/src/__tests__/validate-dev-props.test.ts +56 -0
  51. package/src/__tests__/version.test.ts +35 -0
  52. package/src/__tests__/whoami.test.tsx +276 -87
  53. package/src/__tests__/worker-namespace.test.ts +329 -0
  54. package/src/abort.d.ts +3 -0
  55. package/src/api/dev.ts +93 -0
  56. package/src/api/index.ts +1 -0
  57. package/src/bundle-reporter.tsx +29 -0
  58. package/src/bundle.ts +441 -110
  59. package/src/cfetch/index.ts +83 -66
  60. package/src/cfetch/internal.ts +251 -92
  61. package/src/cli.ts +22 -14
  62. package/src/config/README.md +1 -1
  63. package/src/config/config.ts +224 -179
  64. package/src/config/diagnostics.ts +61 -61
  65. package/src/config/environment.ts +430 -311
  66. package/src/config/index.ts +227 -38
  67. package/src/config/validation-helpers.ts +414 -303
  68. package/src/config/validation.ts +1745 -973
  69. package/src/config-cache.ts +85 -0
  70. package/src/create-worker-preview.ts +209 -118
  71. package/src/create-worker-upload-form.ts +275 -201
  72. package/src/dev/dev-vars.ts +36 -0
  73. package/src/dev/dev.tsx +488 -307
  74. package/src/dev/local.tsx +427 -210
  75. package/src/dev/remote.tsx +407 -188
  76. package/src/dev/use-esbuild.ts +156 -78
  77. package/src/dev/validate-dev-props.ts +31 -0
  78. package/src/dev-registry.tsx +160 -0
  79. package/src/dev.tsx +696 -0
  80. package/src/dialogs.tsx +115 -57
  81. package/src/dispatch-namespace.ts +206 -0
  82. package/src/durable.ts +102 -0
  83. package/src/entry.ts +267 -147
  84. package/src/environment-variables.ts +25 -23
  85. package/src/errors.ts +11 -0
  86. package/src/faye-websocket.d.ts +6 -0
  87. package/src/generate.ts +131 -0
  88. package/src/git-client.ts +42 -0
  89. package/src/global-wrangler-config-path.ts +26 -0
  90. package/src/https-options.ts +85 -80
  91. package/src/index.tsx +2191 -2304
  92. package/src/init.ts +622 -0
  93. package/src/inspect.ts +687 -557
  94. package/src/intl-polyfill.d.ts +139 -0
  95. package/src/is-ci.ts +14 -0
  96. package/src/is-interactive.ts +12 -0
  97. package/src/kv.ts +353 -242
  98. package/src/logger.ts +73 -0
  99. package/src/metrics/index.ts +5 -0
  100. package/src/metrics/metrics-config.ts +239 -0
  101. package/src/metrics/metrics-dispatcher.ts +96 -0
  102. package/src/metrics/metrics-usage-headers.ts +24 -0
  103. package/src/metrics/send-event.ts +92 -0
  104. package/src/miniflare-cli/assets.ts +554 -0
  105. package/src/miniflare-cli/enum-keys.ts +8 -8
  106. package/src/miniflare-cli/index.ts +199 -29
  107. package/src/miniflare-cli/request-context.ts +40 -0
  108. package/src/module-collection.ts +213 -213
  109. package/src/open-in-browser.ts +8 -16
  110. package/src/package-manager.ts +183 -90
  111. package/src/pages/build.tsx +238 -0
  112. package/src/pages/constants.ts +12 -0
  113. package/src/pages/deployments.tsx +106 -0
  114. package/src/pages/dev.tsx +528 -0
  115. package/src/pages/errors.ts +22 -0
  116. package/src/pages/functions/buildPlugin.ts +109 -0
  117. package/src/pages/functions/buildWorker.ts +155 -0
  118. package/src/pages/functions/filepath-routing.test.ts +234 -0
  119. package/src/pages/functions/filepath-routing.ts +189 -0
  120. package/src/pages/functions/identifiers.ts +78 -0
  121. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  122. package/src/pages/functions/routes-consolidation.ts +73 -0
  123. package/src/pages/functions/routes-transformation.test.ts +271 -0
  124. package/src/pages/functions/routes-transformation.ts +122 -0
  125. package/src/pages/functions/routes.ts +151 -0
  126. package/src/pages/functions.tsx +96 -0
  127. package/src/pages/index.tsx +94 -0
  128. package/src/pages/projects.tsx +169 -0
  129. package/src/pages/publish.tsx +391 -0
  130. package/src/pages/types.ts +49 -0
  131. package/src/pages/upload.tsx +413 -0
  132. package/src/pages/utils.ts +12 -0
  133. package/src/parse.ts +224 -121
  134. package/src/paths.ts +6 -6
  135. package/src/preview.ts +31 -0
  136. package/src/proxy.ts +416 -370
  137. package/src/publish.ts +796 -321
  138. package/src/pubsub/index.ts +286 -0
  139. package/src/pubsub/pubsub-commands.tsx +623 -0
  140. package/src/r2.ts +100 -19
  141. package/src/selfsigned.d.ts +23 -23
  142. package/src/sites.tsx +281 -189
  143. package/src/tail/filters.ts +111 -109
  144. package/src/tail/index.ts +230 -217
  145. package/src/tail/printing.ts +92 -47
  146. package/src/update-check.ts +19 -0
  147. package/src/user/choose-account.tsx +69 -0
  148. package/src/user/env-vars.ts +46 -0
  149. package/src/user/generate-auth-url.ts +33 -0
  150. package/src/user/generate-random-state.ts +16 -0
  151. package/src/user/index.ts +3 -0
  152. package/src/user/user.tsx +1191 -0
  153. package/src/whoami.tsx +142 -42
  154. package/src/worker.ts +135 -94
  155. package/src/zones.ts +167 -0
  156. package/templates/checked-fetch.js +17 -0
  157. package/templates/first-party-worker-module-facade.ts +18 -0
  158. package/templates/format-dev-errors.ts +32 -0
  159. package/templates/gitignore +171 -0
  160. package/templates/new-worker-scheduled.js +17 -0
  161. package/templates/new-worker-scheduled.ts +32 -0
  162. package/templates/new-worker.js +3 -3
  163. package/templates/new-worker.ts +18 -3
  164. package/templates/no-op-worker.js +10 -0
  165. package/templates/pages-shim.ts +9 -0
  166. package/templates/pages-template-plugin.ts +167 -0
  167. package/templates/pages-template-worker.ts +172 -0
  168. package/templates/serve-static-assets.ts +63 -0
  169. package/templates/service-bindings-module-facade.js +54 -0
  170. package/templates/service-bindings-sw-facade.js +42 -0
  171. package/templates/tsconfig.json +95 -95
  172. package/wrangler-dist/cli.d.ts +338 -0
  173. package/wrangler-dist/cli.js +124611 -112403
  174. package/miniflare-dist/index.mjs.map +0 -7
  175. package/pages/functions/buildWorker.ts +0 -62
  176. package/pages/functions/filepath-routing.test.ts +0 -223
  177. package/pages/functions/filepath-routing.ts +0 -181
  178. package/pages/functions/identifiers.ts +0 -78
  179. package/pages/functions/routes.ts +0 -151
  180. package/pages/functions/template-worker.ts +0 -130
  181. package/src/__tests__/dev2.test.tsx +0 -154
  182. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  183. package/src/__tests__/logout.test.ts +0 -52
  184. package/src/__tests__/sentry.test.ts +0 -146
  185. package/src/pages.tsx +0 -1047
  186. package/src/reporting.ts +0 -131
  187. package/src/user.tsx +0 -1118
  188. package/templates/static-asset-facade.js +0 -47
  189. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  190. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  191. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  192. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  193. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  194. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  195. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  196. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  197. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  198. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  199. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  200. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  201. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  202. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  203. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  204. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  205. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  206. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  207. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  208. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  209. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  210. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  211. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  212. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  213. package/vendor/wrangler-mime/LICENSE +0 -21
  214. package/vendor/wrangler-mime/Mime.js +0 -97
  215. package/vendor/wrangler-mime/README.md +0 -187
  216. package/vendor/wrangler-mime/cli.js +0 -46
  217. package/vendor/wrangler-mime/index.js +0 -4
  218. package/vendor/wrangler-mime/lite.js +0 -4
  219. package/vendor/wrangler-mime/package.json +0 -52
  220. package/vendor/wrangler-mime/types/other.js +0 -1
  221. package/vendor/wrangler-mime/types/standard.js +0 -1
  222. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,2170 @@
1
+ import * as fs from "node:fs";
2
+ import path from "node:path";
3
+ import * as TOML from "@iarna/toml";
4
+ import { execa, execaSync } from "execa";
5
+ import { parseConfigFileTextToJson } from "typescript";
6
+ import { version as wranglerVersion } from "../../package.json";
7
+ import { getPackageManager } from "../package-manager";
8
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
9
+ import { setMockFetchDashScript, unsetAllMocks } from "./helpers/mock-cfetch";
10
+ import { mockConsoleMethods } from "./helpers/mock-console";
11
+ import {
12
+ mockConfirm,
13
+ clearConfirmMocks,
14
+ mockSelect,
15
+ clearSelectMocks,
16
+ } from "./helpers/mock-dialogs";
17
+ import { runInTempDir } from "./helpers/run-in-tmp";
18
+ import { runWrangler } from "./helpers/run-wrangler";
19
+ import type { PackageManager } from "../package-manager";
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
+
30
+ describe("init", () => {
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(`
162
+ "The --type option is no longer supported.
163
+ If you wish to use webpack then you will need to create a custom build."
164
+ `);
165
+ });
166
+
167
+ it("should error if `--site` is used", async () => {
168
+ await expect(runWrangler("init --site")).rejects
169
+ .toThrowErrorMatchingInlineSnapshot(`
170
+ "The --site option is no longer supported.
171
+ If you wish to create a brand new Worker Sites project then clone the \`worker-sites-template\` starter repository:
172
+
173
+ \`\`\`
174
+ git clone --depth=1 --branch=wrangler2 https://github.com/cloudflare/worker-sites-template my-site
175
+ cd my-site
176
+ \`\`\`
177
+
178
+ Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.
179
+ Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/."
180
+ `);
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
+ });
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
+ }