wrangler 0.0.0-e3e3243 → 0.0.0-e4047cab

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 (310) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -14
  3. package/bin/wrangler.js +166 -31
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +6263 -21
  6. package/package.json +191 -141
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +209 -0
  9. package/src/__tests__/api-devregistry.test.js +56 -0
  10. package/src/__tests__/api.test.ts +80 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +42 -0
  13. package/src/__tests__/configuration.test.ts +3971 -2810
  14. package/src/__tests__/d1.test.ts +80 -0
  15. package/src/__tests__/delete.test.ts +263 -0
  16. package/src/__tests__/deployments.test.ts +91 -0
  17. package/src/__tests__/dev.test.tsx +1521 -526
  18. package/src/__tests__/dialogs.test.tsx +40 -0
  19. package/src/__tests__/generate.test.ts +235 -0
  20. package/src/__tests__/get-host-from-url.test.ts +16 -0
  21. package/src/__tests__/guess-worker-format.test.ts +74 -57
  22. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  23. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  24. package/src/__tests__/helpers/mock-bin.ts +20 -20
  25. package/src/__tests__/helpers/mock-cfetch.ts +219 -93
  26. package/src/__tests__/helpers/mock-console.ts +68 -32
  27. package/src/__tests__/helpers/mock-dialogs.ts +121 -33
  28. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  29. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  30. package/src/__tests__/helpers/mock-istty.ts +74 -0
  31. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  32. package/src/__tests__/helpers/mock-kv.ts +27 -27
  33. package/src/__tests__/helpers/mock-oauth-flow.ts +256 -0
  34. package/src/__tests__/helpers/mock-process.ts +39 -0
  35. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  36. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  37. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  38. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  39. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  40. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  41. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  42. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  43. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  44. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  45. package/src/__tests__/helpers/msw/index.ts +21 -0
  46. package/src/__tests__/helpers/run-in-tmp.ts +28 -22
  47. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  48. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  49. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  50. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  51. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  52. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  53. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  54. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  55. package/src/__tests__/https-options.test.ts +144 -108
  56. package/src/__tests__/index.test.ts +248 -967
  57. package/src/__tests__/init.test.ts +2900 -0
  58. package/src/__tests__/jest.setup.ts +167 -34
  59. package/src/__tests__/kv.test.ts +1661 -1366
  60. package/src/__tests__/logger.test.ts +141 -0
  61. package/src/__tests__/logout.test.ts +36 -41
  62. package/src/__tests__/metrics.test.ts +450 -0
  63. package/src/__tests__/middleware.scheduled.test.ts +135 -0
  64. package/src/__tests__/middleware.test.ts +726 -0
  65. package/src/__tests__/package-manager.test.ts +312 -165
  66. package/src/__tests__/pages-deployment-tail.test.ts +820 -0
  67. package/src/__tests__/pages.test.ts +2437 -77
  68. package/src/__tests__/parse.test.ts +253 -245
  69. package/src/__tests__/paths.test.ts +39 -0
  70. package/src/__tests__/publish.test.ts +7569 -3445
  71. package/src/__tests__/pubsub.test.ts +370 -0
  72. package/src/__tests__/queues.test.ts +522 -0
  73. package/src/__tests__/r2.test.ts +388 -193
  74. package/src/__tests__/route.test.ts +22 -22
  75. package/src/__tests__/secret.test.ts +626 -407
  76. package/src/__tests__/tail.test.ts +779 -442
  77. package/src/__tests__/test-old-node-version.js +31 -0
  78. package/src/__tests__/type-generation.test.ts +226 -0
  79. package/src/__tests__/user.test.ts +129 -0
  80. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  81. package/src/__tests__/validate-dev-props.test.ts +56 -0
  82. package/src/__tests__/version.test.ts +35 -0
  83. package/src/__tests__/whoami.test.tsx +266 -88
  84. package/src/__tests__/worker-namespace.test.ts +326 -0
  85. package/src/abort.d.ts +3 -0
  86. package/src/api/dev.ts +210 -0
  87. package/src/api/index.ts +2 -0
  88. package/src/bundle-reporter.tsx +68 -0
  89. package/src/bundle.ts +791 -123
  90. package/src/cfetch/index.ts +123 -67
  91. package/src/cfetch/internal.ts +216 -92
  92. package/src/cli.ts +23 -14
  93. package/src/config/README.md +1 -1
  94. package/src/config/config.ts +236 -179
  95. package/src/config/diagnostics.ts +61 -61
  96. package/src/config/environment.ts +506 -316
  97. package/src/config/index.ts +310 -38
  98. package/src/config/validation-helpers.ts +385 -342
  99. package/src/config/validation.ts +1970 -1099
  100. package/src/config-cache.ts +85 -0
  101. package/src/create-worker-preview.ts +240 -124
  102. package/src/create-worker-upload-form.ts +300 -201
  103. package/src/d1/backups.tsx +223 -0
  104. package/src/d1/constants.ts +2 -0
  105. package/src/d1/create.tsx +63 -0
  106. package/src/d1/delete.tsx +56 -0
  107. package/src/d1/execute.tsx +352 -0
  108. package/src/d1/formatTimeAgo.ts +14 -0
  109. package/src/d1/index.ts +101 -0
  110. package/src/d1/list.tsx +48 -0
  111. package/src/d1/migrations/apply.tsx +184 -0
  112. package/src/d1/migrations/create.tsx +77 -0
  113. package/src/d1/migrations/helpers.ts +147 -0
  114. package/src/d1/migrations/index.tsx +3 -0
  115. package/src/d1/migrations/list.tsx +79 -0
  116. package/src/d1/migrations/options.ts +18 -0
  117. package/src/d1/options.ts +22 -0
  118. package/src/d1/types.tsx +24 -0
  119. package/src/d1/utils.ts +48 -0
  120. package/src/delete.ts +108 -0
  121. package/src/deployments.ts +77 -0
  122. package/src/deprecated/index.ts +438 -0
  123. package/src/dev/dev-vars.ts +39 -0
  124. package/src/dev/dev.tsx +550 -328
  125. package/src/dev/get-local-persistence-path.tsx +31 -0
  126. package/src/dev/local.tsx +871 -213
  127. package/src/dev/remote.tsx +592 -192
  128. package/src/dev/start-server.ts +539 -0
  129. package/src/dev/use-esbuild.ts +184 -84
  130. package/src/dev/validate-dev-props.ts +31 -0
  131. package/src/dev-registry.tsx +201 -0
  132. package/src/dev.tsx +904 -0
  133. package/src/dialogs.tsx +150 -59
  134. package/src/dispatch-namespace.ts +207 -0
  135. package/src/durable.ts +102 -0
  136. package/src/entry.ts +260 -171
  137. package/src/environment-variables.ts +40 -23
  138. package/src/errors.ts +11 -0
  139. package/src/git-client.ts +124 -0
  140. package/src/global-wrangler-config-path.ts +26 -0
  141. package/src/https-options.ts +85 -80
  142. package/src/index.tsx +639 -2341
  143. package/src/init.ts +970 -0
  144. package/src/inspect.ts +707 -557
  145. package/src/intl-polyfill.d.ts +123 -123
  146. package/src/is-ci.ts +14 -0
  147. package/src/is-interactive.ts +16 -0
  148. package/src/kv/helpers.ts +430 -0
  149. package/src/kv/index.ts +596 -0
  150. package/src/logger.ts +92 -0
  151. package/src/metrics/index.ts +5 -0
  152. package/src/metrics/metrics-config.ts +239 -0
  153. package/src/metrics/metrics-dispatcher.ts +96 -0
  154. package/src/metrics/metrics-usage-headers.ts +24 -0
  155. package/src/metrics/send-event.ts +96 -0
  156. package/src/miniflare-cli/assets.ts +220 -0
  157. package/src/miniflare-cli/index.ts +206 -30
  158. package/src/miniflare-cli/request-context.ts +40 -0
  159. package/src/module-collection.ts +222 -213
  160. package/src/open-in-browser.ts +8 -16
  161. package/src/package-manager.ts +183 -90
  162. package/src/pages/build.tsx +270 -0
  163. package/src/pages/constants.ts +18 -0
  164. package/src/pages/deployment-tails.tsx +284 -0
  165. package/src/pages/deployments.tsx +84 -0
  166. package/src/pages/dev.tsx +706 -0
  167. package/src/pages/errors.ts +66 -0
  168. package/src/pages/functions/buildPlugin.ts +107 -0
  169. package/src/pages/functions/buildWorker.ts +166 -0
  170. package/src/pages/functions/filepath-routing.test.ts +234 -0
  171. package/src/pages/functions/filepath-routing.ts +189 -0
  172. package/src/pages/functions/identifiers.ts +78 -0
  173. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  174. package/src/pages/functions/routes-consolidation.ts +73 -0
  175. package/src/pages/functions/routes-transformation.test.ts +282 -0
  176. package/src/pages/functions/routes-transformation.ts +115 -0
  177. package/src/pages/functions/routes-validation.test.ts +403 -0
  178. package/src/pages/functions/routes-validation.ts +202 -0
  179. package/src/pages/functions/routes.ts +151 -0
  180. package/src/pages/functions.tsx +82 -0
  181. package/src/pages/hash.tsx +13 -0
  182. package/src/pages/index.tsx +102 -0
  183. package/src/pages/projects.tsx +169 -0
  184. package/src/pages/prompt-select-project.tsx +31 -0
  185. package/src/pages/publish.tsx +451 -0
  186. package/src/pages/types.ts +41 -0
  187. package/src/pages/upload.tsx +426 -0
  188. package/src/pages/utils.ts +23 -0
  189. package/src/parse.ts +224 -121
  190. package/src/paths.ts +53 -8
  191. package/src/proxy.ts +629 -375
  192. package/src/publish/index.ts +270 -0
  193. package/src/publish/publish.ts +1035 -0
  194. package/src/pubsub/index.ts +286 -0
  195. package/src/pubsub/pubsub-commands.tsx +624 -0
  196. package/src/queues/cli/commands/consumer/add.ts +71 -0
  197. package/src/queues/cli/commands/consumer/index.ts +22 -0
  198. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  199. package/src/queues/cli/commands/create.ts +25 -0
  200. package/src/queues/cli/commands/delete.ts +26 -0
  201. package/src/queues/cli/commands/index.ts +36 -0
  202. package/src/queues/cli/commands/list.ts +25 -0
  203. package/src/queues/client.ts +135 -0
  204. package/src/queues/utils.ts +18 -0
  205. package/src/r2/constants.ts +4 -0
  206. package/src/r2/helpers.ts +131 -0
  207. package/src/r2/index.ts +289 -0
  208. package/src/routes.ts +140 -0
  209. package/src/secret/index.ts +378 -0
  210. package/src/selfsigned.d.ts +23 -23
  211. package/src/sites.tsx +283 -191
  212. package/src/tail/createTail.ts +379 -0
  213. package/src/tail/filters.ts +108 -110
  214. package/src/tail/index.ts +211 -296
  215. package/src/tail/printing.ts +92 -47
  216. package/src/type-generation.ts +173 -0
  217. package/src/update-check.ts +19 -0
  218. package/src/user/access.ts +69 -0
  219. package/src/user/choose-account.tsx +69 -0
  220. package/src/user/env-vars.ts +46 -0
  221. package/src/user/generate-auth-url.ts +33 -0
  222. package/src/user/generate-random-state.ts +16 -0
  223. package/src/user/index.ts +3 -0
  224. package/src/user/user.tsx +1198 -0
  225. package/src/utils/collectKeyValues.ts +14 -0
  226. package/src/whoami.tsx +142 -42
  227. package/src/worker.ts +182 -100
  228. package/src/yargs-types.ts +24 -0
  229. package/src/zones.ts +191 -0
  230. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  231. package/templates/checked-fetch.js +17 -0
  232. package/templates/d1-beta-facade.js +205 -0
  233. package/templates/first-party-worker-module-facade.ts +18 -0
  234. package/templates/format-dev-errors.ts +32 -0
  235. package/templates/gitignore +171 -0
  236. package/templates/init-tests/test-jest-new-worker.js +25 -0
  237. package/templates/init-tests/test-vitest-new-worker.js +26 -0
  238. package/templates/middleware/common.ts +62 -0
  239. package/templates/middleware/loader-modules.ts +84 -0
  240. package/templates/middleware/loader-sw.ts +227 -0
  241. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  242. package/templates/middleware/middleware-pretty-error.ts +40 -0
  243. package/templates/middleware/middleware-scheduled.ts +15 -0
  244. package/templates/new-worker-scheduled.js +17 -0
  245. package/templates/new-worker-scheduled.ts +32 -0
  246. package/templates/new-worker.js +3 -3
  247. package/templates/new-worker.ts +18 -3
  248. package/templates/no-op-worker.js +10 -0
  249. package/templates/pages-dev-pipeline.ts +28 -0
  250. package/templates/pages-dev-util.ts +52 -0
  251. package/templates/pages-shim.ts +9 -0
  252. package/templates/pages-template-plugin.ts +171 -0
  253. package/templates/pages-template-worker.ts +189 -0
  254. package/templates/serve-static-assets.ts +63 -0
  255. package/templates/service-bindings-module-facade.js +54 -0
  256. package/templates/service-bindings-sw-facade.js +42 -0
  257. package/templates/tsconfig.json +96 -95
  258. package/wrangler-dist/cli.d.ts +893 -0
  259. package/wrangler-dist/cli.js +116816 -94969
  260. package/miniflare-dist/index.mjs.map +0 -7
  261. package/pages/functions/buildWorker.ts +0 -62
  262. package/pages/functions/filepath-routing.test.ts +0 -223
  263. package/pages/functions/filepath-routing.ts +0 -181
  264. package/pages/functions/identifiers.ts +0 -78
  265. package/pages/functions/routes.ts +0 -151
  266. package/pages/functions/template-worker.ts +0 -130
  267. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  268. package/src/__tests__/sentry.test.ts +0 -146
  269. package/src/kv.ts +0 -319
  270. package/src/miniflare-cli/enum-keys.ts +0 -17
  271. package/src/pages.tsx +0 -1061
  272. package/src/publish.ts +0 -350
  273. package/src/r2.ts +0 -50
  274. package/src/reporting.ts +0 -131
  275. package/src/user.tsx +0 -1127
  276. package/templates/static-asset-facade.js +0 -47
  277. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  278. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  279. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  280. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  281. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  282. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  283. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  284. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  285. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  286. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  287. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  288. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  289. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  290. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  291. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  292. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  293. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  294. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  295. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  296. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  297. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  298. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  299. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  300. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  301. package/vendor/wrangler-mime/LICENSE +0 -21
  302. package/vendor/wrangler-mime/Mime.js +0 -97
  303. package/vendor/wrangler-mime/README.md +0 -187
  304. package/vendor/wrangler-mime/cli.js +0 -46
  305. package/vendor/wrangler-mime/index.js +0 -4
  306. package/vendor/wrangler-mime/lite.js +0 -4
  307. package/vendor/wrangler-mime/package.json +0 -52
  308. package/vendor/wrangler-mime/types/other.js +0 -1
  309. package/vendor/wrangler-mime/types/standard.js +0 -1
  310. package/wrangler-dist/cli.js.map +0 -7
@@ -1,983 +1,264 @@
1
- import * as fs from "node:fs";
2
- import * as fsp from "node:fs/promises";
3
- import * as TOML from "@iarna/toml";
4
- import { parseConfigFileTextToJson } from "typescript";
5
- import { version as wranglerVersion } from "../../package.json";
6
1
  import { getPackageManager } from "../package-manager";
7
2
  import { mockConsoleMethods } from "./helpers/mock-console";
8
- import { mockConfirm } from "./helpers/mock-dialogs";
9
3
  import { runInTempDir } from "./helpers/run-in-tmp";
10
4
  import { runWrangler } from "./helpers/run-wrangler";
5
+ import { writeWorkerSource } from "./helpers/write-worker-source";
6
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
11
7
  import type { PackageManager } from "../package-manager";
12
8
 
13
9
  describe("wrangler", () => {
14
- let mockPackageManager: PackageManager;
15
- runInTempDir();
16
-
17
- beforeEach(() => {
18
- mockPackageManager = {
19
- cwd: process.cwd(),
20
- // @ts-expect-error we're making a fake package manager here
21
- type: "mockpm",
22
- addDevDeps: jest.fn(),
23
- install: jest.fn(),
24
- };
25
- (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
26
- });
27
-
28
- const std = mockConsoleMethods();
29
-
30
- describe("no command", () => {
31
- it("should display a list of available commands", async () => {
32
- await runWrangler();
33
-
34
- expect(std.out).toMatchInlineSnapshot(`
35
- "wrangler
36
-
37
- Commands:
38
- wrangler init [name] 📥 Create a wrangler.toml configuration file
39
- wrangler whoami 🕵️ Retrieve your user info and test your auth config
40
- wrangler dev [script] 👂 Start a local server for developing your worker
41
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
42
- wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
43
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
44
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
45
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
46
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
47
- wrangler pages ⚡️ Configure Cloudflare Pages
48
- wrangler r2 📦 Interact with an R2 store
49
-
50
- Flags:
51
- -c, --config Path to .toml configuration file [string]
52
- -h, --help Show help [boolean]
53
- -v, --version Show version number [boolean]
54
- --legacy-env Use legacy environments [boolean]"
55
- `);
56
-
57
- expect(std.err).toMatchInlineSnapshot(`""`);
58
- });
59
- });
60
-
61
- describe("invalid command", () => {
62
- it("should display an error", async () => {
63
- await expect(
64
- runWrangler("invalid-command")
65
- ).rejects.toThrowErrorMatchingInlineSnapshot(
66
- `"Unknown argument: invalid-command"`
67
- );
68
-
69
- expect(std.out).toMatchInlineSnapshot(`""`);
70
- expect(std.err).toMatchInlineSnapshot(`
71
- "wrangler
72
-
73
- Commands:
74
- wrangler init [name] 📥 Create a wrangler.toml configuration file
75
- wrangler whoami 🕵️ Retrieve your user info and test your auth config
76
- wrangler dev [script] 👂 Start a local server for developing your worker
77
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
78
- wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
79
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
80
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
81
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
82
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
83
- wrangler pages ⚡️ Configure Cloudflare Pages
84
- wrangler r2 📦 Interact with an R2 store
85
-
86
- Flags:
87
- -c, --config Path to .toml configuration file [string]
88
- -h, --help Show help [boolean]
89
- -v, --version Show version number [boolean]
90
- --legacy-env Use legacy environments [boolean]
91
-
92
- Unknown argument: invalid-command"
93
- `);
94
- });
95
- });
96
-
97
- describe("init", () => {
98
- it("should create a wrangler.toml", async () => {
99
- mockConfirm({
100
- text: "No package.json found. Would you like to create one?",
101
- result: false,
102
- });
103
- await runWrangler("init");
104
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
105
- expect(typeof parsed.compatibility_date).toBe("string");
106
- expect(parsed.name).toContain("wrangler-tests");
107
- expect(fs.existsSync("./package.json")).toBe(false);
108
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
109
- });
110
-
111
- it("should create a wrangler.toml and a directory for a named Worker ", async () => {
112
- mockConfirm({
113
- text: "No package.json found. Would you like to create one?",
114
- result: false,
115
- });
116
- await runWrangler("init my-worker");
117
- const parsed = TOML.parse(
118
- await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
119
- );
120
- expect(typeof parsed.compatibility_date).toBe("string");
121
- expect(parsed.name).toBe("my-worker");
122
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
123
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
124
- });
125
-
126
- it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
127
- fs.writeFileSync(
128
- "./wrangler.toml",
129
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
130
- "utf-8"
131
- );
132
- mockConfirm({
133
- text: "Do you want to continue initializing this project?",
134
- result: false,
135
- });
136
- await runWrangler("init");
137
- expect(std.warn).toContain("wrangler.toml file already exists!");
138
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
139
- expect(parsed.compatibility_date).toBe("something-else");
140
- });
141
-
142
- it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
143
- fs.writeFileSync(
144
- "./wrangler.toml",
145
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
146
- "utf-8"
147
- );
148
- mockConfirm(
149
- {
150
- text: "Do you want to continue initializing this project?",
151
- result: true,
152
- },
153
- {
154
- text: "No package.json found. Would you like to create one?",
155
- result: true,
156
- },
157
- {
158
- text: "Would you like to use TypeScript?",
159
- result: true,
160
- },
161
- {
162
- text: "Would you like to create a Worker at src/index.ts?",
163
- result: true,
164
- }
165
- );
166
-
167
- await runWrangler("init");
168
- expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
169
- `"compatibility_date=\\"something-else\\""`
170
- );
171
- });
172
-
173
- it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
174
- fs.writeFileSync(
175
- "./wrangler.toml",
176
- `compatibility_date="something-else"`,
177
- "utf-8"
178
- );
179
- mockConfirm(
180
- {
181
- text: "Do you want to continue initializing this project?",
182
- result: true,
183
- },
184
- {
185
- text: "No package.json found. Would you like to create one?",
186
- result: false,
187
- }
188
- );
189
- await runWrangler("init");
190
- expect(std.warn).toContain("wrangler.toml file already exists!");
191
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
192
- expect(parsed.compatibility_date).toBe("something-else");
193
- });
194
-
195
- it("should create a package.json if none is found and user confirms", async () => {
196
- mockConfirm(
197
- {
198
- text: "No package.json found. Would you like to create one?",
199
- result: true,
200
- },
201
- {
202
- text: "Would you like to use TypeScript?",
203
- result: false,
204
- },
205
- {
206
- text: "Would you like to create a Worker at src/index.js?",
207
- result: false,
208
- }
209
- );
210
- await runWrangler("init");
211
- expect(fs.existsSync("./package.json")).toBe(true);
212
- const packageJson = JSON.parse(
213
- fs.readFileSync("./package.json", "utf-8")
214
- );
215
- expect(packageJson.name).toContain("wrangler-tests");
216
- expect(packageJson.version).toEqual("0.0.0");
217
- expect(packageJson.devDependencies).toEqual({
218
- wrangler: expect.any(String),
219
- });
220
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
221
- expect(mockPackageManager.install).toHaveBeenCalled();
222
- });
223
-
224
- it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
225
- mockConfirm(
226
- {
227
- text: "No package.json found. Would you like to create one?",
228
- result: true,
229
- },
230
- {
231
- text: "Would you like to use TypeScript?",
232
- result: false,
233
- },
234
- {
235
- text: "Would you like to create a Worker at src/index.js?",
236
- result: false,
237
- }
238
- );
239
- await runWrangler("init my-worker");
240
- const packageJson = JSON.parse(
241
- fs.readFileSync("./my-worker/package.json", "utf-8")
242
- );
243
- expect(packageJson.name).toBe("my-worker");
244
- });
245
-
246
- it("should not touch an existing package.json in the same directory", async () => {
247
- mockConfirm(
248
- {
249
- text: "Would you like to install wrangler into your package.json?",
250
- result: false,
251
- },
252
- {
253
- text: "Would you like to use TypeScript?",
254
- result: false,
255
- },
256
- {
257
- text: "Would you like to create a Worker at src/index.js?",
258
- result: false,
259
- }
260
- );
261
-
262
- fs.writeFileSync(
263
- "./package.json",
264
- JSON.stringify({ name: "test", version: "1.0.0" }),
265
- "utf-8"
266
- );
267
-
268
- await runWrangler("init");
269
- const packageJson = JSON.parse(
270
- fs.readFileSync("./package.json", "utf-8")
271
- );
272
- expect(packageJson.name).toEqual("test");
273
- expect(packageJson.version).toEqual("1.0.0");
274
- });
275
-
276
- it("should offer to install wrangler into an existing package.json", async () => {
277
- mockConfirm(
278
- {
279
- text: "Would you like to install wrangler into your package.json?",
280
- result: true,
281
- },
282
- {
283
- text: "Would you like to use TypeScript?",
284
- result: false,
285
- },
286
- {
287
- text: "Would you like to create a Worker at src/index.js?",
288
- result: false,
289
- }
290
- );
291
-
292
- fs.writeFileSync(
293
- "./package.json",
294
- JSON.stringify({ name: "test", version: "1.0.0" }),
295
- "utf-8"
296
- );
297
-
298
- await runWrangler("init");
299
- const packageJson = JSON.parse(
300
- fs.readFileSync("./package.json", "utf-8")
301
- );
302
- expect(packageJson.name).toEqual("test");
303
- expect(packageJson.version).toEqual("1.0.0");
304
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
305
- `wrangler@${wranglerVersion}`
306
- );
307
- });
308
-
309
- it("should not touch an existing package.json in an ancestor directory", async () => {
310
- mockConfirm(
311
- {
312
- text: "Would you like to install wrangler into your package.json?",
313
- result: false,
314
- },
315
- {
316
- text: "Would you like to use TypeScript?",
317
- result: false,
318
- },
319
- {
320
- text: "Would you like to create a Worker at src/index.js?",
321
- result: false,
322
- }
323
- );
324
-
325
- fs.writeFileSync(
326
- "./package.json",
327
- JSON.stringify({ name: "test", version: "1.0.0" }),
328
- "utf-8"
329
- );
330
-
331
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
332
- process.chdir("./sub-1/sub-2");
333
-
334
- await runWrangler("init");
335
- expect(fs.existsSync("./package.json")).toBe(false);
336
- expect(fs.existsSync("../../package.json")).toBe(true);
337
-
338
- const packageJson = JSON.parse(
339
- fs.readFileSync("../../package.json", "utf-8")
340
- );
341
- expect(packageJson).toMatchInlineSnapshot(`
342
- Object {
343
- "name": "test",
344
- "version": "1.0.0",
345
- }
346
- `);
347
- });
348
-
349
- it("should offer to create a worker in a non-typescript project", async () => {
350
- mockConfirm(
351
- {
352
- text: "Would you like to install wrangler into your package.json?",
353
- result: false,
354
- },
355
- {
356
- text: "Would you like to use TypeScript?",
357
- result: false,
358
- },
359
- {
360
- text: "Would you like to create a Worker at src/index.js?",
361
- result: true,
362
- }
363
- );
364
-
365
- fs.writeFileSync(
366
- "./package.json",
367
- JSON.stringify({ name: "test", version: "1.0.0" }),
368
- "utf-8"
369
- );
370
-
371
- await runWrangler("init");
372
- expect(fs.existsSync("./src/index.js")).toBe(true);
373
- expect(fs.existsSync("./src/index.ts")).toBe(false);
374
- });
375
-
376
- it("should offer to create a worker in a typescript project", async () => {
377
- mockConfirm(
378
- {
379
- text: "Would you like to install wrangler into your package.json?",
380
- result: false,
381
- },
382
- {
383
- text: "Would you like to use TypeScript?",
384
- result: true,
385
- },
386
- {
387
- text: "Would you like to create a Worker at src/index.ts?",
388
- result: true,
389
- }
390
- );
391
-
392
- fs.writeFileSync(
393
- "./package.json",
394
- JSON.stringify({ name: "test", version: "1.0.0" }),
395
- "utf-8"
396
- );
397
-
398
- await runWrangler("init");
399
- expect(fs.existsSync("./src/index.js")).toBe(false);
400
- expect(fs.existsSync("./src/index.ts")).toBe(true);
401
- });
402
-
403
- it("should add scripts for a typescript project with .ts extension", async () => {
404
- mockConfirm(
405
- {
406
- text: "No package.json found. Would you like to create one?",
407
- result: true,
408
- },
409
- {
410
- text: "Would you like to install wrangler into your package.json?",
411
- result: false,
412
- },
413
- {
414
- text: "Would you like to use TypeScript?",
415
- result: true,
416
- },
417
- {
418
- text: "Would you like to create a Worker at src/index.ts?",
419
- result: true,
420
- }
421
- );
422
- await runWrangler("init");
423
-
424
- expect(fs.existsSync("./package.json")).toBe(true);
425
- const packageJson = JSON.parse(
426
- fs.readFileSync("./package.json", "utf-8")
427
- );
428
-
429
- expect(fs.existsSync("./src/index.js")).toBe(false);
430
- expect(fs.existsSync("./src/index.ts")).toBe(true);
431
-
432
- expect(packageJson.scripts.start).toBe("wrangler dev");
433
- expect(packageJson.scripts.publish).toBe("wrangler publish");
434
- expect(packageJson.name).toContain("wrangler-tests");
435
- expect(packageJson.version).toEqual("0.0.0");
436
- expect(std.out).toMatchInlineSnapshot(`
437
- "✨ Successfully created wrangler.toml
438
- ✨ Created package.json
439
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
440
- ✨ Created src/index.ts
441
-
442
- To start developing your Worker, run \`npm start\`
443
- To publish your Worker to the Internet, run \`npm run publish\`"
444
- `);
445
- });
446
-
447
- it("should not overwrite package.json scripts for a typescript project", async () => {
448
- mockConfirm(
449
- {
450
- text: "Would you like to install wrangler into your package.json?",
451
- result: false,
452
- },
453
- {
454
- text: "Would you like to use TypeScript?",
455
- result: true,
456
- },
457
- {
458
- text: "Would you like to create a Worker at src/index.ts?",
459
- result: true,
460
- }
461
- );
462
- await fsp.writeFile(
463
- "./package.json",
464
- JSON.stringify({
465
- scripts: {
466
- start: "test-start",
467
- publish: "test-publish",
468
- },
469
- })
470
- );
471
- const packageJson = JSON.parse(
472
- fs.readFileSync("./package.json", "utf-8")
473
- );
474
- await runWrangler("init");
475
-
476
- expect(fs.existsSync("./src/index.js")).toBe(false);
477
- expect(fs.existsSync("./src/index.ts")).toBe(true);
478
-
479
- expect(packageJson.scripts.start).toBe("test-start");
480
- expect(packageJson.scripts.publish).toBe("test-publish");
481
- expect(std.out).toMatchInlineSnapshot(`
482
- "✨ Successfully created wrangler.toml
483
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
484
- ✨ Created src/index.ts
485
-
486
- To start developing your Worker, run \`npx wrangler dev\`
487
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
488
- `);
489
- });
490
-
491
- it("should add missing scripts for a non-ts project with .js extension", async () => {
492
- mockConfirm(
493
- {
494
- text: "No package.json found. Would you like to create one?",
495
- result: true,
496
- },
497
- {
498
- text: "Would you like to install wrangler into your package.json?",
499
- result: false,
500
- },
501
- {
502
- text: "Would you like to use TypeScript?",
503
- result: false,
504
- },
505
- {
506
- text: "Would you like to create a Worker at src/index.js?",
507
- result: true,
508
- }
509
- );
510
- await runWrangler("init");
511
-
512
- expect(fs.existsSync("./package.json")).toBe(true);
513
- const packageJson = JSON.parse(
514
- fs.readFileSync("./package.json", "utf-8")
515
- );
516
-
517
- expect(fs.existsSync("./src/index.js")).toBe(true);
518
- expect(fs.existsSync("./src/index.ts")).toBe(false);
519
-
520
- expect(packageJson.scripts.start).toBe("wrangler dev");
521
- expect(packageJson.scripts.publish).toBe("wrangler publish");
522
- expect(packageJson.name).toContain("wrangler-tests");
523
- expect(packageJson.version).toEqual("0.0.0");
524
- expect(std.out).toMatchInlineSnapshot(`
525
- "✨ Successfully created wrangler.toml
526
- ✨ Created package.json
527
- ✨ Created src/index.js
528
-
529
- To start developing your Worker, run \`npm start\`
530
- To publish your Worker to the Internet, run \`npm run publish\`"
531
- `);
532
- });
533
-
534
- it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
535
- mockConfirm(
536
- {
537
- text: "Would you like to install wrangler into your package.json?",
538
- result: false,
539
- },
540
- {
541
- text: "Would you like to use TypeScript?",
542
- result: false,
543
- },
544
- {
545
- text: "Would you like to create a Worker at src/index.js?",
546
- result: true,
547
- }
548
- );
549
- await fsp.writeFile(
550
- "./package.json",
551
- JSON.stringify({
552
- scripts: {
553
- start: "test-start",
554
- publish: "test-publish",
555
- },
556
- })
557
- );
558
- const packageJson = JSON.parse(
559
- fs.readFileSync("./package.json", "utf-8")
560
- );
561
- await runWrangler("init");
562
-
563
- expect(fs.existsSync("./src/index.js")).toBe(true);
564
- expect(fs.existsSync("./src/index.ts")).toBe(false);
565
-
566
- expect(packageJson.scripts.start).toBe("test-start");
567
- expect(packageJson.scripts.publish).toBe("test-publish");
568
- expect(std.out).toMatchInlineSnapshot(`
569
- "✨ Successfully created wrangler.toml
570
- ✨ Created src/index.js
571
-
572
- To start developing your Worker, run \`npx wrangler dev\`
573
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
574
- `);
575
- });
576
-
577
- it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
578
- mockConfirm(
579
- {
580
- text: "Would you like to install wrangler into your package.json?",
581
- result: false,
582
- },
583
- {
584
- text: "Would you like to use TypeScript?",
585
- result: false,
586
- }
587
- );
588
-
589
- fs.writeFileSync(
590
- "./package.json",
591
- JSON.stringify({ name: "test", version: "1.0.0" }),
592
- "utf-8"
593
- );
594
- fs.mkdirSync("./src", { recursive: true });
595
- const PLACEHOLDER = "/* placeholder text */";
596
- fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
597
-
598
- await runWrangler("init");
599
- expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
600
- expect(fs.existsSync("./src/index.ts")).toBe(false);
601
- });
602
-
603
- it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
604
- mockConfirm(
605
- {
606
- text: "Would you like to install wrangler into your package.json?",
607
- result: false,
608
- },
609
- {
610
- text: "Would you like to use TypeScript?",
611
- result: true,
612
- }
613
- );
614
-
615
- fs.writeFileSync(
616
- "./package.json",
617
- JSON.stringify({ name: "test", version: "1.0.0" }),
618
- "utf-8"
619
- );
620
- fs.mkdirSync("./src", { recursive: true });
621
- const PLACEHOLDER = "/* placeholder text */";
622
- fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
623
-
624
- await runWrangler("init");
625
- expect(fs.existsSync("./src/index.js")).toBe(false);
626
- expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
627
- });
628
-
629
- it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
630
- mockConfirm(
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: true,
638
- },
639
- {
640
- text: "Would you like to create a Worker at src/index.ts?",
641
- result: false,
642
- }
643
- );
644
- await runWrangler("init");
645
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
646
- const { config: tsconfigJson, error: tsConfigParseError } =
647
- parseConfigFileTextToJson(
648
- "./tsconfig.json",
649
- fs.readFileSync("./tsconfig.json", "utf-8")
650
- );
651
- expect(tsConfigParseError).toBeUndefined();
652
- expect(tsconfigJson.compilerOptions.types).toEqual([
653
- "@cloudflare/workers-types",
654
- ]);
655
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
656
- "@cloudflare/workers-types",
657
- "typescript"
658
- );
659
- });
660
-
661
- it("should not touch an existing tsconfig.json in the same directory", async () => {
662
- fs.writeFileSync(
663
- "./package.json",
664
- JSON.stringify({
665
- name: "test",
666
- version: "1.0.0",
667
- devDependencies: {
668
- wrangler: "0.0.0",
669
- "@cloudflare/workers-types": "0.0.0",
670
- },
671
- }),
672
- "utf-8"
673
- );
674
- fs.writeFileSync(
675
- "./tsconfig.json",
676
- JSON.stringify({ compilerOptions: {} }),
677
- "utf-8"
678
- );
679
-
680
- await runWrangler("init");
681
- const tsconfigJson = JSON.parse(
682
- fs.readFileSync("./tsconfig.json", "utf-8")
683
- );
684
- expect(tsconfigJson.compilerOptions).toEqual({});
685
- });
686
-
687
- it("should offer to install type definitions in an existing typescript project", async () => {
688
- mockConfirm(
689
- {
690
- text: "Would you like to install wrangler into your package.json?",
691
- result: false,
692
- },
693
- {
694
- text: "Would you like to install the type definitions for Workers into your package.json?",
695
- result: true,
696
- },
697
- {
698
- text: "Would you like to create a Worker at src/index.ts?",
699
- result: false,
700
- }
701
- );
702
- fs.writeFileSync(
703
- "./package.json",
704
- JSON.stringify({
705
- name: "test",
706
- version: "1.0.0",
707
- }),
708
- "utf-8"
709
- );
710
- fs.writeFileSync(
711
- "./tsconfig.json",
712
- JSON.stringify({ compilerOptions: {} }),
713
- "utf-8"
714
- );
715
-
716
- await runWrangler("init");
717
- const tsconfigJson = JSON.parse(
718
- fs.readFileSync("./tsconfig.json", "utf-8")
719
- );
720
- // unchanged tsconfig
721
- expect(tsconfigJson.compilerOptions).toEqual({});
722
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
723
- "@cloudflare/workers-types"
724
- );
725
- });
726
-
727
- it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
728
- fs.writeFileSync(
729
- "./package.json",
730
- JSON.stringify({
731
- name: "test",
732
- version: "1.0.0",
733
- devDependencies: {
734
- wrangler: "0.0.0",
735
- "@cloudflare/workers-types": "0.0.0",
736
- },
737
- }),
738
- "utf-8"
739
- );
740
- fs.writeFileSync(
741
- "./tsconfig.json",
742
- JSON.stringify({ compilerOptions: {} }),
743
- "utf-8"
744
- );
745
-
746
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
747
- process.chdir("./sub-1/sub-2");
748
-
749
- await runWrangler("init");
750
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
751
- expect(fs.existsSync("../../tsconfig.json")).toBe(true);
752
-
753
- const tsconfigJson = JSON.parse(
754
- fs.readFileSync("../../tsconfig.json", "utf-8")
755
- );
756
- expect(tsconfigJson.compilerOptions).toEqual({});
757
- });
758
-
759
- it("should initialize with no interactive prompts if `--yes` is used", async () => {
760
- await runWrangler("init --yes");
761
-
762
- expect(fs.existsSync("./src/index.js")).toBe(false);
763
- expect(fs.existsSync("./src/index.ts")).toBe(true);
764
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
765
- expect(fs.existsSync("./package.json")).toBe(true);
766
- expect(fs.existsSync("./wrangler.toml")).toBe(true);
767
- expect(std.out).toMatchInlineSnapshot(`
768
- "✨ Successfully created wrangler.toml
769
- ✨ Created package.json
770
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
771
- ✨ Created src/index.ts
772
-
773
- To start developing your Worker, run \`npm start\`
774
- To publish your Worker to the Internet, run \`npm run publish\`"
775
- `);
776
- expect(std.err).toMatchInlineSnapshot(`""`);
777
- expect(std.warn).toMatchInlineSnapshot(`""`);
778
- });
779
-
780
- it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
781
- await runWrangler("init my-worker --yes");
782
-
783
- expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
784
- expect(fs.existsSync("./my-worker/src/index.ts")).toBe(true);
785
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(true);
786
- expect(fs.existsSync("./my-worker/package.json")).toBe(true);
787
- expect(fs.existsSync("./my-worker/wrangler.toml")).toBe(true);
788
- const parsedWranglerToml = TOML.parse(
789
- fs.readFileSync("./my-worker/wrangler.toml", "utf-8")
790
- );
791
- expect(parsedWranglerToml.main).toEqual("src/index.ts");
792
- expect(std.out).toMatchInlineSnapshot(`
793
- "✨ Successfully created wrangler.toml
794
- ✨ Created package.json
795
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
796
- ✨ Created src/index.ts
797
-
798
- To start developing your Worker, run \`cd my-worker && npm start\`
799
- To publish your Worker to the Internet, run \`npm run publish\`"
800
- `);
801
- expect(std.err).toMatchInlineSnapshot(`""`);
802
- expect(std.warn).toMatchInlineSnapshot(`""`);
803
- });
804
-
805
- it("should initialize with no interactive prompts if `-y` is used", async () => {
806
- await runWrangler("init -y");
807
-
808
- expect(fs.existsSync("./src/index.js")).toBe(false);
809
- expect(fs.existsSync("./src/index.ts")).toBe(true);
810
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
811
- expect(fs.existsSync("./package.json")).toBe(true);
812
- expect(fs.existsSync("./wrangler.toml")).toBe(true);
813
- });
814
-
815
- it("should error if `--type javascript` is used", async () => {
816
- await expect(
817
- runWrangler("init --type javascript")
818
- ).rejects.toThrowErrorMatchingInlineSnapshot(
819
- `"The --type option is no longer supported."`
820
- );
821
- });
822
-
823
- it("should error if `--type rust` is used", async () => {
824
- await expect(
825
- runWrangler("init --type rust")
826
- ).rejects.toThrowErrorMatchingInlineSnapshot(
827
- `"The --type option is no longer supported."`
828
- );
829
- });
830
-
831
- it("should error if `--type webpack` is used", async () => {
832
- await expect(runWrangler("init --type webpack")).rejects
833
- .toThrowErrorMatchingInlineSnapshot(`
834
- "The --type option is no longer supported.
835
- If you wish to use webpack then you will need to create a custom build."
836
- `);
837
- });
838
- });
839
-
840
- describe("preview", () => {
841
- it("should throw an error if the deprecated command is used with positional arguments", async () => {
842
- await expect(runWrangler("preview GET")).rejects
843
- .toThrowErrorMatchingInlineSnapshot(`
844
- "DEPRECATION WARNING:
10
+ let mockPackageManager: PackageManager;
11
+ runInTempDir();
12
+
13
+ beforeEach(() => {
14
+ mockPackageManager = {
15
+ cwd: process.cwd(),
16
+ // @ts-expect-error we're making a fake package manager here
17
+ type: "mockpm",
18
+ addDevDeps: jest.fn(),
19
+ install: jest.fn(),
20
+ };
21
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
22
+ });
23
+
24
+ const std = mockConsoleMethods();
25
+
26
+ describe("no command", () => {
27
+ it("should display a list of available commands", async () => {
28
+ await runWrangler();
29
+
30
+ expect(std.out).toMatchInlineSnapshot(`
31
+ "wrangler
32
+
33
+ Commands:
34
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
35
+ wrangler dev [script] 👂 Start a local server for developing your worker
36
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
37
+ wrangler delete [script] 🗑 Delete your Worker from Cloudflare.
38
+ wrangler tail [worker] 🦚 Starts a log tailing session for a published Worker.
39
+ wrangler secret 🤫 Generate a secret that can be referenced in a Worker
40
+ wrangler secret:bulk <json> 🗄️ Bulk upload secrets for a Worker
41
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
42
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
43
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
44
+ wrangler pages ⚡️ Configure Cloudflare Pages
45
+ wrangler queues 🇶 Configure Workers Queues
46
+ wrangler r2 📦 Interact with an R2 store
47
+ wrangler dispatch-namespace 📦 Interact with a dispatch namespace
48
+ wrangler d1 🗄 Interact with a D1 database
49
+ wrangler pubsub 📮 Interact and manage Pub/Sub Brokers
50
+ wrangler login 🔓 Login to Cloudflare
51
+ wrangler logout 🚪 Logout from Cloudflare
52
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
53
+ wrangler types 📝 Generate types from bindings & module rules in config
54
+ wrangler deployments 🚢 Displays the 10 most recent deployments for a worker
55
+
56
+ Flags:
57
+ -c, --config Path to .toml configuration file [string]
58
+ -e, --env Environment to use for operations and .env files [string]
59
+ -h, --help Show help [boolean]
60
+ -v, --version Show version number [boolean]"
61
+ `);
62
+
63
+ expect(std.err).toMatchInlineSnapshot(`""`);
64
+ });
65
+ });
66
+
67
+ describe("invalid command", () => {
68
+ it("should display an error", async () => {
69
+ await expect(
70
+ runWrangler("invalid-command")
71
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
72
+ `"Unknown argument: invalid-command"`
73
+ );
74
+
75
+ expect(std.out).toMatchInlineSnapshot(`
76
+ "
77
+ wrangler
78
+
79
+ Commands:
80
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
81
+ wrangler dev [script] 👂 Start a local server for developing your worker
82
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
83
+ wrangler delete [script] 🗑 Delete your Worker from Cloudflare.
84
+ wrangler tail [worker] 🦚 Starts a log tailing session for a published Worker.
85
+ wrangler secret 🤫 Generate a secret that can be referenced in a Worker
86
+ wrangler secret:bulk <json> 🗄️ Bulk upload secrets for a Worker
87
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
88
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
89
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
90
+ wrangler pages ⚡️ Configure Cloudflare Pages
91
+ wrangler queues 🇶 Configure Workers Queues
92
+ wrangler r2 📦 Interact with an R2 store
93
+ wrangler dispatch-namespace 📦 Interact with a dispatch namespace
94
+ wrangler d1 🗄 Interact with a D1 database
95
+ wrangler pubsub 📮 Interact and manage Pub/Sub Brokers
96
+ wrangler login 🔓 Login to Cloudflare
97
+ wrangler logout 🚪 Logout from Cloudflare
98
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
99
+ wrangler types 📝 Generate types from bindings & module rules in config
100
+ wrangler deployments 🚢 Displays the 10 most recent deployments for a worker
101
+
102
+ Flags:
103
+ -c, --config Path to .toml configuration file [string]
104
+ -e, --env Environment to use for operations and .env files [string]
105
+ -h, --help Show help [boolean]
106
+ -v, --version Show version number [boolean]"
107
+ `);
108
+ expect(std.err).toMatchInlineSnapshot(`
109
+ "X [ERROR] Unknown argument: invalid-command
110
+
111
+ "
112
+ `);
113
+ });
114
+ });
115
+
116
+ describe("preview", () => {
117
+ it("should throw an error if the deprecated command is used with positional arguments", async () => {
118
+ await expect(runWrangler("preview GET")).rejects
119
+ .toThrowErrorMatchingInlineSnapshot(`
120
+ "Deprecation:
845
121
  The \`wrangler preview\` command has been deprecated.
846
122
  Try using \`wrangler dev\` to to try out a worker during development.
847
123
  "
848
124
  `);
849
- await expect(runWrangler(`preview GET "SomeBody"`)).rejects
850
- .toThrowErrorMatchingInlineSnapshot(`
851
- "DEPRECATION WARNING:
125
+ await expect(runWrangler(`preview GET "SomeBody"`)).rejects
126
+ .toThrowErrorMatchingInlineSnapshot(`
127
+ "Deprecation:
852
128
  The \`wrangler preview\` command has been deprecated.
853
129
  Try using \`wrangler dev\` to to try out a worker during development.
854
130
  "
855
131
  `);
856
- });
857
- });
858
-
859
- describe("subcommand implicit help ran on imcomplete command execution", () => {
860
- function endEventLoop() {
861
- return new Promise((resolve) => setImmediate(resolve));
862
- }
863
- it("no subcommand for 'secret' should display a list of available subcommands", async () => {
864
- await runWrangler("secret");
865
- await endEventLoop();
866
- expect(std.out).toMatchInlineSnapshot(`
867
- "wrangler secret
868
-
869
- 🤫 Generate a secret that can be referenced in the worker script
870
-
871
- Commands:
872
- wrangler secret put <key> Create or update a secret variable for a script
873
- wrangler secret delete <key> Delete a secret variable from a script
874
- wrangler secret list List all secrets for a script
875
-
876
- Flags:
877
- -c, --config Path to .toml configuration file [string]
878
- -h, --help Show help [boolean]
879
- -v, --version Show version number [boolean]
880
- --legacy-env Use legacy environments [boolean]"
881
- `);
882
- });
883
-
884
- it("no subcommand 'kv:namespace' should display a list of available subcommands", async () => {
885
- await runWrangler("kv:namespace");
886
- await endEventLoop();
887
- expect(std.out).toMatchInlineSnapshot(`
888
- "wrangler kv:namespace
889
-
890
- 🗂️ Interact with your Workers KV Namespaces
891
-
892
- Commands:
893
- wrangler kv:namespace create <namespace> Create a new namespace
894
- wrangler kv:namespace list Outputs a list of all KV namespaces associated with your account id.
895
- wrangler kv:namespace delete Deletes a given namespace.
896
-
897
- Flags:
898
- -c, --config Path to .toml configuration file [string]
899
- -h, --help Show help [boolean]
900
- -v, --version Show version number [boolean]
901
- --legacy-env Use legacy environments [boolean]"
902
- `);
903
- });
904
-
905
- it("no subcommand 'kv:key' should display a list of available subcommands", async () => {
906
- await runWrangler("kv:key");
907
- await endEventLoop();
908
- expect(std.out).toMatchInlineSnapshot(`
909
- "wrangler kv:key
910
-
911
- 🔑 Individually manage Workers KV key-value pairs
912
-
913
- Commands:
914
- wrangler kv:key put <key> [value] Writes a single key/value pair to the given namespace.
915
- wrangler kv:key list Outputs a list of all keys in a given namespace.
916
- wrangler kv:key get <key> Reads a single value by key from the given namespace.
917
- wrangler kv:key delete <key> Removes a single key value pair from the given namespace.
918
-
919
- Flags:
920
- -c, --config Path to .toml configuration file [string]
921
- -h, --help Show help [boolean]
922
- -v, --version Show version number [boolean]
923
- --legacy-env Use legacy environments [boolean]"
924
- `);
925
- });
926
-
927
- it("no subcommand 'kv:bulk' should display a list of available subcommands", async () => {
928
- await runWrangler("kv:bulk");
929
- await endEventLoop();
930
- expect(std.out).toMatchInlineSnapshot(`
931
- "wrangler kv:bulk
932
-
933
- 💪 Interact with multiple Workers KV key-value pairs at once
934
-
935
- Commands:
936
- wrangler kv:bulk put <filename> Upload multiple key-value pairs to a namespace
937
- wrangler kv:bulk delete <filename> Delete multiple key-value pairs from a namespace
938
-
939
- Flags:
940
- -c, --config Path to .toml configuration file [string]
941
- -h, --help Show help [boolean]
942
- -v, --version Show version number [boolean]
943
- --legacy-env Use legacy environments [boolean]"
944
- `);
945
- });
946
- it("no subcommand 'r2' should display a list of available subcommands", async () => {
947
- await runWrangler("r2");
948
- await endEventLoop();
949
- expect(std.out).toMatchInlineSnapshot(`
950
- "wrangler r2
951
-
952
- 📦 Interact with an R2 store
953
-
954
- Commands:
955
- wrangler r2 bucket Manage R2 buckets
956
-
957
- Flags:
958
- -c, --config Path to .toml configuration file [string]
959
- -h, --help Show help [boolean]
960
- -v, --version Show version number [boolean]
961
- --legacy-env Use legacy environments [boolean]"
962
- `);
963
- });
964
- });
965
- describe("Deprecated commands", () => {
966
- it("should print a deprecation message for 'generate'", async () => {
967
- await runWrangler("generate").catch((err) => {
968
- expect(err.message).toMatchInlineSnapshot(`
969
- "DEPRECATION WARNING:
970
- \`wrangler generate\` has been deprecated, please refer to https://github.com/cloudflare/wrangler2/blob/main/docs/deprecations.md#generate for alternatives"
971
- `);
972
- });
973
- });
974
- it("should print a deprecation message for 'build'", async () => {
975
- await runWrangler("build").catch((err) => {
976
- expect(err.message).toMatchInlineSnapshot(`
977
- "DEPRECATION WARNING:
978
- \`wrangler build\` has been deprecated, please refer to https://github.com/cloudflare/wrangler2/blob/main/docs/deprecations.md#build for alternatives"
979
- `);
980
- });
981
- });
982
- });
132
+ });
133
+ });
134
+
135
+ describe("subcommand implicit help ran on incomplete command execution", () => {
136
+ it("no subcommand for 'secret' should display a list of available subcommands", async () => {
137
+ await runWrangler("secret");
138
+ await endEventLoop();
139
+ expect(std.out).toMatchInlineSnapshot(`
140
+ "wrangler secret
141
+
142
+ 🤫 Generate a secret that can be referenced in a Worker
143
+
144
+ Commands:
145
+ wrangler secret put <key> Create or update a secret variable for a Worker
146
+ wrangler secret delete <key> Delete a secret variable from a Worker
147
+ wrangler secret list List all secrets for a Worker
148
+
149
+ Flags:
150
+ -c, --config Path to .toml configuration file [string]
151
+ -e, --env Environment to use for operations and .env files [string]
152
+ -h, --help Show help [boolean]
153
+ -v, --version Show version number [boolean]"
154
+ `);
155
+ });
156
+
157
+ it("no subcommand 'kv:namespace' should display a list of available subcommands", async () => {
158
+ await runWrangler("kv:namespace");
159
+ await endEventLoop();
160
+ expect(std.out).toMatchInlineSnapshot(`
161
+ "wrangler kv:namespace
162
+
163
+ 🗂️ Interact with your Workers KV Namespaces
164
+
165
+ Commands:
166
+ wrangler kv:namespace create <namespace> Create a new namespace
167
+ wrangler kv:namespace list Outputs a list of all KV namespaces associated with your account id.
168
+ wrangler kv:namespace delete Deletes a given namespace.
169
+
170
+ Flags:
171
+ -c, --config Path to .toml configuration file [string]
172
+ -e, --env Environment to use for operations and .env files [string]
173
+ -h, --help Show help [boolean]
174
+ -v, --version Show version number [boolean]"
175
+ `);
176
+ });
177
+
178
+ it("no subcommand 'kv:key' should display a list of available subcommands", async () => {
179
+ await runWrangler("kv:key");
180
+ await endEventLoop();
181
+ expect(std.out).toMatchInlineSnapshot(`
182
+ "wrangler kv:key
183
+
184
+ 🔑 Individually manage Workers KV key-value pairs
185
+
186
+ Commands:
187
+ wrangler kv:key put <key> [value] Writes a single key/value pair to the given namespace.
188
+ wrangler kv:key list Outputs a list of all keys in a given namespace.
189
+ wrangler kv:key get <key> Reads a single value by key from the given namespace.
190
+ wrangler kv:key delete <key> Removes a single key value pair from the given namespace.
191
+
192
+ Flags:
193
+ -c, --config Path to .toml configuration file [string]
194
+ -e, --env Environment to use for operations and .env files [string]
195
+ -h, --help Show help [boolean]
196
+ -v, --version Show version number [boolean]"
197
+ `);
198
+ });
199
+
200
+ it("no subcommand 'kv:bulk' should display a list of available subcommands", async () => {
201
+ await runWrangler("kv:bulk");
202
+ await endEventLoop();
203
+ expect(std.out).toMatchInlineSnapshot(`
204
+ "wrangler kv:bulk
205
+
206
+ 💪 Interact with multiple Workers KV key-value pairs at once
207
+
208
+ Commands:
209
+ wrangler kv:bulk put <filename> Upload multiple key-value pairs to a namespace
210
+ wrangler kv:bulk delete <filename> Delete multiple key-value pairs from a namespace
211
+
212
+ Flags:
213
+ -c, --config Path to .toml configuration file [string]
214
+ -e, --env Environment to use for operations and .env files [string]
215
+ -h, --help Show help [boolean]
216
+ -v, --version Show version number [boolean]"
217
+ `);
218
+ });
219
+
220
+ it("no subcommand 'r2' should display a list of available subcommands", async () => {
221
+ await runWrangler("r2");
222
+ await endEventLoop();
223
+ expect(std.out).toMatchInlineSnapshot(`
224
+ "wrangler r2
225
+
226
+ 📦 Interact with an R2 store
227
+
228
+ Commands:
229
+ wrangler r2 object Manage R2 objects
230
+ wrangler r2 bucket Manage R2 buckets
231
+
232
+ Flags:
233
+ -c, --config Path to .toml configuration file [string]
234
+ -e, --env Environment to use for operations and .env files [string]
235
+ -h, --help Show help [boolean]
236
+ -v, --version Show version number [boolean]"
237
+ `);
238
+ });
239
+ });
240
+
241
+ it("should print a deprecation message for 'build' and then try to run `publish --dry-run --outdir`", async () => {
242
+ writeWranglerToml({
243
+ main: "index.js",
244
+ });
245
+ writeWorkerSource();
246
+ await runWrangler("build");
247
+ await endEventLoop();
248
+ expect(std.out).toMatchInlineSnapshot(`
249
+ "▲ [WARNING] Deprecation: \`wrangler build\` has been deprecated.
250
+
251
+ Please refer to https://developers.cloudflare.com/workers/wrangler/migration/deprecations/#build
252
+ for more information.
253
+ Attempting to run \`wrangler publish --dry-run --outdir=dist\` for you instead:
254
+
255
+
256
+ Total Upload: xx KiB / gzip: xx KiB
257
+ --dry-run: exiting now."
258
+ `);
259
+ });
983
260
  });
261
+
262
+ function endEventLoop() {
263
+ return new Promise((resolve) => setImmediate(resolve));
264
+ }