wrangler 0.0.0-e492c10 → 0.0.0-e4ca780f

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 (360) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +25 -5
  3. package/bin/wrangler.js +174 -34
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +1590 -81
  6. package/package.json +198 -149
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +238 -0
  9. package/src/__tests__/api-devregistry.test.ts +121 -0
  10. package/src/__tests__/api.test.ts +102 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +30 -24
  13. package/src/__tests__/configuration.test.ts +4498 -3114
  14. package/src/__tests__/d1/d1.test.ts +82 -0
  15. package/src/__tests__/d1/execute.test.ts +66 -0
  16. package/src/__tests__/d1/migrate.test.ts +157 -0
  17. package/src/__tests__/d1/splitter.test.ts +255 -0
  18. package/src/__tests__/delete.test.ts +272 -0
  19. package/src/__tests__/deployments.test.ts +369 -0
  20. package/src/__tests__/dev.test.tsx +1592 -802
  21. package/src/__tests__/generate.test.ts +237 -0
  22. package/src/__tests__/get-host-from-url.test.ts +16 -0
  23. package/src/__tests__/guess-worker-format.test.ts +103 -68
  24. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  25. package/src/__tests__/helpers/end-event-loop.ts +6 -0
  26. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  27. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  28. package/src/__tests__/helpers/mock-bin.ts +20 -20
  29. package/src/__tests__/helpers/mock-console.ts +64 -39
  30. package/src/__tests__/helpers/mock-dialogs.ts +104 -55
  31. package/src/__tests__/helpers/mock-get-pages-upload-token.ts +25 -0
  32. package/src/__tests__/helpers/mock-get-zone-from-host.ts +11 -0
  33. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  34. package/src/__tests__/helpers/mock-istty.ts +65 -18
  35. package/src/__tests__/helpers/mock-known-routes.ts +12 -0
  36. package/src/__tests__/helpers/mock-kv.ts +41 -28
  37. package/src/__tests__/helpers/mock-oauth-flow.ts +241 -233
  38. package/src/__tests__/helpers/mock-process.ts +34 -0
  39. package/src/__tests__/helpers/mock-set-timeout.ts +16 -0
  40. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  41. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  42. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  43. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  44. package/src/__tests__/helpers/msw/handlers/deployments.ts +160 -0
  45. package/src/__tests__/helpers/msw/handlers/namespaces.ts +81 -0
  46. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  47. package/src/__tests__/helpers/msw/handlers/r2.ts +60 -0
  48. package/src/__tests__/helpers/msw/handlers/script.ts +56 -0
  49. package/src/__tests__/helpers/msw/handlers/user.ts +52 -0
  50. package/src/__tests__/helpers/msw/handlers/zones.ts +20 -0
  51. package/src/__tests__/helpers/msw/index.ts +52 -0
  52. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  53. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  54. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  55. package/src/__tests__/helpers/string-dynamic-values-matcher.ts +28 -0
  56. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  57. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  58. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  59. package/src/__tests__/helpers/worker-scripts/parent-worker.js +11 -0
  60. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  61. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  62. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  63. package/src/__tests__/https-options.test.ts +142 -109
  64. package/src/__tests__/index.test.ts +262 -224
  65. package/src/__tests__/init.test.ts +3168 -1488
  66. package/src/__tests__/jest.setup.ts +152 -50
  67. package/src/__tests__/kv.test.ts +1789 -1432
  68. package/src/__tests__/logger.test.ts +167 -75
  69. package/src/__tests__/logout.test.ts +47 -0
  70. package/src/__tests__/metrics.test.ts +493 -0
  71. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  72. package/src/__tests__/middleware.test.ts +816 -0
  73. package/src/__tests__/mtls-certificates.test.ts +588 -0
  74. package/src/__tests__/package-manager.test.ts +312 -165
  75. package/src/__tests__/pages/deployment-list.test.ts +80 -0
  76. package/src/__tests__/pages/functions-build.test.ts +452 -0
  77. package/src/__tests__/pages/pages.test.ts +81 -0
  78. package/src/__tests__/pages/project-create.test.ts +63 -0
  79. package/src/__tests__/pages/project-list.test.ts +110 -0
  80. package/src/__tests__/pages/project-upload.test.ts +500 -0
  81. package/src/__tests__/pages/publish.test.ts +2864 -0
  82. package/src/__tests__/pages-deployment-tail.test.ts +955 -0
  83. package/src/__tests__/parse.test.ts +358 -248
  84. package/src/__tests__/paths.test.ts +39 -0
  85. package/src/__tests__/publish.test.ts +8322 -4847
  86. package/src/__tests__/pubsub.test.ts +496 -0
  87. package/src/__tests__/queues.test.ts +532 -0
  88. package/src/__tests__/r2.test.ts +369 -205
  89. package/src/__tests__/route.test.ts +18 -18
  90. package/src/__tests__/secret.test.ts +683 -436
  91. package/src/__tests__/tail.test.ts +835 -567
  92. package/src/__tests__/test-old-node-version.js +31 -0
  93. package/src/__tests__/traverse-module-graph.test.ts +220 -0
  94. package/src/__tests__/tsconfig-sanity.ts +12 -0
  95. package/src/__tests__/tsconfig.json +8 -0
  96. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  97. package/src/__tests__/type-generation.test.ts +234 -0
  98. package/src/__tests__/user.test.ts +107 -120
  99. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  100. package/src/__tests__/validate-dev-props.test.ts +56 -0
  101. package/src/__tests__/version.test.ts +35 -0
  102. package/src/__tests__/whoami.test.tsx +165 -98
  103. package/src/__tests__/worker-namespace.test.ts +340 -0
  104. package/src/abort.d.ts +3 -0
  105. package/src/api/dev.ts +316 -0
  106. package/src/api/index.ts +11 -0
  107. package/src/api/mtls-certificate.ts +148 -0
  108. package/src/api/pages/create-worker-bundle-contents.ts +76 -0
  109. package/src/api/pages/index.ts +5 -0
  110. package/src/api/pages/publish.tsx +355 -0
  111. package/src/bundle-reporter.ts +68 -0
  112. package/src/bundle.ts +874 -135
  113. package/src/cfetch/index.ts +123 -69
  114. package/src/cfetch/internal.ts +217 -96
  115. package/src/cli.ts +23 -8
  116. package/src/config/config.ts +244 -196
  117. package/src/config/diagnostics.ts +61 -61
  118. package/src/config/environment.ts +563 -339
  119. package/src/config/index.ts +381 -41
  120. package/src/config/validation-helpers.ts +379 -357
  121. package/src/config/validation.ts +2181 -1263
  122. package/src/config-cache.ts +81 -41
  123. package/src/create-worker-preview.ts +250 -132
  124. package/src/create-worker-upload-form.ts +343 -224
  125. package/src/d1/backups.tsx +219 -0
  126. package/src/d1/constants.ts +2 -0
  127. package/src/d1/create.tsx +68 -0
  128. package/src/d1/delete.ts +53 -0
  129. package/src/d1/execute.tsx +357 -0
  130. package/src/d1/formatTimeAgo.ts +14 -0
  131. package/src/d1/index.ts +100 -0
  132. package/src/d1/list.tsx +60 -0
  133. package/src/d1/migrations/apply.tsx +212 -0
  134. package/src/d1/migrations/create.tsx +79 -0
  135. package/src/d1/migrations/helpers.ts +172 -0
  136. package/src/d1/migrations/index.ts +3 -0
  137. package/src/d1/migrations/list.tsx +95 -0
  138. package/src/d1/migrations/options.ts +23 -0
  139. package/src/d1/options.ts +22 -0
  140. package/src/d1/splitter.ts +161 -0
  141. package/src/d1/types.ts +25 -0
  142. package/src/d1/utils.ts +49 -0
  143. package/src/delete.ts +100 -0
  144. package/src/deployments.ts +368 -0
  145. package/src/deprecated/index.ts +144 -0
  146. package/src/dev/dev-vars.ts +20 -17
  147. package/src/dev/dev.tsx +535 -353
  148. package/src/dev/get-local-persistence-path.ts +31 -0
  149. package/src/dev/local.tsx +900 -292
  150. package/src/dev/remote.tsx +620 -201
  151. package/src/dev/start-server.ts +534 -0
  152. package/src/dev/use-esbuild.ts +182 -97
  153. package/src/dev/validate-dev-props.ts +40 -0
  154. package/src/dev-registry.ts +202 -0
  155. package/src/dev.tsx +929 -0
  156. package/src/dialogs.ts +136 -0
  157. package/src/dispatch-namespace.ts +211 -0
  158. package/src/docs/index.ts +97 -0
  159. package/src/durable.ts +102 -0
  160. package/src/entry.ts +259 -189
  161. package/src/environment-variables/factory.ts +88 -0
  162. package/src/environment-variables/misc-variables.ts +30 -0
  163. package/src/errors.ts +6 -6
  164. package/src/generate/index.ts +298 -0
  165. package/src/git-client.ts +135 -0
  166. package/src/global-wrangler-config-path.ts +26 -0
  167. package/src/https-options.ts +84 -80
  168. package/src/index.ts +758 -0
  169. package/src/init.ts +1032 -0
  170. package/src/inspect.ts +721 -562
  171. package/src/intl-polyfill.d.ts +123 -123
  172. package/src/is-ci.ts +14 -0
  173. package/src/is-interactive.ts +16 -0
  174. package/src/jest.d.ts +4 -0
  175. package/src/kv/helpers.ts +430 -0
  176. package/src/kv/index.ts +594 -0
  177. package/src/logger.ts +96 -43
  178. package/src/metrics/index.ts +5 -0
  179. package/src/metrics/metrics-config.ts +239 -0
  180. package/src/metrics/metrics-dispatcher.ts +96 -0
  181. package/src/metrics/metrics-usage-headers.ts +24 -0
  182. package/src/metrics/send-event.ts +99 -0
  183. package/src/miniflare-cli/assets.ts +251 -0
  184. package/src/miniflare-cli/index.ts +202 -33
  185. package/src/miniflare-cli/request-context.ts +18 -18
  186. package/src/miniflare-cli/tsconfig.json +9 -0
  187. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  188. package/src/miniflare-cli/types.ts +11 -0
  189. package/src/module-collection.ts +290 -215
  190. package/src/mtls-certificate/cli.ts +155 -0
  191. package/src/open-in-browser.ts +5 -14
  192. package/src/package-manager.ts +182 -90
  193. package/src/pages/build.ts +410 -0
  194. package/src/pages/buildFunctions.ts +140 -0
  195. package/src/pages/constants.ts +18 -0
  196. package/src/pages/deployment-tails.ts +281 -0
  197. package/src/pages/deployments.tsx +84 -0
  198. package/src/pages/dev.ts +716 -0
  199. package/src/pages/errors.ts +67 -0
  200. package/src/pages/functions/buildPlugin.ts +113 -0
  201. package/src/pages/functions/buildWorker.ts +291 -0
  202. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  203. package/src/pages/functions/filepath-routing.ts +189 -0
  204. package/src/pages/functions/identifiers.ts +78 -0
  205. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  206. package/src/pages/functions/routes-consolidation.ts +73 -0
  207. package/src/pages/functions/routes-transformation.test.ts +282 -0
  208. package/src/pages/functions/routes-transformation.ts +115 -0
  209. package/src/pages/functions/routes-validation.test.ts +403 -0
  210. package/src/pages/functions/routes-validation.ts +202 -0
  211. package/src/pages/functions/routes.ts +151 -0
  212. package/src/pages/functions/tsconfig.json +8 -0
  213. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  214. package/src/pages/functions.ts +86 -0
  215. package/src/pages/hash.ts +13 -0
  216. package/src/pages/index.ts +102 -0
  217. package/src/pages/projects.tsx +159 -0
  218. package/src/pages/prompt-select-project.tsx +31 -0
  219. package/src/pages/publish.tsx +267 -0
  220. package/src/pages/types.ts +46 -0
  221. package/src/pages/upload.tsx +469 -0
  222. package/src/pages/utils.ts +23 -0
  223. package/src/parse.ts +246 -123
  224. package/src/paths.ts +53 -8
  225. package/src/proxy.ts +628 -383
  226. package/src/publish/index.ts +274 -0
  227. package/src/publish/publish.ts +1060 -0
  228. package/src/pubsub/index.ts +286 -0
  229. package/src/pubsub/pubsub-commands.ts +623 -0
  230. package/src/queues/cli/commands/consumer/add.ts +71 -0
  231. package/src/queues/cli/commands/consumer/index.ts +19 -0
  232. package/src/queues/cli/commands/consumer/remove.ts +31 -0
  233. package/src/queues/cli/commands/create.ts +25 -0
  234. package/src/queues/cli/commands/delete.ts +26 -0
  235. package/src/queues/cli/commands/index.ts +35 -0
  236. package/src/queues/cli/commands/list.ts +25 -0
  237. package/src/queues/client.ts +136 -0
  238. package/src/queues/utils.ts +18 -0
  239. package/src/r2/constants.ts +4 -0
  240. package/src/r2/helpers.ts +132 -0
  241. package/src/r2/index.ts +289 -0
  242. package/src/routes.ts +140 -0
  243. package/src/secret/index.ts +376 -0
  244. package/src/selfsigned.d.ts +23 -23
  245. package/src/sites.ts +372 -0
  246. package/src/tail/createTail.ts +406 -0
  247. package/src/tail/filters.ts +109 -111
  248. package/src/tail/index.ts +208 -296
  249. package/src/tail/printing.ts +101 -46
  250. package/src/traverse-module-graph.ts +53 -0
  251. package/src/tsconfig-sanity.ts +16 -0
  252. package/src/type-generation.ts +181 -0
  253. package/src/update-check.ts +11 -11
  254. package/src/user/access.ts +68 -0
  255. package/src/user/auth-variables.ts +113 -0
  256. package/src/user/choose-account.tsx +39 -0
  257. package/src/user/generate-auth-url.ts +33 -0
  258. package/src/user/generate-random-state.ts +16 -0
  259. package/src/user/index.ts +2 -0
  260. package/src/user/user.ts +1233 -0
  261. package/src/utils/collectKeyValues.ts +14 -0
  262. package/src/utils/render.ts +93 -0
  263. package/src/whoami.ts +135 -0
  264. package/src/worker.ts +203 -101
  265. package/src/yargs-types.ts +37 -0
  266. package/src/zones.ts +191 -0
  267. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  268. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  269. package/templates/__tests__/tsconfig.json +8 -0
  270. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  271. package/templates/checked-fetch.js +23 -0
  272. package/templates/d1-beta-facade.js +244 -0
  273. package/templates/facade.d.ts +14 -0
  274. package/templates/first-party-worker-module-facade.ts +19 -0
  275. package/templates/format-dev-errors.ts +34 -0
  276. package/templates/gitignore +1 -0
  277. package/templates/init-tests/test-jest-new-worker.js +23 -0
  278. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  279. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  280. package/templates/middleware/common.ts +62 -0
  281. package/templates/middleware/loader-modules.ts +84 -0
  282. package/templates/middleware/loader-sw.ts +233 -0
  283. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  284. package/templates/middleware/middleware-pretty-error.ts +40 -0
  285. package/templates/middleware/middleware-scheduled.ts +15 -0
  286. package/templates/new-worker-scheduled.js +17 -0
  287. package/templates/new-worker-scheduled.ts +32 -0
  288. package/templates/new-worker.js +3 -3
  289. package/templates/new-worker.ts +21 -3
  290. package/templates/no-op-worker.js +10 -0
  291. package/templates/pages-dev-pipeline.ts +31 -0
  292. package/templates/pages-dev-util.ts +52 -0
  293. package/templates/pages-shim.ts +12 -0
  294. package/templates/pages-template-plugin.ts +180 -0
  295. package/templates/pages-template-worker.ts +189 -0
  296. package/templates/serve-static-assets.ts +66 -0
  297. package/templates/service-bindings-module-facade.js +55 -0
  298. package/templates/service-bindings-sw-facade.js +42 -0
  299. package/templates/tsconfig-sanity.ts +11 -0
  300. package/templates/tsconfig.init.json +105 -0
  301. package/templates/tsconfig.json +6 -103
  302. package/templates/tsconfig.tsbuildinfo +1 -0
  303. package/wrangler-dist/cli.d.ts +1641 -0
  304. package/wrangler-dist/cli.js +129145 -89350
  305. package/wrangler-dist/wasm-sync.wasm +0 -0
  306. package/pages/functions/buildPlugin.ts +0 -92
  307. package/pages/functions/buildWorker.ts +0 -138
  308. package/pages/functions/filepath-routing.ts +0 -189
  309. package/pages/functions/identifiers.ts +0 -78
  310. package/pages/functions/routes.ts +0 -156
  311. package/pages/functions/template-plugin.ts +0 -146
  312. package/pages/functions/template-worker.ts +0 -143
  313. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  314. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  315. package/src/__tests__/pages.test.ts +0 -327
  316. package/src/dialogs.tsx +0 -87
  317. package/src/environment-variables.ts +0 -35
  318. package/src/index.tsx +0 -2698
  319. package/src/kv.ts +0 -319
  320. package/src/miniflare-cli/enum-keys.ts +0 -17
  321. package/src/pages.tsx +0 -1875
  322. package/src/publish.ts +0 -469
  323. package/src/r2.ts +0 -50
  324. package/src/sites.tsx +0 -293
  325. package/src/user.tsx +0 -1203
  326. package/src/whoami.tsx +0 -70
  327. package/templates/static-asset-facade.js +0 -47
  328. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  329. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  330. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  331. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  332. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  333. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  334. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  335. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  336. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  337. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  338. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  339. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  340. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  341. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  342. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  343. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  344. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  345. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  346. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  347. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  348. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  349. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  350. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  351. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  352. package/vendor/wrangler-mime/LICENSE +0 -21
  353. package/vendor/wrangler-mime/Mime.js +0 -97
  354. package/vendor/wrangler-mime/README.md +0 -187
  355. package/vendor/wrangler-mime/cli.js +0 -46
  356. package/vendor/wrangler-mime/index.js +0 -4
  357. package/vendor/wrangler-mime/lite.js +0 -4
  358. package/vendor/wrangler-mime/package.json +0 -52
  359. package/vendor/wrangler-mime/types/other.js +0 -1
  360. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,139 +1,178 @@
1
1
  import * as fs from "node:fs";
2
- import * as fsp from "node:fs/promises";
3
2
  import path from "node:path";
4
3
  import * as TOML from "@iarna/toml";
5
- import { execa } from "execa";
4
+ import { execa, execaSync } from "execa";
5
+ import { rest } from "msw";
6
6
  import { parseConfigFileTextToJson } from "typescript";
7
7
  import { version as wranglerVersion } from "../../package.json";
8
8
  import { getPackageManager } from "../package-manager";
9
+ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
9
10
  import { mockConsoleMethods } from "./helpers/mock-console";
10
- import { mockConfirm, clearConfirmMocks } from "./helpers/mock-dialogs";
11
+ import { clearDialogs, mockConfirm, mockSelect } from "./helpers/mock-dialogs";
12
+ import { useMockIsTTY } from "./helpers/mock-istty";
13
+ import { msw } from "./helpers/msw";
11
14
  import { runInTempDir } from "./helpers/run-in-tmp";
12
15
  import { runWrangler } from "./helpers/run-wrangler";
13
16
  import type { PackageManager } from "../package-manager";
14
17
 
18
+ /**
19
+ * An expectation matcher for the minimal generated wrangler.toml.
20
+ */
21
+ const MINIMAL_WRANGLER_TOML = {
22
+ compatibility_date: expect.any(String),
23
+ name: expect.stringContaining("wrangler-tests"),
24
+ main: "src/index.ts",
25
+ };
26
+
15
27
  describe("init", () => {
16
- let mockPackageManager: PackageManager;
17
- runInTempDir();
18
-
19
- beforeEach(() => {
20
- mockPackageManager = {
21
- cwd: process.cwd(),
22
- // @ts-expect-error we're making a fake package manager here
23
- type: "mockpm",
24
- addDevDeps: jest.fn(),
25
- install: jest.fn(),
26
- };
27
- (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
28
- });
29
-
30
- afterEach(() => {
31
- clearConfirmMocks();
32
- });
33
-
34
- const std = mockConsoleMethods();
35
-
36
- describe("options", () => {
37
- it("should initialize with no interactive prompts if `--yes` is used", async () => {
38
- await runWrangler("init --yes");
39
-
40
- expect(fs.existsSync("./src/index.js")).toBe(false);
41
- expect(fs.existsSync("./src/index.ts")).toBe(true);
42
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
43
- expect(fs.existsSync("./package.json")).toBe(true);
44
- expect(fs.existsSync("./wrangler.toml")).toBe(true);
45
- expect(std.out).toMatchInlineSnapshot(`
46
- "✨ Created wrangler.toml
47
- ✨ Initialized git repository
48
- ✨ Created package.json
49
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
50
- ✨ Created src/index.ts
51
-
52
- To start developing your Worker, run \`npm start\`
53
- To publish your Worker to the Internet, run \`npm run publish\`"
54
- `);
55
- expect(std.err).toMatchInlineSnapshot(`""`);
56
- expect(std.warn).toMatchInlineSnapshot(`""`);
57
- });
58
-
59
- it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
60
- await runWrangler("init my-worker --yes");
61
-
62
- expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
63
- expect(fs.existsSync("./my-worker/src/index.ts")).toBe(true);
64
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(true);
65
- expect(fs.existsSync("./my-worker/package.json")).toBe(true);
66
- expect(fs.existsSync("./my-worker/wrangler.toml")).toBe(true);
67
- const parsedWranglerToml = TOML.parse(
68
- fs.readFileSync("./my-worker/wrangler.toml", "utf-8")
69
- );
70
- expect(parsedWranglerToml.main).toEqual("src/index.ts");
71
- expect(std.out).toMatchInlineSnapshot(`
72
- "✨ Created my-worker/wrangler.toml
73
- ✨ Initialized git repository at my-worker
74
- ✨ Created my-worker/package.json
75
- ✨ Created my-worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies
76
- ✨ Created my-worker/src/index.ts
77
-
78
- To start developing your Worker, run \`cd my-worker && npm start\`
79
- To publish your Worker to the Internet, run \`npm run publish\`"
80
- `);
81
- expect(std.err).toMatchInlineSnapshot(`""`);
82
- expect(std.warn).toMatchInlineSnapshot(`""`);
83
- });
84
-
85
- it("should initialize with no interactive prompts if `-y` is used", async () => {
86
- await runWrangler("init -y");
87
-
88
- expect(fs.existsSync("./src/index.js")).toBe(false);
89
- expect(fs.existsSync("./src/index.ts")).toBe(true);
90
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
91
- expect(fs.existsSync("./package.json")).toBe(true);
92
- expect(fs.existsSync("./wrangler.toml")).toBe(true);
93
- expect(std).toMatchInlineSnapshot(`
94
- Object {
95
- "debug": "",
96
- "err": "",
97
- "out": "✨ Created wrangler.toml
98
- ✨ Initialized git repository
99
- ✨ Created package.json
100
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
101
- ✨ Created src/index.ts
102
-
103
- To start developing your Worker, run \`npm start\`
104
- To publish your Worker to the Internet, run \`npm run publish\`",
105
- "warn": "",
106
- }
107
- `);
108
- });
109
-
110
- it("should error if `--type javascript` is used", async () => {
111
- await expect(
112
- runWrangler("init --type javascript")
113
- ).rejects.toThrowErrorMatchingInlineSnapshot(
114
- `"The --type option is no longer supported."`
115
- );
116
- });
117
-
118
- it("should error if `--type rust` is used", async () => {
119
- await expect(
120
- runWrangler("init --type rust")
121
- ).rejects.toThrowErrorMatchingInlineSnapshot(
122
- `"The --type option is no longer supported."`
123
- );
124
- });
125
-
126
- it("should error if `--type webpack` is used", async () => {
127
- await expect(runWrangler("init --type webpack")).rejects
128
- .toThrowErrorMatchingInlineSnapshot(`
28
+ let mockPackageManager: PackageManager;
29
+ runInTempDir();
30
+ const { setIsTTY } = useMockIsTTY();
31
+
32
+ beforeEach(() => {
33
+ setIsTTY(true);
34
+
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
+ clearDialogs();
47
+ });
48
+
49
+ const std = mockConsoleMethods();
50
+
51
+ describe("options", () => {
52
+ it("should initialize with no interactive prompts if `--yes` is used", async () => {
53
+ await runWrangler("init --yes");
54
+
55
+ checkFiles({
56
+ items: {
57
+ "./src/index.js": false,
58
+ "./src/index.ts": true,
59
+ "./tsconfig.json": true,
60
+ "./package.json": true,
61
+ "./wrangler.toml": true,
62
+ },
63
+ });
64
+
65
+ expect(std.out).toMatchInlineSnapshot(`
66
+ "✨ Created wrangler.toml
67
+ ✨ Initialized git repository
68
+ ✨ Created package.json
69
+ ✨ Created tsconfig.json
70
+ ✨ Created src/index.ts
71
+ ✨ Created src/index.test.ts
72
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
73
+
74
+ To start developing your Worker, run \`npm start\`
75
+ To start testing your Worker, run \`npm test\`
76
+ To publish your Worker to the Internet, run \`npm run deploy\`"
77
+ `);
78
+ expect(std.err).toMatchInlineSnapshot(`""`);
79
+ expect(std.warn).toMatchInlineSnapshot(`""`);
80
+ });
81
+
82
+ it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
83
+ await runWrangler("init my-worker --yes");
84
+
85
+ checkFiles({
86
+ items: {
87
+ "./my-worker/src/index.js": false,
88
+ "./my-worker/src/index.ts": true,
89
+ "./my-worker/tsconfig.json": true,
90
+ "./my-worker/package.json": true,
91
+ "./my-worker/wrangler.toml": wranglerToml({
92
+ ...MINIMAL_WRANGLER_TOML,
93
+ name: "my-worker",
94
+ }),
95
+ },
96
+ });
97
+
98
+ expect(std.out).toMatchInlineSnapshot(`
99
+ "✨ Created my-worker/wrangler.toml
100
+ ✨ Initialized git repository at my-worker
101
+ ✨ Created my-worker/package.json
102
+ ✨ Created my-worker/tsconfig.json
103
+ ✨ Created my-worker/src/index.ts
104
+ ✨ Created my-worker/src/index.test.ts
105
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
106
+
107
+ To start developing your Worker, run \`cd my-worker && npm start\`
108
+ To start testing your Worker, run \`npm test\`
109
+ To publish your Worker to the Internet, run \`npm run deploy\`"
110
+ `);
111
+ expect(std.err).toMatchInlineSnapshot(`""`);
112
+ expect(std.warn).toMatchInlineSnapshot(`""`);
113
+ });
114
+
115
+ it("should initialize with no interactive prompts if `-y` is used", async () => {
116
+ await runWrangler("init -y");
117
+
118
+ checkFiles({
119
+ items: {
120
+ "./src/index.js": false,
121
+ "./src/index.ts": true,
122
+ "./tsconfig.json": true,
123
+ "./package.json": true,
124
+ "./wrangler.toml": true,
125
+ },
126
+ });
127
+
128
+ expect(std).toMatchInlineSnapshot(`
129
+ Object {
130
+ "debug": "",
131
+ "err": "",
132
+ "info": "Your project will use Vitest to run your tests.",
133
+ "out": "✨ Created wrangler.toml
134
+ ✨ Initialized git repository
135
+ ✨ Created package.json
136
+ ✨ Created tsconfig.json
137
+ ✨ Created src/index.ts
138
+ ✨ Created src/index.test.ts
139
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
140
+
141
+ To start developing your Worker, run \`npm start\`
142
+ To start testing your Worker, run \`npm test\`
143
+ To publish your Worker to the Internet, run \`npm run deploy\`",
144
+ "warn": "",
145
+ }
146
+ `);
147
+ });
148
+
149
+ it("should error if `--type javascript` is used", async () => {
150
+ await expect(
151
+ runWrangler("init --type javascript")
152
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
153
+ `"The --type option is no longer supported."`
154
+ );
155
+ });
156
+
157
+ it("should error if `--type rust` is used", async () => {
158
+ await expect(
159
+ runWrangler("init --type rust")
160
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
161
+ `"The --type option is no longer supported."`
162
+ );
163
+ });
164
+
165
+ it("should error if `--type webpack` is used", async () => {
166
+ await expect(runWrangler("init --type webpack")).rejects
167
+ .toThrowErrorMatchingInlineSnapshot(`
129
168
  "The --type option is no longer supported.
130
169
  If you wish to use webpack then you will need to create a custom build."
131
170
  `);
132
- });
171
+ });
133
172
 
134
- it("should error if `--site` is used", async () => {
135
- await expect(runWrangler("init --site")).rejects
136
- .toThrowErrorMatchingInlineSnapshot(`
173
+ it("should error if `--site` is used", async () => {
174
+ await expect(runWrangler("init --site")).rejects
175
+ .toThrowErrorMatchingInlineSnapshot(`
137
176
  "The --site option is no longer supported.
138
177
  If you wish to create a brand new Worker Sites project then clone the \`worker-sites-template\` starter repository:
139
178
 
@@ -145,1372 +184,3013 @@ describe("init", () => {
145
184
  Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.
146
185
  Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/."
147
186
  `);
148
- });
149
- });
150
-
151
- describe("wrangler.toml", () => {
152
- it("should create a wrangler.toml", async () => {
153
- mockConfirm(
154
- {
155
- text: "Would you like to use git to manage this Worker?",
156
- result: false,
157
- },
158
- {
159
- text: "No package.json found. Would you like to create one?",
160
- result: false,
161
- }
162
- );
163
- await runWrangler("init");
164
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
165
- expect(typeof parsed.compatibility_date).toBe("string");
166
- expect(parsed.name).toContain("wrangler-tests");
167
- expect(fs.existsSync("./package.json")).toBe(false);
168
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
169
- expect(std).toMatchInlineSnapshot(`
170
- Object {
171
- "debug": "",
172
- "err": "",
173
- "out": "✨ Created wrangler.toml",
174
- "warn": "",
175
- }
176
- `);
177
- });
178
-
179
- it("should create a wrangler.toml and a directory for a named Worker ", async () => {
180
- mockConfirm(
181
- {
182
- text: "Would you like to use git to manage this Worker?",
183
- result: false,
184
- },
185
- {
186
- text: "No package.json found. Would you like to create one?",
187
- result: false,
188
- }
189
- );
190
- await runWrangler("init my-worker");
191
- const parsed = TOML.parse(
192
- await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
193
- );
194
- expect(typeof parsed.compatibility_date).toBe("string");
195
- expect(parsed.name).toBe("my-worker");
196
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
197
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
198
-
199
- expect(std).toMatchInlineSnapshot(`
200
- Object {
201
- "debug": "",
202
- "err": "",
203
- "out": "✨ Created my-worker/wrangler.toml",
204
- "warn": "",
205
- }
206
- `);
207
- });
208
-
209
- it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
210
- fs.writeFileSync(
211
- "./wrangler.toml",
212
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
213
- "utf-8"
214
- );
215
- mockConfirm({
216
- text: "Do you want to continue initializing this project?",
217
- result: false,
218
- });
219
- await runWrangler("init");
220
- expect(std.warn).toContain("wrangler.toml already exists!");
221
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
222
- expect(parsed.compatibility_date).toBe("something-else");
223
-
224
- expect(std).toMatchInlineSnapshot(`
225
- Object {
226
- "debug": "",
227
- "err": "",
228
- "out": "",
229
- "warn": "▲ [WARNING] wrangler.toml already exists!
230
-
231
- ",
232
- }
233
- `);
234
- });
235
-
236
- it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
237
- fs.mkdirSync("path/to/worker", { recursive: true });
238
- fs.writeFileSync(
239
- "path/to/worker/wrangler.toml",
240
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
241
- "utf-8"
242
- );
243
- mockConfirm({
244
- text: "Do you want to continue initializing this project?",
245
- result: false,
246
- });
247
- await runWrangler("init path/to/worker");
248
- expect(std.warn).toContain("wrangler.toml already exists!");
249
- const parsed = TOML.parse(
250
- await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
251
- );
252
- expect(parsed.compatibility_date).toBe("something-else");
253
- expect(std).toMatchInlineSnapshot(`
254
- Object {
255
- "debug": "",
256
- "err": "",
257
- "out": "",
258
- "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
259
-
260
- ",
261
- }
262
- `);
263
- });
264
-
265
- it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
266
- fs.writeFileSync(
267
- "./wrangler.toml",
268
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
269
- "utf-8"
270
- );
271
- mockConfirm(
272
- {
273
- text: "Would you like to use git to manage this Worker?",
274
- result: true,
275
- },
276
- {
277
- text: "Do you want to continue initializing this project?",
278
- result: true,
279
- },
280
- {
281
- text: "No package.json found. Would you like to create one?",
282
- result: true,
283
- },
284
- {
285
- text: "Would you like to use TypeScript?",
286
- result: true,
287
- },
288
- {
289
- text: "Would you like to create a Worker at src/index.ts?",
290
- result: true,
291
- }
292
- );
293
-
294
- await runWrangler("init");
295
- expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
296
- `"compatibility_date=\\"something-else\\""`
297
- );
298
- });
299
-
300
- it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
301
- fs.writeFileSync(
302
- "./wrangler.toml",
303
- `compatibility_date="something-else"`,
304
- "utf-8"
305
- );
306
- mockConfirm(
307
- {
308
- text: "Do you want to continue initializing this project?",
309
- result: true,
310
- },
311
- {
312
- text: "Would you like to use git to manage this Worker?",
313
- result: false,
314
- },
315
- {
316
- text: "No package.json found. Would you like to create one?",
317
- result: false,
318
- }
319
- );
320
- await runWrangler("init");
321
- expect(std.warn).toContain("wrangler.toml already exists!");
322
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
323
- expect(parsed.compatibility_date).toBe("something-else");
324
- expect(std).toMatchInlineSnapshot(`
325
- Object {
326
- "debug": "",
327
- "err": "",
328
- "out": "",
329
- "warn": "▲ [WARNING] wrangler.toml already exists!
330
-
331
- ",
332
- }
333
- `);
334
- });
335
- });
336
-
337
- describe("git init", () => {
338
- it("should offer to initialize a git repository", async () => {
339
- mockConfirm(
340
- {
341
- text: "Would you like to use git to manage this Worker?",
342
- result: true,
343
- },
344
- {
345
- text: "No package.json found. Would you like to create one?",
346
- result: false,
347
- }
348
- );
349
- await runWrangler("init");
350
- expect(std).toMatchInlineSnapshot(`
351
- Object {
352
- "debug": "",
353
- "err": "",
354
- "out": "✨ Created wrangler.toml
355
- ✨ Initialized git repository",
356
- "warn": "",
357
- }
358
- `);
359
- expect(fs.lstatSync(".git").isDirectory()).toBe(true);
360
- expect(fs.lstatSync(".gitignore").isFile()).toBe(true);
361
- });
362
-
363
- it("should not offer to initialize a git repo if it's already inside one", async () => {
364
- await execa("git", ["init"]);
365
- fs.mkdirSync("some-folder");
366
- process.chdir("some-folder");
367
- await runWrangler("init -y");
368
-
369
- // Note the lack of "✨ Initialized git repository" in the log
370
- expect(std).toMatchInlineSnapshot(`
371
- Object {
372
- "debug": "",
373
- "err": "",
374
- "out": "✨ Created wrangler.toml
375
- ✨ Created package.json
376
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
377
- ✨ Created src/index.ts
378
-
379
- To start developing your Worker, run \`npm start\`
380
- To publish your Worker to the Internet, run \`npm run publish\`",
381
- "warn": "",
382
- }
383
- `);
384
- });
385
-
386
- it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
387
- fs.mkdirSync("path/to/worker", { recursive: true });
388
- await execa("git", ["init"], { cwd: "path/to/worker" });
389
- expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
390
-
391
- await runWrangler("init path/to/worker/my-worker -y");
392
-
393
- // Note the lack of "✨ Initialized git repository" in the log
394
- expect(std).toMatchInlineSnapshot(`
395
- Object {
396
- "debug": "",
397
- "err": "",
398
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
399
- ✨ Created path/to/worker/my-worker/package.json
400
- ✨ Created path/to/worker/my-worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies
401
- ✨ Created path/to/worker/my-worker/src/index.ts
402
-
403
- To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
404
- To publish your Worker to the Internet, run \`npm run publish\`",
405
- "warn": "",
406
- }
407
- `);
408
- });
409
-
410
- it.todo(
411
- "should not offer to initialize a git repo if git is not installed"
412
- );
413
- // I... don't know how to test this lol
414
- });
415
-
416
- describe("package.json", () => {
417
- it("should create a package.json if none is found and user confirms", async () => {
418
- mockConfirm(
419
- {
420
- text: "Would you like to use git to manage this Worker?",
421
- result: false,
422
- },
423
- {
424
- text: "No package.json found. Would you like to create one?",
425
- result: true,
426
- },
427
- {
428
- text: "Would you like to use TypeScript?",
429
- result: false,
430
- },
431
- {
432
- text: "Would you like to create a Worker at src/index.js?",
433
- result: false,
434
- }
435
- );
436
- await runWrangler("init");
437
- expect(fs.existsSync("./package.json")).toBe(true);
438
- const packageJson = JSON.parse(
439
- fs.readFileSync("./package.json", "utf-8")
440
- );
441
- expect(packageJson.name).toContain("wrangler-tests");
442
- expect(packageJson.version).toEqual("0.0.0");
443
- expect(packageJson.devDependencies).toEqual({
444
- wrangler: expect.any(String),
445
- });
446
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
447
- expect(mockPackageManager.install).toHaveBeenCalled();
448
- expect(std).toMatchInlineSnapshot(`
449
- Object {
450
- "debug": "",
451
- "err": "",
452
- "out": "✨ Created wrangler.toml
453
- ✨ Created package.json",
454
- "warn": "",
455
- }
456
- `);
457
- });
458
-
459
- it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
460
- mockConfirm(
461
- {
462
- text: "Would you like to use git to manage this Worker?",
463
- result: false,
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: false,
472
- },
473
- {
474
- text: "Would you like to create a Worker at my-worker/src/index.js?",
475
- result: false,
476
- }
477
- );
478
- await runWrangler("init my-worker");
479
- const packageJson = JSON.parse(
480
- fs.readFileSync("./my-worker/package.json", "utf-8")
481
- );
482
- expect(packageJson.name).toBe("my-worker");
483
- expect(std).toMatchInlineSnapshot(`
484
- Object {
485
- "debug": "",
486
- "err": "",
487
- "out": "✨ Created my-worker/wrangler.toml
488
- ✨ Created my-worker/package.json",
489
- "warn": "",
490
- }
491
- `);
492
- });
493
-
494
- it("should not touch an existing package.json in the same directory", async () => {
495
- mockConfirm(
496
- {
497
- text: "Would you like to use git to manage this Worker?",
498
- result: false,
499
- },
500
- {
501
- text: "Would you like to install wrangler into package.json?",
502
- result: false,
503
- },
504
- {
505
- text: "Would you like to use TypeScript?",
506
- result: false,
507
- },
508
- {
509
- text: "Would you like to create a Worker at src/index.js?",
510
- result: false,
511
- }
512
- );
513
-
514
- fs.writeFileSync(
515
- "./package.json",
516
- JSON.stringify({ name: "test", version: "1.0.0" }),
517
- "utf-8"
518
- );
519
-
520
- await runWrangler("init");
521
- const packageJson = JSON.parse(
522
- fs.readFileSync("./package.json", "utf-8")
523
- );
524
- expect(packageJson.name).toEqual("test");
525
- expect(packageJson.version).toEqual("1.0.0");
526
- expect(std).toMatchInlineSnapshot(`
527
- Object {
528
- "debug": "",
529
- "err": "",
530
- "out": "✨ Created wrangler.toml",
531
- "warn": "",
532
- }
533
- `);
534
- });
535
-
536
- it("should not touch an existing package.json in a target directory", async () => {
537
- mockConfirm(
538
- {
539
- text: "Would you like to use git to manage this Worker?",
540
- result: false,
541
- },
542
- {
543
- text: "Would you like to install wrangler into path/to/worker/package.json?",
544
- result: false,
545
- },
546
- {
547
- text: "Would you like to use TypeScript?",
548
- result: false,
549
- },
550
- {
551
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
552
- result: false,
553
- }
554
- );
555
-
556
- fs.mkdirSync("path/to/worker", { recursive: true });
557
- fs.writeFileSync(
558
- "path/to/worker/package.json",
559
- JSON.stringify({ name: "test", version: "1.0.0" }),
560
- "utf-8"
561
- );
562
-
563
- await runWrangler("init path/to/worker/my-worker");
564
- const packageJson = JSON.parse(
565
- fs.readFileSync("path/to/worker/package.json", "utf-8")
566
- );
567
- expect(packageJson.name).toEqual("test");
568
- expect(packageJson.version).toEqual("1.0.0");
569
- expect(std).toMatchInlineSnapshot(`
570
- Object {
571
- "debug": "",
572
- "err": "",
573
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml",
574
- "warn": "",
575
- }
576
- `);
577
- });
578
-
579
- it("should offer to install wrangler into an existing package.json", async () => {
580
- mockConfirm(
581
- {
582
- text: "Would you like to use git to manage this Worker?",
583
- result: false,
584
- },
585
- {
586
- text: "Would you like to install wrangler into package.json?",
587
- result: true,
588
- },
589
- {
590
- text: "Would you like to use TypeScript?",
591
- result: false,
592
- },
593
- {
594
- text: "Would you like to create a Worker at src/index.js?",
595
- result: false,
596
- }
597
- );
598
-
599
- fs.writeFileSync(
600
- "./package.json",
601
- JSON.stringify({ name: "test", version: "1.0.0" }),
602
- "utf-8"
603
- );
604
-
605
- await runWrangler("init");
606
- const packageJson = JSON.parse(
607
- fs.readFileSync("./package.json", "utf-8")
608
- );
609
- expect(packageJson.name).toEqual("test");
610
- expect(packageJson.version).toEqual("1.0.0");
611
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
612
- `wrangler@${wranglerVersion}`
613
- );
614
- expect(std).toMatchInlineSnapshot(`
615
- Object {
616
- "debug": "",
617
- "err": "",
618
- "out": "✨ Created wrangler.toml
619
- ✨ Installed wrangler",
620
- "warn": "",
621
- }
622
- `);
623
- });
624
-
625
- it("should offer to install wrangler into a package.json relative to the target directory", async () => {
626
- mockConfirm(
627
- {
628
- text: "Would you like to use git to manage this Worker?",
629
- result: false,
630
- },
631
- {
632
- text: "Would you like to install wrangler into path/to/worker/package.json?",
633
- result: true,
634
- },
635
- {
636
- text: "Would you like to use TypeScript?",
637
- result: false,
638
- },
639
- {
640
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
641
- result: false,
642
- }
643
- );
644
-
645
- fs.mkdirSync("path/to/worker", { recursive: true });
646
- fs.writeFileSync(
647
- "path/to/worker/package.json",
648
- JSON.stringify({ name: "test", version: "1.0.0" }),
649
- "utf-8"
650
- );
651
-
652
- await runWrangler("init path/to/worker/my-worker");
653
- const packageJson = JSON.parse(
654
- fs.readFileSync("path/to/worker/package.json", "utf-8")
655
- );
656
- expect(packageJson.name).toEqual("test");
657
- expect(packageJson.version).toEqual("1.0.0");
658
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
659
- `wrangler@${wranglerVersion}`
660
- );
661
- expect(mockPackageManager.cwd).toBe(process.cwd());
662
- expect(std).toMatchInlineSnapshot(`
663
- Object {
664
- "debug": "",
665
- "err": "",
666
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
667
- ✨ Installed wrangler",
668
- "warn": "",
669
- }
670
- `);
671
- });
672
-
673
- it("should not touch an existing package.json in an ancestor directory", async () => {
674
- mockConfirm(
675
- {
676
- text: "Would you like to use git to manage this Worker?",
677
- result: false,
678
- },
679
- {
680
- text: "Would you like to install wrangler into ../../package.json?",
681
- result: false,
682
- },
683
- {
684
- text: "Would you like to use TypeScript?",
685
- result: false,
686
- },
687
- {
688
- text: "Would you like to create a Worker at src/index.js?",
689
- result: false,
690
- }
691
- );
692
-
693
- fs.writeFileSync(
694
- "./package.json",
695
- JSON.stringify({ name: "test", version: "1.0.0" }),
696
- "utf-8"
697
- );
698
-
699
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
700
- process.chdir("./sub-1/sub-2");
701
-
702
- await runWrangler("init");
703
- expect(fs.existsSync("./package.json")).toBe(false);
704
- expect(fs.existsSync("../../package.json")).toBe(true);
705
-
706
- const packageJson = JSON.parse(
707
- fs.readFileSync("../../package.json", "utf-8")
708
- );
709
- expect(packageJson).toMatchInlineSnapshot(`
710
- Object {
711
- "name": "test",
712
- "version": "1.0.0",
713
- }
714
- `);
715
- expect(std).toMatchInlineSnapshot(`
716
- Object {
717
- "debug": "",
718
- "err": "",
719
- "out": "✨ Created wrangler.toml",
720
- "warn": "",
721
- }
722
- `);
723
- });
724
- });
725
-
726
- describe("typescript", () => {
727
- it("should offer to create a worker in a non-typescript project", async () => {
728
- mockConfirm(
729
- {
730
- text: "Would you like to use git to manage this Worker?",
731
- result: false,
732
- },
733
- {
734
- text: "Would you like to install wrangler into package.json?",
735
- result: false,
736
- },
737
- {
738
- text: "Would you like to use TypeScript?",
739
- result: false,
740
- },
741
- {
742
- text: "Would you like to create a Worker at src/index.js?",
743
- result: true,
744
- }
745
- );
746
-
747
- fs.writeFileSync(
748
- "./package.json",
749
- JSON.stringify({ name: "test", version: "1.0.0" }),
750
- "utf-8"
751
- );
752
-
753
- await runWrangler("init");
754
- expect(fs.existsSync("./src/index.js")).toBe(true);
755
- expect(fs.existsSync("./src/index.ts")).toBe(false);
756
- expect(std).toMatchInlineSnapshot(`
757
- Object {
758
- "debug": "",
759
- "err": "",
760
- "out": "✨ Created wrangler.toml
761
- ✨ Created src/index.js
762
-
763
- To start developing your Worker, run \`npx wrangler dev\`
764
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
765
- "warn": "",
766
- }
767
- `);
768
- });
769
-
770
- it("should offer to create a worker in a typescript project", async () => {
771
- mockConfirm(
772
- {
773
- text: "Would you like to use git to manage this Worker?",
774
- result: false,
775
- },
776
- {
777
- text: "Would you like to install wrangler into package.json?",
778
- result: false,
779
- },
780
- {
781
- text: "Would you like to use TypeScript?",
782
- result: true,
783
- },
784
- {
785
- text: "Would you like to create a Worker at src/index.ts?",
786
- result: true,
787
- }
788
- );
789
-
790
- fs.writeFileSync(
791
- "./package.json",
792
- JSON.stringify({ name: "test", version: "1.0.0" }),
793
- "utf-8"
794
- );
795
-
796
- await runWrangler("init");
797
- expect(fs.existsSync("./src/index.js")).toBe(false);
798
- expect(fs.existsSync("./src/index.ts")).toBe(true);
799
- expect(std).toMatchInlineSnapshot(`
800
- Object {
801
- "debug": "",
802
- "err": "",
803
- "out": "✨ Created wrangler.toml
804
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
805
- ✨ Created src/index.ts
806
-
807
- To start developing your Worker, run \`npx wrangler dev\`
808
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
809
- "warn": "",
810
- }
811
- `);
812
- });
813
-
814
- it("should add scripts for a typescript project with .ts extension", async () => {
815
- mockConfirm(
816
- {
817
- text: "Would you like to use git to manage this Worker?",
818
- result: false,
819
- },
820
- {
821
- text: "No package.json found. Would you like to create one?",
822
- result: true,
823
- },
824
- {
825
- text: "Would you like to install wrangler into package.json?",
826
- result: false,
827
- },
828
- {
829
- text: "Would you like to use TypeScript?",
830
- result: true,
831
- },
832
- {
833
- text: "Would you like to create a Worker at src/index.ts?",
834
- result: true,
835
- }
836
- );
837
- await runWrangler("init");
838
-
839
- expect(fs.existsSync("./package.json")).toBe(true);
840
- const packageJson = JSON.parse(
841
- fs.readFileSync("./package.json", "utf-8")
842
- );
843
-
844
- expect(fs.existsSync("./src/index.js")).toBe(false);
845
- expect(fs.existsSync("./src/index.ts")).toBe(true);
846
-
847
- expect(packageJson.scripts.start).toBe("wrangler dev");
848
- expect(packageJson.scripts.publish).toBe("wrangler publish");
849
- expect(packageJson.name).toContain("wrangler-tests");
850
- expect(packageJson.version).toEqual("0.0.0");
851
- expect(std.out).toMatchInlineSnapshot(`
852
- "✨ Created wrangler.toml
853
- ✨ Created package.json
854
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
855
- ✨ Created src/index.ts
856
-
857
- To start developing your Worker, run \`npm start\`
858
- To publish your Worker to the Internet, run \`npm run publish\`"
859
- `);
860
- });
861
-
862
- it("should not overwrite package.json scripts for a typescript project", async () => {
863
- mockConfirm(
864
- {
865
- text: "Would you like to use git to manage this Worker?",
866
- result: false,
867
- },
868
- {
869
- text: "Would you like to install wrangler into package.json?",
870
- result: false,
871
- },
872
- {
873
- text: "Would you like to use TypeScript?",
874
- result: true,
875
- },
876
- {
877
- text: "Would you like to create a Worker at src/index.ts?",
878
- result: true,
879
- }
880
- );
881
- await fsp.writeFile(
882
- "./package.json",
883
- JSON.stringify({
884
- scripts: {
885
- start: "test-start",
886
- publish: "test-publish",
887
- },
888
- })
889
- );
890
- const packageJson = JSON.parse(
891
- fs.readFileSync("./package.json", "utf-8")
892
- );
893
- await runWrangler("init");
894
-
895
- expect(fs.existsSync("./src/index.js")).toBe(false);
896
- expect(fs.existsSync("./src/index.ts")).toBe(true);
897
-
898
- expect(packageJson.scripts.start).toBe("test-start");
899
- expect(packageJson.scripts.publish).toBe("test-publish");
900
- expect(std.out).toMatchInlineSnapshot(`
901
- "✨ Created wrangler.toml
902
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
903
- ✨ Created src/index.ts
904
-
905
- To start developing your Worker, run \`npx wrangler dev\`
906
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
907
- `);
908
- });
909
-
910
- it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
911
- mockConfirm(
912
- {
913
- text: "Would you like to use git to manage this Worker?",
914
- result: false,
915
- },
916
- {
917
- text: "Would you like to install wrangler into package.json?",
918
- result: false,
919
- },
920
- {
921
- text: "Would you like to use TypeScript?",
922
- result: true,
923
- }
924
- );
925
-
926
- fs.writeFileSync(
927
- "./package.json",
928
- JSON.stringify({ name: "test", version: "1.0.0" }),
929
- "utf-8"
930
- );
931
- fs.mkdirSync("./src", { recursive: true });
932
- const PLACEHOLDER = "/* placeholder text */";
933
- fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
934
-
935
- await runWrangler("init");
936
- expect(fs.existsSync("./src/index.js")).toBe(false);
937
- expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
938
- expect(std).toMatchInlineSnapshot(`
939
- Object {
940
- "debug": "",
941
- "err": "",
942
- "out": "✨ Created wrangler.toml
943
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies",
944
- "warn": "",
945
- }
946
- `);
947
- });
948
-
949
- 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 () => {
950
- mockConfirm(
951
- {
952
- text: "Would you like to use git to manage this Worker?",
953
- result: false,
954
- },
955
- {
956
- text: "Would you like to install wrangler into package.json?",
957
- result: false,
958
- },
959
- {
960
- text: "Would you like to use TypeScript?",
961
- result: true,
962
- }
963
- );
964
-
965
- fs.writeFileSync(
966
- "./package.json",
967
- JSON.stringify({ name: "test", version: "1.0.0" }),
968
- "utf-8"
969
- );
970
- fs.mkdirSync("./my-worker/src", { recursive: true });
971
- const PLACEHOLDER = "/* placeholder text */";
972
- fs.writeFileSync("./my-worker/src/index.ts", PLACEHOLDER, "utf-8");
973
-
974
- await runWrangler("init my-worker");
975
- expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
976
- expect(fs.readFileSync("./my-worker/src/index.ts", "utf-8")).toBe(
977
- PLACEHOLDER
978
- );
979
- expect(std).toMatchInlineSnapshot(`
980
- Object {
981
- "debug": "",
982
- "err": "",
983
- "out": "✨ Created my-worker/wrangler.toml
984
- ✨ Created my-worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies",
985
- "warn": "",
986
- }
987
- `);
988
- });
989
-
990
- it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
991
- mockConfirm(
992
- {
993
- text: "Would you like to use git to manage this Worker?",
994
- result: false,
995
- },
996
- {
997
- text: "No package.json found. Would you like to create one?",
998
- result: true,
999
- },
1000
- {
1001
- text: "Would you like to use TypeScript?",
1002
- result: true,
1003
- },
1004
- {
1005
- text: "Would you like to create a Worker at src/index.ts?",
1006
- result: false,
1007
- }
1008
- );
1009
- await runWrangler("init");
1010
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
1011
- const { config: tsconfigJson, error: tsConfigParseError } =
1012
- parseConfigFileTextToJson(
1013
- "./tsconfig.json",
1014
- fs.readFileSync("./tsconfig.json", "utf-8")
1015
- );
1016
- expect(tsConfigParseError).toBeUndefined();
1017
- expect(tsconfigJson.compilerOptions.types).toEqual([
1018
- "@cloudflare/workers-types",
1019
- ]);
1020
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1021
- "@cloudflare/workers-types",
1022
- "typescript"
1023
- );
1024
- expect(std).toMatchInlineSnapshot(`
1025
- Object {
1026
- "debug": "",
1027
- "err": "",
1028
- "out": "✨ Created wrangler.toml
1029
- ✨ Created package.json
1030
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies",
1031
- "warn": "",
1032
- }
1033
- `);
1034
- });
1035
-
1036
- it("should not touch an existing tsconfig.json in the same directory", async () => {
1037
- fs.writeFileSync(
1038
- "./package.json",
1039
- JSON.stringify({
1040
- name: "test",
1041
- version: "1.0.0",
1042
- devDependencies: {
1043
- wrangler: "0.0.0",
1044
- "@cloudflare/workers-types": "0.0.0",
1045
- },
1046
- }),
1047
- "utf-8"
1048
- );
1049
- fs.writeFileSync(
1050
- "./tsconfig.json",
1051
- JSON.stringify({ compilerOptions: {} }),
1052
- "utf-8"
1053
- );
1054
-
1055
- mockConfirm(
1056
- {
1057
- text: "Would you like to use git to manage this Worker?",
1058
- result: false,
1059
- },
1060
- {
1061
- text: "Would you like to create a Worker at src/index.ts?",
1062
- result: true,
1063
- }
1064
- );
1065
-
1066
- await runWrangler("init");
1067
- const tsconfigJson = JSON.parse(
1068
- fs.readFileSync("./tsconfig.json", "utf-8")
1069
- );
1070
- expect(tsconfigJson.compilerOptions).toEqual({});
1071
- expect(std).toMatchInlineSnapshot(`
1072
- Object {
1073
- "debug": "",
1074
- "err": "",
1075
- "out": "✨ Created wrangler.toml
1076
- ✨ Created src/index.ts
1077
-
1078
- To start developing your Worker, run \`npx wrangler dev\`
1079
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1080
- "warn": "",
1081
- }
1082
- `);
1083
- });
1084
-
1085
- it("should not touch an existing tsconfig.json in the ancestor of a target directory", async () => {
1086
- fs.mkdirSync("path/to/worker", { recursive: true });
1087
- fs.writeFileSync(
1088
- "path/to/worker/package.json",
1089
- JSON.stringify({
1090
- name: "test",
1091
- version: "1.0.0",
1092
- devDependencies: {
1093
- wrangler: "0.0.0",
1094
- "@cloudflare/workers-types": "0.0.0",
1095
- },
1096
- }),
1097
- "utf-8"
1098
- );
1099
- fs.writeFileSync(
1100
- "path/to/worker/tsconfig.json",
1101
- JSON.stringify({ compilerOptions: {} }),
1102
- "utf-8"
1103
- );
1104
-
1105
- mockConfirm(
1106
- {
1107
- text: "Would you like to use git to manage this Worker?",
1108
- result: false,
1109
- },
1110
- {
1111
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
1112
- result: true,
1113
- }
1114
- );
1115
-
1116
- await runWrangler("init path/to/worker/my-worker");
1117
- const tsconfigJson = JSON.parse(
1118
- fs.readFileSync("path/to/worker/tsconfig.json", "utf-8")
1119
- );
1120
- expect(tsconfigJson.compilerOptions).toEqual({});
1121
- expect(std).toMatchInlineSnapshot(`
1122
- Object {
1123
- "debug": "",
1124
- "err": "",
1125
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1126
- ✨ Created path/to/worker/my-worker/src/index.ts
1127
-
1128
- To start developing your Worker, run \`npx wrangler dev\`
1129
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1130
- "warn": "",
1131
- }
1132
- `);
1133
- });
1134
-
1135
- it("should offer to install type definitions in an existing typescript project", async () => {
1136
- mockConfirm(
1137
- {
1138
- text: "Would you like to use git to manage this Worker?",
1139
- result: false,
1140
- },
1141
- {
1142
- text: "Would you like to install wrangler into package.json?",
1143
- result: false,
1144
- },
1145
- {
1146
- text: "Would you like to install the type definitions for Workers into your package.json?",
1147
- result: true,
1148
- },
1149
- {
1150
- text: "Would you like to create a Worker at src/index.ts?",
1151
- result: false,
1152
- }
1153
- );
1154
- fs.writeFileSync(
1155
- "./package.json",
1156
- JSON.stringify({
1157
- name: "test",
1158
- version: "1.0.0",
1159
- }),
1160
- "utf-8"
1161
- );
1162
- fs.writeFileSync(
1163
- "./tsconfig.json",
1164
- JSON.stringify({ compilerOptions: {} }),
1165
- "utf-8"
1166
- );
1167
-
1168
- await runWrangler("init");
1169
- const tsconfigJson = JSON.parse(
1170
- fs.readFileSync("./tsconfig.json", "utf-8")
1171
- );
1172
- // unchanged tsconfig
1173
- expect(tsconfigJson.compilerOptions).toEqual({});
1174
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1175
- "@cloudflare/workers-types"
1176
- );
1177
- expect(std).toMatchInlineSnapshot(`
1178
- Object {
1179
- "debug": "",
1180
- "err": "",
1181
- "out": "✨ Created wrangler.toml
1182
- ✨ Installed @cloudflare/workers-types.
1183
- Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1184
- "warn": "",
1185
- }
1186
- `);
1187
- });
1188
-
1189
- it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1190
- fs.writeFileSync(
1191
- "./package.json",
1192
- JSON.stringify({
1193
- name: "test",
1194
- version: "1.0.0",
1195
- devDependencies: {
1196
- wrangler: "0.0.0",
1197
- "@cloudflare/workers-types": "0.0.0",
1198
- },
1199
- }),
1200
- "utf-8"
1201
- );
1202
- fs.writeFileSync(
1203
- "./tsconfig.json",
1204
- JSON.stringify({ compilerOptions: {} }),
1205
- "utf-8"
1206
- );
1207
-
1208
- mockConfirm(
1209
- {
1210
- text: "Would you like to use git to manage this Worker?",
1211
- result: false,
1212
- },
1213
- {
1214
- text: "Would you like to create a Worker at src/index.ts?",
1215
- result: true,
1216
- }
1217
- );
1218
-
1219
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
1220
- process.chdir("./sub-1/sub-2");
1221
-
1222
- await runWrangler("init");
1223
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
1224
- expect(fs.existsSync("../../tsconfig.json")).toBe(true);
1225
-
1226
- const tsconfigJson = JSON.parse(
1227
- fs.readFileSync("../../tsconfig.json", "utf-8")
1228
- );
1229
- expect(tsconfigJson.compilerOptions).toEqual({});
1230
-
1231
- expect(std).toMatchInlineSnapshot(`
1232
- Object {
1233
- "debug": "",
1234
- "err": "",
1235
- "out": "✨ Created wrangler.toml
1236
- ✨ Created src/index.ts
1237
-
1238
- To start developing your Worker, run \`npx wrangler dev\`
1239
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1240
- "warn": "",
1241
- }
1242
- `);
1243
- });
1244
- });
1245
-
1246
- describe("javascript", () => {
1247
- it("should add missing scripts for a non-ts project with .js extension", async () => {
1248
- mockConfirm(
1249
- {
1250
- text: "Would you like to use git to manage this Worker?",
1251
- result: false,
1252
- },
1253
- {
1254
- text: "No package.json found. Would you like to create one?",
1255
- result: true,
1256
- },
1257
- {
1258
- text: "Would you like to install wrangler into package.json?",
1259
- result: false,
1260
- },
1261
- {
1262
- text: "Would you like to use TypeScript?",
1263
- result: false,
1264
- },
1265
- {
1266
- text: "Would you like to create a Worker at src/index.js?",
1267
- result: true,
1268
- }
1269
- );
1270
- await runWrangler("init");
1271
-
1272
- expect(fs.existsSync("./package.json")).toBe(true);
1273
- const packageJson = JSON.parse(
1274
- fs.readFileSync("./package.json", "utf-8")
1275
- );
1276
-
1277
- expect(fs.existsSync("./src/index.js")).toBe(true);
1278
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1279
-
1280
- expect(packageJson.scripts.start).toBe("wrangler dev");
1281
- expect(packageJson.scripts.publish).toBe("wrangler publish");
1282
- expect(packageJson.name).toContain("wrangler-tests");
1283
- expect(packageJson.version).toEqual("0.0.0");
1284
- expect(std.out).toMatchInlineSnapshot(`
1285
- "✨ Created wrangler.toml
1286
- ✨ Created package.json
1287
- ✨ Created src/index.js
1288
-
1289
- To start developing your Worker, run \`npm start\`
1290
- To publish your Worker to the Internet, run \`npm run publish\`"
1291
- `);
1292
- });
1293
-
1294
- it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
1295
- mockConfirm(
1296
- {
1297
- text: "Would you like to use git to manage this Worker?",
1298
- result: false,
1299
- },
1300
- {
1301
- text: "Would you like to install wrangler into package.json?",
1302
- result: false,
1303
- },
1304
- {
1305
- text: "Would you like to use TypeScript?",
1306
- result: false,
1307
- },
1308
- {
1309
- text: "Would you like to create a Worker at src/index.js?",
1310
- result: true,
1311
- }
1312
- );
1313
- await fsp.writeFile(
1314
- "./package.json",
1315
- JSON.stringify({
1316
- scripts: {
1317
- start: "test-start",
1318
- publish: "test-publish",
1319
- },
1320
- })
1321
- );
1322
- const packageJson = JSON.parse(
1323
- fs.readFileSync("./package.json", "utf-8")
1324
- );
1325
- await runWrangler("init");
1326
-
1327
- expect(fs.existsSync("./src/index.js")).toBe(true);
1328
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1329
-
1330
- expect(packageJson.scripts.start).toBe("test-start");
1331
- expect(packageJson.scripts.publish).toBe("test-publish");
1332
- expect(std.out).toMatchInlineSnapshot(`
1333
- "✨ Created wrangler.toml
1334
- ✨ Created src/index.js
1335
-
1336
- To start developing your Worker, run \`npx wrangler dev\`
1337
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
1338
- `);
1339
- });
1340
-
1341
- it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
1342
- mockConfirm(
1343
- {
1344
- text: "Would you like to use git to manage this Worker?",
1345
- result: false,
1346
- },
1347
- {
1348
- text: "Would you like to install wrangler into package.json?",
1349
- result: false,
1350
- },
1351
- {
1352
- text: "Would you like to use TypeScript?",
1353
- result: false,
1354
- }
1355
- );
1356
-
1357
- fs.writeFileSync(
1358
- "./package.json",
1359
- JSON.stringify({ name: "test", version: "1.0.0" }),
1360
- "utf-8"
1361
- );
1362
- fs.mkdirSync("./src", { recursive: true });
1363
- const PLACEHOLDER = "/* placeholder text */";
1364
- fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
1365
-
1366
- await runWrangler("init");
1367
- expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
1368
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1369
- expect(std).toMatchInlineSnapshot(`
1370
- Object {
1371
- "debug": "",
1372
- "err": "",
1373
- "out": "✨ Created wrangler.toml",
1374
- "warn": "",
1375
- }
1376
- `);
1377
- });
1378
-
1379
- it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1380
- mockConfirm(
1381
- {
1382
- text: "Would you like to use git to manage this Worker?",
1383
- result: false,
1384
- },
1385
- {
1386
- text: "Would you like to install wrangler into package.json?",
1387
- result: false,
1388
- },
1389
- {
1390
- text: "Would you like to use TypeScript?",
1391
- result: false,
1392
- }
1393
- );
1394
-
1395
- fs.writeFileSync(
1396
- "./package.json",
1397
- JSON.stringify({ name: "test", version: "1.0.0" }),
1398
- "utf-8"
1399
- );
1400
- fs.mkdirSync("./my-worker/src", { recursive: true });
1401
- const PLACEHOLDER = "/* placeholder text */";
1402
- fs.writeFileSync("./my-worker/src/index.js", PLACEHOLDER, "utf-8");
1403
-
1404
- await runWrangler("init my-worker");
1405
- expect(fs.readFileSync("./my-worker/src/index.js", "utf-8")).toBe(
1406
- PLACEHOLDER
1407
- );
1408
- expect(fs.existsSync("./my-worker/src/index.ts")).toBe(false);
1409
- expect(std).toMatchInlineSnapshot(`
1410
- Object {
1411
- "debug": "",
1412
- "err": "",
1413
- "out": "✨ Created my-worker/wrangler.toml",
1414
- "warn": "",
1415
- }
1416
- `);
1417
- });
1418
- });
1419
-
1420
- describe("worker names", () => {
1421
- it("should create a worker with a given name", async () => {
1422
- await runWrangler("init my-worker -y");
1423
-
1424
- const parsed = TOML.parse(
1425
- await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
1426
- );
1427
-
1428
- expect(typeof parsed.compatibility_date).toBe("string");
1429
- expect(parsed.name).toBe("my-worker");
1430
- });
1431
-
1432
- it('should create a worker with the name of the current directory if "name" is .', async () => {
1433
- await runWrangler("init . -y");
1434
-
1435
- const parsed = TOML.parse(await fsp.readFile("wrangler.toml", "utf-8"));
1436
-
1437
- expect(typeof parsed.compatibility_date).toBe("string");
1438
- expect(parsed.name).toBe(path.basename(process.cwd()).toLowerCase());
1439
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1440
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1441
- expect(std).toMatchInlineSnapshot(`
1442
- Object {
1443
- "debug": "",
1444
- "err": "",
1445
- "out": "✨ Created wrangler.toml
1446
- ✨ Initialized git repository
1447
- ✨ Created package.json
1448
- ✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies
1449
- ✨ Created src/index.ts
1450
-
1451
- To start developing your Worker, run \`npm start\`
1452
- To publish your Worker to the Internet, run \`npm run publish\`",
1453
- "warn": "",
1454
- }
1455
- `);
1456
- });
1457
-
1458
- it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
1459
- await runWrangler("init path/to/worker -y");
1460
-
1461
- const parsed = TOML.parse(
1462
- await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
1463
- );
1464
-
1465
- expect(typeof parsed.compatibility_date).toBe("string");
1466
- expect(parsed.name).toBe("worker");
1467
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1468
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1469
- expect(std).toMatchInlineSnapshot(`
1470
- Object {
1471
- "debug": "",
1472
- "err": "",
1473
- "out": "✨ Created path/to/worker/wrangler.toml
1474
- ✨ Initialized git repository at path/to/worker
1475
- ✨ Created path/to/worker/package.json
1476
- ✨ Created path/to/worker/tsconfig.json, installed @cloudflare/workers-types into devDependencies
1477
- ✨ Created path/to/worker/src/index.ts
1478
-
1479
- To start developing your Worker, run \`cd path/to/worker && npm start\`
1480
- To publish your Worker to the Internet, run \`npm run publish\`",
1481
- "warn": "",
1482
- }
1483
- `);
1484
- });
1485
-
1486
- it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
1487
- await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
1488
- const parsed = TOML.parse(
1489
- await fsp.readFile(
1490
- "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml",
1491
- "utf-8"
1492
- )
1493
- );
1494
-
1495
- expect(typeof parsed.compatibility_date).toBe("string");
1496
- expect(parsed.name).toBe("weird_w0rkr_n4m3-js-tsx-tar-gz");
1497
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1498
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1499
- expect(std).toMatchInlineSnapshot(`
1500
- Object {
1501
- "debug": "",
1502
- "err": "",
1503
- "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1504
- ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1505
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1506
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json, installed @cloudflare/workers-types into devDependencies
1507
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1508
-
1509
- To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1510
- To publish your Worker to the Internet, run \`npm run publish\`",
1511
- "warn": "",
1512
- }
1513
- `);
1514
- });
1515
- });
187
+ });
188
+ });
189
+
190
+ describe("wrangler.toml", () => {
191
+ it("should create a wrangler.toml", async () => {
192
+ mockConfirm(
193
+ {
194
+ text: "Would you like to use git to manage this Worker?",
195
+ result: false,
196
+ },
197
+ {
198
+ text: "No package.json found. Would you like to create one?",
199
+ result: false,
200
+ }
201
+ );
202
+ await runWrangler("init");
203
+ checkFiles({
204
+ items: {
205
+ "wrangler.toml": wranglerToml({
206
+ compatibility_date: expect.any(String),
207
+ name: expect.stringContaining("wrangler-tests"),
208
+ }),
209
+ "package.json": false,
210
+ "tsconfig.json": false,
211
+ },
212
+ });
213
+
214
+ expect(std).toMatchInlineSnapshot(`
215
+ Object {
216
+ "debug": "",
217
+ "err": "",
218
+ "info": "",
219
+ "out": "✨ Created wrangler.toml",
220
+ "warn": "",
221
+ }
222
+ `);
223
+ });
224
+
225
+ it("should create a wrangler.toml and a directory for a named Worker ", async () => {
226
+ mockConfirm(
227
+ {
228
+ text: "Would you like to use git to manage this Worker?",
229
+ result: false,
230
+ },
231
+ {
232
+ text: "No package.json found. Would you like to create one?",
233
+ result: false,
234
+ }
235
+ );
236
+ await runWrangler("init my-worker");
237
+
238
+ checkFiles({
239
+ items: {
240
+ "my-worker/wrangler.toml": wranglerToml({
241
+ compatibility_date: expect.any(String),
242
+ name: "my-worker",
243
+ }),
244
+ "my-worker/package.json": false,
245
+ "my-worker/tsconfig.json": false,
246
+ },
247
+ });
248
+
249
+ expect(std).toMatchInlineSnapshot(`
250
+ Object {
251
+ "debug": "",
252
+ "err": "",
253
+ "info": "",
254
+ "out": "✨ Created my-worker/wrangler.toml",
255
+ "warn": "",
256
+ }
257
+ `);
258
+ });
259
+
260
+ it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
261
+ writeFiles({
262
+ items: {
263
+ "wrangler.toml": wranglerToml({
264
+ // use a fake value to make sure the file is not overwritten
265
+ compatibility_date: "something-else",
266
+ }),
267
+ },
268
+ });
269
+
270
+ mockConfirm({
271
+ text: "Do you want to continue initializing this project?",
272
+ result: false,
273
+ });
274
+
275
+ await runWrangler("init");
276
+ expect(std.warn).toContain("wrangler.toml already exists!");
277
+
278
+ checkFiles({
279
+ items: {
280
+ "wrangler.toml": wranglerToml({
281
+ compatibility_date: "something-else",
282
+ }),
283
+ "package.json": false,
284
+ "tsconfig.json": false,
285
+ },
286
+ });
287
+
288
+ expect(std).toMatchInlineSnapshot(`
289
+ Object {
290
+ "debug": "",
291
+ "err": "",
292
+ "info": "",
293
+ "out": "",
294
+ "warn": "▲ [WARNING] wrangler.toml already exists!
295
+
296
+ ",
297
+ }
298
+ `);
299
+ });
300
+
301
+ it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
302
+ writeFiles({
303
+ items: {
304
+ "path/to/worker/wrangler.toml": wranglerToml({
305
+ compatibility_date: "something-else",
306
+ }),
307
+ },
308
+ });
309
+ mockConfirm({
310
+ text: "Do you want to continue initializing this project?",
311
+ result: false,
312
+ });
313
+
314
+ await runWrangler("init path/to/worker");
315
+
316
+ expect(std.warn).toContain("wrangler.toml already exists!");
317
+ checkFiles({
318
+ items: {
319
+ "path/to/worker/wrangler.toml": wranglerToml({
320
+ compatibility_date: "something-else",
321
+ }),
322
+ "package.json": false,
323
+ "tsconfig.json": false,
324
+ },
325
+ });
326
+ expect(std).toMatchInlineSnapshot(`
327
+ Object {
328
+ "debug": "",
329
+ "err": "",
330
+ "info": "",
331
+ "out": "",
332
+ "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
333
+
334
+ ",
335
+ }
336
+ `);
337
+ });
338
+
339
+ it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
340
+ writeFiles({
341
+ items: {
342
+ "wrangler.toml": wranglerToml({
343
+ compatibility_date: "something-else",
344
+ }),
345
+ },
346
+ });
347
+ mockConfirm(
348
+ {
349
+ text: "Do you want to continue initializing this project?",
350
+ result: true,
351
+ },
352
+ {
353
+ text: "Would you like to use git to manage this Worker?",
354
+ result: true,
355
+ },
356
+ {
357
+ text: "No package.json found. Would you like to create one?",
358
+ result: true,
359
+ },
360
+ {
361
+ text: "Would you like to use TypeScript?",
362
+ result: true,
363
+ }
364
+ );
365
+
366
+ mockSelect({
367
+ text: `Would you like to create a Worker at ${path.join(
368
+ "src",
369
+ "index.ts"
370
+ )}?`,
371
+ result: "fetch",
372
+ });
373
+ mockConfirm({
374
+ text: "Would you like us to write your first test with Vitest?",
375
+ result: true,
376
+ });
377
+
378
+ await runWrangler("init");
379
+
380
+ checkFiles({
381
+ items: {
382
+ "wrangler.toml": wranglerToml({
383
+ compatibility_date: "something-else",
384
+ }),
385
+ },
386
+ });
387
+ });
388
+
389
+ it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
390
+ writeFiles({
391
+ items: {
392
+ "wrangler.toml": wranglerToml({
393
+ compatibility_date: "something-else",
394
+ }),
395
+ },
396
+ });
397
+ mockConfirm(
398
+ {
399
+ text: "Do you want to continue initializing this project?",
400
+ result: true,
401
+ },
402
+ {
403
+ text: "Would you like to use git to manage this Worker?",
404
+ result: false,
405
+ },
406
+ {
407
+ text: "No package.json found. Would you like to create one?",
408
+ result: false,
409
+ }
410
+ );
411
+
412
+ await runWrangler("init");
413
+
414
+ expect(std.warn).toContain("wrangler.toml already exists!");
415
+ checkFiles({
416
+ items: {
417
+ "wrangler.toml": wranglerToml({
418
+ compatibility_date: "something-else",
419
+ }),
420
+ },
421
+ });
422
+ expect(std).toMatchInlineSnapshot(`
423
+ Object {
424
+ "debug": "",
425
+ "err": "",
426
+ "info": "",
427
+ "out": "",
428
+ "warn": "▲ [WARNING] wrangler.toml already exists!
429
+
430
+ ",
431
+ }
432
+ `);
433
+ });
434
+
435
+ it("should not add a Cron Trigger to wrangler.toml when creating a Scheduled Worker if wrangler.toml already exists", async () => {
436
+ writeFiles({
437
+ items: {
438
+ "wrangler.toml": wranglerToml({
439
+ compatibility_date: "something-else",
440
+ }),
441
+ },
442
+ });
443
+ mockConfirm(
444
+ {
445
+ text: "Do you want to continue initializing this project?",
446
+ result: true,
447
+ },
448
+ {
449
+ text: "Would you like to use git to manage this Worker?",
450
+ result: true,
451
+ },
452
+
453
+ {
454
+ text: "No package.json found. Would you like to create one?",
455
+ result: true,
456
+ },
457
+ {
458
+ text: "Would you like to use TypeScript?",
459
+ result: true,
460
+ }
461
+ );
462
+ mockSelect({
463
+ text: `Would you like to create a Worker at ${path.join(
464
+ "src",
465
+ "index.ts"
466
+ )}?`,
467
+ result: "scheduled",
468
+ });
469
+
470
+ mockConfirm({
471
+ text: "Would you like us to write your first test with Vitest?",
472
+ result: true,
473
+ });
474
+ await runWrangler("init");
475
+
476
+ checkFiles({
477
+ items: {
478
+ "wrangler.toml": wranglerToml({
479
+ compatibility_date: "something-else",
480
+ }),
481
+ },
482
+ });
483
+ });
484
+
485
+ 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 () => {
486
+ mockConfirm(
487
+ {
488
+ text: "Would you like to use git to manage this Worker?",
489
+ result: true,
490
+ },
491
+ {
492
+ text: "No package.json found. Would you like to create one?",
493
+ result: true,
494
+ },
495
+ {
496
+ text: "Would you like to use TypeScript?",
497
+ result: true,
498
+ }
499
+ );
500
+ mockSelect({
501
+ text: `Would you like to create a Worker at ${path.join(
502
+ "src",
503
+ "index.ts"
504
+ )}?`,
505
+ result: "scheduled",
506
+ });
507
+ mockConfirm({
508
+ text: "Would you like us to write your first test with Vitest?",
509
+ result: true,
510
+ });
511
+ await runWrangler("init");
512
+
513
+ checkFiles({
514
+ items: {
515
+ "wrangler.toml": wranglerToml({
516
+ ...MINIMAL_WRANGLER_TOML,
517
+ triggers: { crons: ["1 * * * *"] },
518
+ }),
519
+ },
520
+ });
521
+ });
522
+ });
523
+
524
+ describe("git init", () => {
525
+ it("should offer to initialize a git repository", async () => {
526
+ mockConfirm(
527
+ {
528
+ text: "Would you like to use git to manage this Worker?",
529
+ result: true,
530
+ },
531
+ {
532
+ text: "No package.json found. Would you like to create one?",
533
+ result: false,
534
+ }
535
+ );
536
+
537
+ await runWrangler("init");
538
+
539
+ checkFiles({
540
+ items: {
541
+ "wrangler.toml": wranglerToml({
542
+ compatibility_date: expect.any(String),
543
+ name: expect.stringContaining("wrangler-tests"),
544
+ }),
545
+ ".git": { items: {} },
546
+ ".gitignore": true,
547
+ },
548
+ });
549
+ expect(std).toMatchInlineSnapshot(`
550
+ Object {
551
+ "debug": "",
552
+ "err": "",
553
+ "info": "",
554
+ "out": "✨ Created wrangler.toml
555
+ ✨ Initialized git repository",
556
+ "warn": "",
557
+ }
558
+ `);
559
+ expect((await execa("git", ["branch", "--show-current"])).stdout).toEqual(
560
+ getDefaultBranchName()
561
+ );
562
+ });
563
+
564
+ it("should not offer to initialize a git repo if it's already inside one", async () => {
565
+ await execa("git", ["init"]);
566
+ setWorkingDirectory("some-folder");
567
+
568
+ await runWrangler("init -y");
569
+
570
+ checkFiles({
571
+ items: {
572
+ "wrangler.toml": wranglerToml({
573
+ ...MINIMAL_WRANGLER_TOML,
574
+ name: "some-folder",
575
+ }),
576
+ ".git": { items: {} },
577
+ ".gitignore": false,
578
+ },
579
+ });
580
+
581
+ // Note the lack of "✨ Initialized git repository" in the log
582
+ expect(std).toMatchInlineSnapshot(`
583
+ Object {
584
+ "debug": "",
585
+ "err": "",
586
+ "info": "Your project will use Vitest to run your tests.",
587
+ "out": "✨ Created wrangler.toml
588
+ ✨ Created package.json
589
+ ✨ Created tsconfig.json
590
+ ✨ Created src/index.ts
591
+ ✨ Created src/index.test.ts
592
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
593
+
594
+ To start developing your Worker, run \`npm start\`
595
+ To start testing your Worker, run \`npm test\`
596
+ To publish your Worker to the Internet, run \`npm run deploy\`",
597
+ "warn": "",
598
+ }
599
+ `);
600
+ });
601
+
602
+ it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
603
+ fs.mkdirSync("path/to/worker", { recursive: true });
604
+ await execa("git", ["init"], { cwd: "path/to/worker" });
605
+ expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
606
+
607
+ await runWrangler("init path/to/worker/my-worker -y");
608
+
609
+ // Note the lack of "✨ Initialized git repository" in the log
610
+ expect(std).toMatchInlineSnapshot(`
611
+ Object {
612
+ "debug": "",
613
+ "err": "",
614
+ "info": "Your project will use Vitest to run your tests.",
615
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
616
+ ✨ Created path/to/worker/my-worker/package.json
617
+ ✨ Created path/to/worker/my-worker/tsconfig.json
618
+ ✨ Created path/to/worker/my-worker/src/index.ts
619
+ ✨ Created path/to/worker/my-worker/src/index.test.ts
620
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
621
+
622
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
623
+ To start testing your Worker, run \`npm test\`
624
+ To publish your Worker to the Internet, run \`npm run deploy\`",
625
+ "warn": "",
626
+ }
627
+ `);
628
+ });
629
+
630
+ // I... don't know how to test this lol
631
+ it.todo(
632
+ "should not offer to initialize a git repo if git is not installed"
633
+ );
634
+
635
+ it("should initialize git repo with the user's default branch", async () => {
636
+ mockConfirm(
637
+ {
638
+ text: "Would you like to use git to manage this Worker?",
639
+ result: true,
640
+ },
641
+ {
642
+ text: "No package.json found. Would you like to create one?",
643
+ result: false,
644
+ }
645
+ );
646
+ await runWrangler("init");
647
+ expect(std).toMatchInlineSnapshot(`
648
+ Object {
649
+ "debug": "",
650
+ "err": "",
651
+ "info": "",
652
+ "out": "✨ Created wrangler.toml
653
+ ✨ Initialized git repository",
654
+ "warn": "",
655
+ }
656
+ `);
657
+
658
+ expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
659
+ `refs/heads/${getDefaultBranchName()}`
660
+ );
661
+ });
662
+ });
663
+
664
+ describe("package.json", () => {
665
+ it("should create a package.json if none is found and user confirms", async () => {
666
+ mockConfirm(
667
+ {
668
+ text: "Would you like to use git to manage this Worker?",
669
+ result: false,
670
+ },
671
+ {
672
+ text: "No package.json found. Would you like to create one?",
673
+ result: true,
674
+ },
675
+ {
676
+ text: "Would you like to use TypeScript?",
677
+ result: false,
678
+ }
679
+ );
680
+ mockSelect({
681
+ text: `Would you like to create a Worker at ${path.join(
682
+ "src",
683
+ "index.js"
684
+ )}?`,
685
+ result: "none",
686
+ });
687
+
688
+ await runWrangler("init");
689
+
690
+ checkFiles({
691
+ items: {
692
+ "package.json": {
693
+ contents: expect.objectContaining({
694
+ name: expect.stringContaining("wrangler-tests"),
695
+ version: "0.0.0",
696
+ devDependencies: {
697
+ wrangler: expect.any(String),
698
+ },
699
+ }),
700
+ },
701
+ "tsconfig.json": false,
702
+ },
703
+ });
704
+ expect(mockPackageManager.install).toHaveBeenCalled();
705
+ expect(std).toMatchInlineSnapshot(`
706
+ Object {
707
+ "debug": "",
708
+ "err": "",
709
+ "info": "",
710
+ "out": "✨ Created wrangler.toml
711
+ ✨ Created package.json",
712
+ "warn": "",
713
+ }
714
+ `);
715
+ });
716
+
717
+ it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
718
+ mockConfirm(
719
+ {
720
+ text: "Would you like to use git to manage this Worker?",
721
+ result: false,
722
+ },
723
+ {
724
+ text: "No package.json found. Would you like to create one?",
725
+ result: true,
726
+ },
727
+ {
728
+ text: "Would you like to use TypeScript?",
729
+ result: false,
730
+ }
731
+ );
732
+ mockSelect({
733
+ text: `Would you like to create a Worker at ${path.join(
734
+ "my-worker",
735
+ "src",
736
+ "index.js"
737
+ )}?`,
738
+ result: "none",
739
+ });
740
+
741
+ await runWrangler("init my-worker");
742
+
743
+ checkFiles({
744
+ items: {
745
+ "my-worker/package.json": {
746
+ contents: expect.objectContaining({
747
+ name: "my-worker",
748
+ version: "0.0.0",
749
+ devDependencies: {
750
+ wrangler: expect.any(String),
751
+ },
752
+ }),
753
+ },
754
+ "my-worker/tsconfig.json": false,
755
+ },
756
+ });
757
+ expect(std).toMatchInlineSnapshot(`
758
+ Object {
759
+ "debug": "",
760
+ "err": "",
761
+ "info": "",
762
+ "out": "✨ Created my-worker/wrangler.toml
763
+ ✨ Created my-worker/package.json",
764
+ "warn": "",
765
+ }
766
+ `);
767
+ });
768
+
769
+ it("should not touch an existing package.json in the same directory", async () => {
770
+ mockConfirm(
771
+ {
772
+ text: "Would you like to use git to manage this Worker?",
773
+ result: false,
774
+ },
775
+ {
776
+ text: "Would you like to install wrangler into package.json?",
777
+ result: false,
778
+ },
779
+ {
780
+ text: "Would you like to use TypeScript?",
781
+ result: false,
782
+ }
783
+ );
784
+ mockSelect({
785
+ text: `Would you like to create a Worker at ${path.join(
786
+ "src",
787
+ "index.js"
788
+ )}?`,
789
+ result: "none",
790
+ });
791
+ writeFiles({
792
+ items: {
793
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
794
+ },
795
+ });
796
+
797
+ await runWrangler("init");
798
+
799
+ checkFiles({
800
+ items: {
801
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
802
+ },
803
+ });
804
+ expect(std).toMatchInlineSnapshot(`
805
+ Object {
806
+ "debug": "",
807
+ "err": "",
808
+ "info": "",
809
+ "out": "✨ Created wrangler.toml",
810
+ "warn": "",
811
+ }
812
+ `);
813
+ });
814
+
815
+ it("should not touch an existing package.json in an ancestor directory, when a name is passed", async () => {
816
+ mockConfirm(
817
+ {
818
+ text: "Would you like to use git to manage this Worker?",
819
+ result: false,
820
+ },
821
+ {
822
+ text: "No package.json found. Would you like to create one?",
823
+ result: true,
824
+ },
825
+ {
826
+ text: "Would you like to use TypeScript?",
827
+ result: false,
828
+ }
829
+ );
830
+ mockSelect({
831
+ text: `Would you like to create a Worker at ${path.join(
832
+ "path",
833
+ "to",
834
+ "worker",
835
+ "my-worker",
836
+ "src",
837
+ "index.js"
838
+ )}?`,
839
+ result: "none",
840
+ });
841
+ writeFiles({
842
+ items: {
843
+ "path/to/worker/package.json": {
844
+ contents: { name: "test", version: "1.0.0" },
845
+ },
846
+ },
847
+ });
848
+
849
+ await runWrangler("init path/to/worker/my-worker");
850
+
851
+ checkFiles({
852
+ items: {
853
+ "path/to/worker/package.json": {
854
+ contents: { name: "test", version: "1.0.0" },
855
+ },
856
+ },
857
+ });
858
+ expect(std).toMatchInlineSnapshot(`
859
+ Object {
860
+ "debug": "",
861
+ "err": "",
862
+ "info": "",
863
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
864
+ ✨ Created path/to/worker/my-worker/package.json",
865
+ "warn": "",
866
+ }
867
+ `);
868
+ });
869
+
870
+ it("should offer to install wrangler into an existing package.json", async () => {
871
+ mockConfirm(
872
+ {
873
+ text: "Would you like to use git to manage this Worker?",
874
+ result: false,
875
+ },
876
+ {
877
+ text: "Would you like to install wrangler into package.json?",
878
+ result: true,
879
+ },
880
+ {
881
+ text: "Would you like to use TypeScript?",
882
+ result: false,
883
+ }
884
+ );
885
+ mockSelect({
886
+ text: `Would you like to create a Worker at ${path.join(
887
+ "src",
888
+ "index.js"
889
+ )}?`,
890
+ result: "none",
891
+ });
892
+ writeFiles({
893
+ items: {
894
+ "package.json": {
895
+ contents: { name: "test", version: "1.0.0" },
896
+ },
897
+ },
898
+ });
899
+
900
+ await runWrangler("init");
901
+
902
+ checkFiles({
903
+ items: {
904
+ "package.json": {
905
+ contents: { name: "test", version: "1.0.0" },
906
+ },
907
+ },
908
+ });
909
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
910
+ `wrangler@${wranglerVersion}`
911
+ );
912
+ expect(std).toMatchInlineSnapshot(`
913
+ Object {
914
+ "debug": "",
915
+ "err": "",
916
+ "info": "",
917
+ "out": "✨ Created wrangler.toml
918
+ ✨ Installed wrangler into devDependencies",
919
+ "warn": "",
920
+ }
921
+ `);
922
+ });
923
+
924
+ it("should offer to install wrangler into a package.json relative to the target directory, if no name is provided", async () => {
925
+ mockConfirm(
926
+ {
927
+ text: "Would you like to use git to manage this Worker?",
928
+ result: false,
929
+ },
930
+ {
931
+ text: `Would you like to install wrangler into ${path.join(
932
+ "..",
933
+ "package.json"
934
+ )}?`,
935
+ result: true,
936
+ },
937
+ {
938
+ text: "Would you like to use TypeScript?",
939
+ result: false,
940
+ }
941
+ );
942
+ mockSelect({
943
+ text: `Would you like to create a Worker at ${path.join(
944
+ "src",
945
+ "index.js"
946
+ )}?`,
947
+ result: "none",
948
+ });
949
+ writeFiles({
950
+ items: {
951
+ "path/to/worker/package.json": {
952
+ contents: { name: "test", version: "1.0.0" },
953
+ },
954
+ },
955
+ });
956
+ setWorkingDirectory("path/to/worker/my-worker");
957
+
958
+ await runWrangler("init");
959
+
960
+ setWorkingDirectory("../../../..");
961
+ checkFiles({
962
+ items: {
963
+ "path/to/worker/package.json": {
964
+ contents: { name: "test", version: "1.0.0" },
965
+ },
966
+ "path/to/worker/my-worker/package.json": false,
967
+ },
968
+ });
969
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
970
+ `wrangler@${wranglerVersion}`
971
+ );
972
+ expect(mockPackageManager.cwd).toBe(process.cwd());
973
+ expect(std).toMatchInlineSnapshot(`
974
+ Object {
975
+ "debug": "",
976
+ "err": "",
977
+ "info": "",
978
+ "out": "✨ Created wrangler.toml
979
+ ✨ Installed wrangler into devDependencies",
980
+ "warn": "",
981
+ }
982
+ `);
983
+ });
984
+
985
+ it("should not touch an existing package.json in an ancestor directory", async () => {
986
+ mockConfirm(
987
+ {
988
+ text: "Would you like to use git to manage this Worker?",
989
+ result: false,
990
+ },
991
+ {
992
+ text: `Would you like to install wrangler into ${path.join(
993
+ "..",
994
+ "..",
995
+ "package.json"
996
+ )}?`,
997
+ result: false,
998
+ },
999
+ {
1000
+ text: "Would you like to use TypeScript?",
1001
+ result: false,
1002
+ }
1003
+ );
1004
+ mockSelect({
1005
+ text: `Would you like to create a Worker at ${path.join(
1006
+ "src",
1007
+ "index.js"
1008
+ )}?`,
1009
+ result: "none",
1010
+ });
1011
+ writeFiles({
1012
+ items: {
1013
+ "package.json": {
1014
+ contents: { name: "test", version: "1.0.0" },
1015
+ },
1016
+ },
1017
+ });
1018
+ setWorkingDirectory("./sub-1/sub-2");
1019
+
1020
+ await runWrangler("init");
1021
+
1022
+ checkFiles({
1023
+ items: {
1024
+ "package.json": false,
1025
+ "../../package.json": {
1026
+ contents: { name: "test", version: "1.0.0" },
1027
+ },
1028
+ },
1029
+ });
1030
+ expect(std).toMatchInlineSnapshot(`
1031
+ Object {
1032
+ "debug": "",
1033
+ "err": "",
1034
+ "info": "",
1035
+ "out": "✨ Created wrangler.toml",
1036
+ "warn": "",
1037
+ }
1038
+ `);
1039
+ });
1040
+ });
1041
+
1042
+ describe("typescript", () => {
1043
+ it("should offer to create a worker in a non-typescript project", async () => {
1044
+ mockConfirm(
1045
+ {
1046
+ text: "Would you like to use git to manage this Worker?",
1047
+ result: false,
1048
+ },
1049
+ {
1050
+ text: "Would you like to install wrangler into package.json?",
1051
+ result: false,
1052
+ },
1053
+ {
1054
+ text: "Would you like to use TypeScript?",
1055
+ result: false,
1056
+ }
1057
+ );
1058
+ mockSelect({
1059
+ text: `Would you like to create a Worker at ${path.join(
1060
+ "src",
1061
+ "index.js"
1062
+ )}?`,
1063
+ result: "fetch",
1064
+ });
1065
+ mockConfirm({
1066
+ text: "Would you like us to write your first test?",
1067
+ result: false,
1068
+ });
1069
+ writeFiles({
1070
+ items: {
1071
+ "package.json": {
1072
+ contents: { name: "test", version: "1.0.0" },
1073
+ },
1074
+ },
1075
+ });
1076
+
1077
+ await runWrangler("init");
1078
+
1079
+ checkFiles({
1080
+ items: {
1081
+ "wrangler.toml": wranglerToml({
1082
+ ...MINIMAL_WRANGLER_TOML,
1083
+ main: "src/index.js",
1084
+ }),
1085
+ "src/index.js": true,
1086
+ "src/index.ts": false,
1087
+ },
1088
+ });
1089
+ expect(std).toMatchInlineSnapshot(`
1090
+ Object {
1091
+ "debug": "",
1092
+ "err": "",
1093
+ "info": "",
1094
+ "out": "✨ Created wrangler.toml
1095
+ ✨ Created src/index.js
1096
+
1097
+ To start developing your Worker, run \`npx wrangler dev\`
1098
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1099
+ "warn": "",
1100
+ }
1101
+ `);
1102
+ });
1103
+
1104
+ it("should offer to create a worker in a typescript project", async () => {
1105
+ mockConfirm(
1106
+ {
1107
+ text: "Would you like to use git to manage this Worker?",
1108
+ result: false,
1109
+ },
1110
+ {
1111
+ text: "Would you like to install wrangler into package.json?",
1112
+ result: false,
1113
+ },
1114
+ {
1115
+ text: "Would you like to use TypeScript?",
1116
+ result: true,
1117
+ }
1118
+ );
1119
+ mockSelect({
1120
+ text: `Would you like to create a Worker at ${path.join(
1121
+ "src",
1122
+ "index.ts"
1123
+ )}?`,
1124
+ result: "fetch",
1125
+ });
1126
+ mockConfirm({
1127
+ text: "Would you like us to write your first test with Vitest?",
1128
+ result: true,
1129
+ });
1130
+ writeFiles({
1131
+ items: {
1132
+ "package.json": {
1133
+ contents: { name: "test", version: "1.0.0" },
1134
+ },
1135
+ },
1136
+ });
1137
+
1138
+ await runWrangler("init");
1139
+
1140
+ checkFiles({
1141
+ items: {
1142
+ "src/index.js": false,
1143
+ "src/index.ts": true,
1144
+ },
1145
+ });
1146
+ expect(std).toMatchInlineSnapshot(`
1147
+ Object {
1148
+ "debug": "",
1149
+ "err": "",
1150
+ "info": "",
1151
+ "out": "✨ Created wrangler.toml
1152
+ ✨ Created tsconfig.json
1153
+ ✨ Created src/index.ts
1154
+ ✨ Created src/index.test.ts
1155
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1156
+
1157
+ To start developing your Worker, run \`npx wrangler dev\`
1158
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1159
+ "warn": "",
1160
+ }
1161
+ `);
1162
+ });
1163
+
1164
+ it("should add scripts for a typescript project with .ts extension", async () => {
1165
+ mockConfirm(
1166
+ {
1167
+ text: "Would you like to use git to manage this Worker?",
1168
+ result: false,
1169
+ },
1170
+ {
1171
+ text: "No package.json found. Would you like to create one?",
1172
+ result: true,
1173
+ },
1174
+
1175
+ {
1176
+ text: "Would you like to use TypeScript?",
1177
+ result: true,
1178
+ }
1179
+ );
1180
+ mockSelect({
1181
+ text: `Would you like to create a Worker at ${path.join(
1182
+ "src",
1183
+ "index.ts"
1184
+ )}?`,
1185
+ result: "fetch",
1186
+ });
1187
+ mockConfirm({
1188
+ text: "Would you like us to write your first test with Vitest?",
1189
+ result: true,
1190
+ });
1191
+
1192
+ await runWrangler("init");
1193
+
1194
+ checkFiles({
1195
+ items: {
1196
+ "package.json": {
1197
+ contents: expect.objectContaining({
1198
+ name: expect.stringContaining("wrangler-tests"),
1199
+ version: "0.0.0",
1200
+ scripts: {
1201
+ deploy: "wrangler publish",
1202
+ start: "wrangler dev",
1203
+ test: "vitest",
1204
+ },
1205
+ devDependencies: {
1206
+ wrangler: expect.any(String),
1207
+ },
1208
+ }),
1209
+ },
1210
+ "src/index.js": false,
1211
+ "src/index.ts": true,
1212
+ },
1213
+ });
1214
+ expect(std.out).toMatchInlineSnapshot(`
1215
+ "✨ Created wrangler.toml
1216
+ ✨ Created package.json
1217
+ ✨ Created tsconfig.json
1218
+ ✨ Created src/index.ts
1219
+ ✨ Created src/index.test.ts
1220
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1221
+
1222
+ To start developing your Worker, run \`npm start\`
1223
+ To start testing your Worker, run \`npm test\`
1224
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1225
+ `);
1226
+ });
1227
+
1228
+ it("should not overwrite package.json scripts for a typescript project", async () => {
1229
+ mockConfirm(
1230
+ {
1231
+ text: "Would you like to use git to manage this Worker?",
1232
+ result: false,
1233
+ },
1234
+ {
1235
+ text: "Would you like to install wrangler into package.json?",
1236
+ result: false,
1237
+ },
1238
+ {
1239
+ text: "Would you like to use TypeScript?",
1240
+ result: true,
1241
+ }
1242
+ );
1243
+ mockSelect({
1244
+ text: `Would you like to create a Worker at ${path.join(
1245
+ "src",
1246
+ "index.ts"
1247
+ )}?`,
1248
+ result: "fetch",
1249
+ });
1250
+ mockConfirm({
1251
+ text: "Would you like us to write your first test with Vitest?",
1252
+ result: true,
1253
+ });
1254
+ writeFiles({
1255
+ items: {
1256
+ "package.json": {
1257
+ contents: {
1258
+ scripts: {
1259
+ start: "test-start",
1260
+ deploy: "test-publish",
1261
+ },
1262
+ },
1263
+ },
1264
+ },
1265
+ });
1266
+
1267
+ await runWrangler("init");
1268
+
1269
+ checkFiles({
1270
+ items: {
1271
+ "package.json": {
1272
+ contents: {
1273
+ scripts: {
1274
+ start: "test-start",
1275
+ deploy: "test-publish",
1276
+ },
1277
+ },
1278
+ },
1279
+ "src/index.js": false,
1280
+ "src/index.ts": true,
1281
+ },
1282
+ });
1283
+ expect(std.out).toMatchInlineSnapshot(`
1284
+ "✨ Created wrangler.toml
1285
+ ✨ Created tsconfig.json
1286
+ ✨ Created src/index.ts
1287
+ ✨ Created src/index.test.ts
1288
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1289
+
1290
+ To start developing your Worker, run \`npx wrangler dev\`
1291
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1292
+ `);
1293
+ });
1294
+
1295
+ it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
1296
+ mockConfirm(
1297
+ {
1298
+ text: "Would you like to use git to manage this Worker?",
1299
+ result: false,
1300
+ },
1301
+ {
1302
+ text: "Would you like to install wrangler into package.json?",
1303
+ result: false,
1304
+ },
1305
+ {
1306
+ text: "Would you like to use TypeScript?",
1307
+ result: true,
1308
+ }
1309
+ );
1310
+ const PLACEHOLDER = "/* placeholder text */";
1311
+ writeFiles({
1312
+ items: {
1313
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1314
+ "src/index.ts": { contents: PLACEHOLDER },
1315
+ },
1316
+ });
1317
+
1318
+ await runWrangler("init");
1319
+
1320
+ checkFiles({
1321
+ items: {
1322
+ "src/index.js": false,
1323
+ "src/index.ts": { contents: PLACEHOLDER },
1324
+ },
1325
+ });
1326
+ expect(std).toMatchInlineSnapshot(`
1327
+ Object {
1328
+ "debug": "",
1329
+ "err": "",
1330
+ "info": "",
1331
+ "out": "✨ Created wrangler.toml
1332
+ ✨ Created tsconfig.json
1333
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1334
+ "warn": "",
1335
+ }
1336
+ `);
1337
+ });
1338
+
1339
+ 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 () => {
1340
+ mockConfirm(
1341
+ {
1342
+ text: "Would you like to use git to manage this Worker?",
1343
+ result: false,
1344
+ },
1345
+ {
1346
+ text: "No package.json found. Would you like to create one?",
1347
+ result: true,
1348
+ },
1349
+
1350
+ {
1351
+ text: "Would you like to use TypeScript?",
1352
+ result: true,
1353
+ }
1354
+ );
1355
+ const PLACEHOLDER = "/* placeholder text */";
1356
+ writeFiles({
1357
+ items: {
1358
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1359
+ "my-worker/src/index.ts": { contents: PLACEHOLDER },
1360
+ },
1361
+ });
1362
+
1363
+ await runWrangler("init my-worker");
1364
+
1365
+ checkFiles({
1366
+ items: {
1367
+ "my-worker/src/index.js": false,
1368
+ "my-worker/src/index.ts": { contents: PLACEHOLDER },
1369
+ },
1370
+ });
1371
+ expect(std).toMatchInlineSnapshot(`
1372
+ Object {
1373
+ "debug": "",
1374
+ "err": "",
1375
+ "info": "",
1376
+ "out": "✨ Created my-worker/wrangler.toml
1377
+ ✨ Created my-worker/package.json
1378
+ ✨ Created my-worker/tsconfig.json
1379
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1380
+ "warn": "",
1381
+ }
1382
+ `);
1383
+ });
1384
+
1385
+ it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
1386
+ mockConfirm(
1387
+ {
1388
+ text: "Would you like to use git to manage this Worker?",
1389
+ result: false,
1390
+ },
1391
+ {
1392
+ text: "No package.json found. Would you like to create one?",
1393
+ result: true,
1394
+ },
1395
+ {
1396
+ text: "Would you like to use TypeScript?",
1397
+ result: true,
1398
+ }
1399
+ );
1400
+ mockSelect({
1401
+ text: `Would you like to create a Worker at ${path.join(
1402
+ "src",
1403
+ "index.ts"
1404
+ )}?`,
1405
+ result: "none",
1406
+ });
1407
+
1408
+ await runWrangler("init");
1409
+
1410
+ checkFiles({
1411
+ items: {
1412
+ "tsconfig.json": {
1413
+ contents: {
1414
+ config: {
1415
+ compilerOptions: expect.objectContaining({
1416
+ types: ["@cloudflare/workers-types"],
1417
+ }),
1418
+ },
1419
+ error: undefined,
1420
+ },
1421
+ },
1422
+ },
1423
+ });
1424
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1425
+ "@cloudflare/workers-types",
1426
+ "typescript"
1427
+ );
1428
+ expect(std).toMatchInlineSnapshot(`
1429
+ Object {
1430
+ "debug": "",
1431
+ "err": "",
1432
+ "info": "",
1433
+ "out": "✨ Created wrangler.toml
1434
+ ✨ Created package.json
1435
+ ✨ Created tsconfig.json
1436
+ ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1437
+ "warn": "",
1438
+ }
1439
+ `);
1440
+ });
1441
+
1442
+ it("should not touch an existing tsconfig.json in the same directory", async () => {
1443
+ mockConfirm({
1444
+ text: "Would you like to use git to manage this Worker?",
1445
+ result: false,
1446
+ });
1447
+ mockSelect({
1448
+ text: `Would you like to create a Worker at ${path.join(
1449
+ "src",
1450
+ "index.ts"
1451
+ )}?`,
1452
+ result: "fetch",
1453
+ });
1454
+ mockConfirm({
1455
+ text: "Would you like us to write your first test with Vitest?",
1456
+ result: true,
1457
+ });
1458
+ writeFiles({
1459
+ items: {
1460
+ "package.json": {
1461
+ contents: {
1462
+ name: "test",
1463
+ version: "1.0.0",
1464
+ devDependencies: {
1465
+ wrangler: "0.0.0",
1466
+ "@cloudflare/workers-types": "0.0.0",
1467
+ },
1468
+ },
1469
+ },
1470
+ "tsconfig.json": { contents: { compilerOptions: {} } },
1471
+ },
1472
+ });
1473
+
1474
+ await runWrangler("init");
1475
+
1476
+ checkFiles({
1477
+ items: {
1478
+ "tsconfig.json": {
1479
+ contents: { config: { compilerOptions: {} }, error: undefined },
1480
+ },
1481
+ },
1482
+ });
1483
+ expect(std).toMatchInlineSnapshot(`
1484
+ Object {
1485
+ "debug": "",
1486
+ "err": "",
1487
+ "info": "",
1488
+ "out": "✨ Created wrangler.toml
1489
+ ✨ Created src/index.ts
1490
+ ✨ Created src/index.test.ts
1491
+ ✨ Installed vitest into devDependencies
1492
+
1493
+ To start developing your Worker, run \`npx wrangler dev\`
1494
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1495
+ "warn": "",
1496
+ }
1497
+ `);
1498
+ });
1499
+
1500
+ it("should not touch an existing tsconfig.json in the ancestor of a target directory, if a name is passed", async () => {
1501
+ mockConfirm(
1502
+ {
1503
+ text: "Would you like to use git to manage this Worker?",
1504
+ result: false,
1505
+ },
1506
+ {
1507
+ text: "No package.json found. Would you like to create one?",
1508
+ result: true,
1509
+ },
1510
+ {
1511
+ text: "Would you like to use TypeScript?",
1512
+ result: true,
1513
+ }
1514
+ );
1515
+ mockSelect({
1516
+ text: `Would you like to create a Worker at ${path.join(
1517
+ "path",
1518
+ "to",
1519
+ "worker",
1520
+ "my-worker",
1521
+ "src",
1522
+ "index.ts"
1523
+ )}?`,
1524
+ result: "fetch",
1525
+ });
1526
+ mockConfirm({
1527
+ text: "Would you like us to write your first test with Vitest?",
1528
+ result: true,
1529
+ });
1530
+ writeFiles({
1531
+ items: {
1532
+ "path/to/worker/package.json": {
1533
+ contents: {
1534
+ name: "test",
1535
+ version: "1.0.0",
1536
+ devDependencies: {
1537
+ wrangler: "0.0.0",
1538
+ "@cloudflare/workers-types": "0.0.0",
1539
+ },
1540
+ },
1541
+ },
1542
+ "path/to/worker/tsconfig.json": { contents: { compilerOptions: {} } },
1543
+ },
1544
+ });
1545
+
1546
+ await runWrangler("init path/to/worker/my-worker");
1547
+
1548
+ checkFiles({
1549
+ items: {
1550
+ "path/to/worker/tsconfig.json": {
1551
+ contents: { config: { compilerOptions: {}, error: undefined } },
1552
+ },
1553
+ },
1554
+ });
1555
+ expect(std).toMatchInlineSnapshot(`
1556
+ Object {
1557
+ "debug": "",
1558
+ "err": "",
1559
+ "info": "",
1560
+ "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1561
+ ✨ Created path/to/worker/my-worker/package.json
1562
+ ✨ Created path/to/worker/my-worker/tsconfig.json
1563
+ ✨ Created path/to/worker/my-worker/src/index.ts
1564
+ ✨ Created path/to/worker/my-worker/src/index.test.ts
1565
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
1566
+
1567
+ To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
1568
+ To start testing your Worker, run \`npm test\`
1569
+ To publish your Worker to the Internet, run \`npm run deploy\`",
1570
+ "warn": "",
1571
+ }
1572
+ `);
1573
+ });
1574
+
1575
+ it("should offer to install type definitions in an existing typescript project", async () => {
1576
+ mockConfirm(
1577
+ {
1578
+ text: "Would you like to use git to manage this Worker?",
1579
+ result: false,
1580
+ },
1581
+ {
1582
+ text: "Would you like to install wrangler into package.json?",
1583
+ result: false,
1584
+ },
1585
+ {
1586
+ text: "Would you like to install the type definitions for Workers into your package.json?",
1587
+ result: true,
1588
+ }
1589
+ );
1590
+ mockSelect({
1591
+ text: `Would you like to create a Worker at ${path.join(
1592
+ "src",
1593
+ "index.ts"
1594
+ )}?`,
1595
+ result: "none",
1596
+ });
1597
+ writeFiles({
1598
+ items: {
1599
+ "./package.json": {
1600
+ contents: {
1601
+ name: "test",
1602
+ version: "1.0.0",
1603
+ },
1604
+ },
1605
+ "./tsconfig.json": { contents: { compilerOptions: {} } },
1606
+ },
1607
+ });
1608
+
1609
+ await runWrangler("init");
1610
+
1611
+ checkFiles({
1612
+ items: {
1613
+ // unchanged tsconfig
1614
+ "tsconfig.json": { contents: { config: { compilerOptions: {} } } },
1615
+ },
1616
+ });
1617
+ expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1618
+ "@cloudflare/workers-types"
1619
+ );
1620
+ expect(std).toMatchInlineSnapshot(`
1621
+ Object {
1622
+ "debug": "",
1623
+ "err": "",
1624
+ "info": "",
1625
+ "out": "✨ Created wrangler.toml
1626
+ ✨ Installed @cloudflare/workers-types into devDependencies
1627
+ 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1628
+ "warn": "",
1629
+ }
1630
+ `);
1631
+ });
1632
+
1633
+ it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1634
+ mockConfirm({
1635
+ text: "Would you like to use git to manage this Worker?",
1636
+ result: false,
1637
+ });
1638
+ mockSelect({
1639
+ text: `Would you like to create a Worker at ${path.join(
1640
+ "src",
1641
+ "index.ts"
1642
+ )}?`,
1643
+ result: "fetch",
1644
+ });
1645
+ mockConfirm({
1646
+ text: "Would you like us to write your first test with Vitest?",
1647
+ result: true,
1648
+ });
1649
+ writeFiles({
1650
+ items: {
1651
+ "package.json": {
1652
+ contents: {
1653
+ name: "test",
1654
+ version: "1.0.0",
1655
+ devDependencies: {
1656
+ wrangler: "0.0.0",
1657
+ "@cloudflare/workers-types": "0.0.0",
1658
+ },
1659
+ },
1660
+ },
1661
+ "tsconfig.json": { contents: { compilerOptions: {} } },
1662
+ },
1663
+ });
1664
+ setWorkingDirectory("./sub-1/sub-2");
1665
+
1666
+ await runWrangler("init");
1667
+
1668
+ checkFiles({
1669
+ items: {
1670
+ "tsconfig.json": false,
1671
+ "../../tsconfig.json": {
1672
+ contents: { config: { compilerOptions: {} } },
1673
+ },
1674
+ },
1675
+ });
1676
+ expect(std).toMatchInlineSnapshot(`
1677
+ Object {
1678
+ "debug": "",
1679
+ "err": "",
1680
+ "info": "",
1681
+ "out": "✨ Created wrangler.toml
1682
+ ✨ Created src/index.ts
1683
+ ✨ Created src/index.test.ts
1684
+ ✨ Installed vitest into devDependencies
1685
+
1686
+ To start developing your Worker, run \`npx wrangler dev\`
1687
+ To publish your Worker to the Internet, run \`npx wrangler publish\`",
1688
+ "warn": "",
1689
+ }
1690
+ `);
1691
+ });
1692
+ });
1693
+
1694
+ describe("javascript", () => {
1695
+ it("should add missing scripts for a non-ts project with .js extension", async () => {
1696
+ mockConfirm(
1697
+ {
1698
+ text: "Would you like to use git to manage this Worker?",
1699
+ result: false,
1700
+ },
1701
+ {
1702
+ text: "No package.json found. Would you like to create one?",
1703
+ result: true,
1704
+ },
1705
+
1706
+ {
1707
+ text: "Would you like to use TypeScript?",
1708
+ result: false,
1709
+ }
1710
+ );
1711
+ mockSelect({
1712
+ text: `Would you like to create a Worker at ${path.join(
1713
+ "src",
1714
+ "index.js"
1715
+ )}?`,
1716
+ result: "fetch",
1717
+ });
1718
+ mockConfirm({
1719
+ text: "Would you like us to write your first test?",
1720
+ result: false,
1721
+ });
1722
+ await runWrangler("init");
1723
+
1724
+ checkFiles({
1725
+ items: {
1726
+ "src/index.js": true,
1727
+ "src/index.ts": false,
1728
+ "package.json": {
1729
+ contents: expect.objectContaining({
1730
+ name: expect.stringContaining("wrangler-tests"),
1731
+ version: "0.0.0",
1732
+ scripts: {
1733
+ start: "wrangler dev",
1734
+ deploy: "wrangler publish",
1735
+ },
1736
+ }),
1737
+ },
1738
+ },
1739
+ });
1740
+ expect(std.out).toMatchInlineSnapshot(`
1741
+ "✨ Created wrangler.toml
1742
+ ✨ Created package.json
1743
+ ✨ Created src/index.js
1744
+
1745
+ To start developing your Worker, run \`npm start\`
1746
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1747
+ `);
1748
+ });
1749
+ it("should add a jest test for a non-ts project with .js extension", async () => {
1750
+ mockConfirm(
1751
+ {
1752
+ text: "Would you like to use git to manage this Worker?",
1753
+ result: false,
1754
+ },
1755
+ {
1756
+ text: "No package.json found. Would you like to create one?",
1757
+ result: true,
1758
+ },
1759
+ {
1760
+ text: "Would you like to use TypeScript?",
1761
+ result: false,
1762
+ }
1763
+ );
1764
+ mockSelect({
1765
+ text: `Would you like to create a Worker at ${path.join(
1766
+ "src",
1767
+ "index.js"
1768
+ )}?`,
1769
+ result: "fetch",
1770
+ });
1771
+ mockConfirm({
1772
+ text: "Would you like us to write your first test?",
1773
+ result: true,
1774
+ });
1775
+ mockSelect({
1776
+ text: "Which test runner would you like to use?",
1777
+ result: "jest",
1778
+ });
1779
+ await runWrangler("init");
1780
+
1781
+ checkFiles({
1782
+ items: {
1783
+ "src/index.js": true,
1784
+ "src/index.test.js": true,
1785
+ "src/index.ts": false,
1786
+ "package.json": {
1787
+ contents: expect.objectContaining({
1788
+ name: expect.stringContaining("wrangler-tests"),
1789
+ version: "0.0.0",
1790
+ scripts: {
1791
+ start: "wrangler dev",
1792
+ deploy: "wrangler publish",
1793
+ test: "jest",
1794
+ },
1795
+ }),
1796
+ },
1797
+ },
1798
+ });
1799
+ expect(std.out).toMatchInlineSnapshot(`
1800
+ "✨ Created wrangler.toml
1801
+ ✨ Created package.json
1802
+ ✨ Created src/index.js
1803
+ ✨ Created src/index.test.js
1804
+ ✨ Installed jest into devDependencies
1805
+
1806
+ To start developing your Worker, run \`npm start\`
1807
+ To start testing your Worker, run \`npm test\`
1808
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1809
+ `);
1810
+ });
1811
+
1812
+ it("should add a vitest test for a non-ts project with .js extension", async () => {
1813
+ mockConfirm(
1814
+ {
1815
+ text: "Would you like to use git to manage this Worker?",
1816
+ result: false,
1817
+ },
1818
+ {
1819
+ text: "No package.json found. Would you like to create one?",
1820
+ result: true,
1821
+ },
1822
+ {
1823
+ text: "Would you like to use TypeScript?",
1824
+ result: false,
1825
+ }
1826
+ );
1827
+ mockSelect({
1828
+ text: `Would you like to create a Worker at ${path.join(
1829
+ "src",
1830
+ "index.js"
1831
+ )}?`,
1832
+ result: "fetch",
1833
+ });
1834
+ mockConfirm({
1835
+ text: "Would you like us to write your first test?",
1836
+ result: true,
1837
+ });
1838
+ mockSelect({
1839
+ text: "Which test runner would you like to use?",
1840
+ result: "vitest",
1841
+ });
1842
+ await runWrangler("init");
1843
+
1844
+ checkFiles({
1845
+ items: {
1846
+ "src/index.js": true,
1847
+ "src/index.test.js": true,
1848
+ "src/index.ts": false,
1849
+ "package.json": {
1850
+ contents: expect.objectContaining({
1851
+ name: expect.stringContaining("wrangler-tests"),
1852
+ version: "0.0.0",
1853
+ scripts: {
1854
+ start: "wrangler dev",
1855
+ deploy: "wrangler publish",
1856
+ test: "vitest",
1857
+ },
1858
+ }),
1859
+ },
1860
+ },
1861
+ });
1862
+ expect(std.out).toMatchInlineSnapshot(`
1863
+ "✨ Created wrangler.toml
1864
+ ✨ Created package.json
1865
+ ✨ Created src/index.js
1866
+ ✨ Created src/index.test.js
1867
+ ✨ Installed vitest into devDependencies
1868
+
1869
+ To start developing your Worker, run \`npm start\`
1870
+ To start testing your Worker, run \`npm test\`
1871
+ To publish your Worker to the Internet, run \`npm run deploy\`"
1872
+ `);
1873
+ });
1874
+
1875
+ it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
1876
+ mockConfirm(
1877
+ {
1878
+ text: "Would you like to use git to manage this Worker?",
1879
+ result: false,
1880
+ },
1881
+ {
1882
+ text: "Would you like to install wrangler into package.json?",
1883
+ result: false,
1884
+ },
1885
+ {
1886
+ text: "Would you like to use TypeScript?",
1887
+ result: false,
1888
+ }
1889
+ );
1890
+ mockSelect({
1891
+ text: `Would you like to create a Worker at ${path.join(
1892
+ "src",
1893
+ "index.js"
1894
+ )}?`,
1895
+ result: "fetch",
1896
+ });
1897
+ mockConfirm({
1898
+ text: "Would you like us to write your first test?",
1899
+ result: false,
1900
+ });
1901
+ writeFiles({
1902
+ items: {
1903
+ "package.json": {
1904
+ contents: {
1905
+ scripts: {
1906
+ start: "test-start",
1907
+ deploy: "test-publish",
1908
+ },
1909
+ },
1910
+ },
1911
+ },
1912
+ });
1913
+
1914
+ await runWrangler("init");
1915
+
1916
+ checkFiles({
1917
+ items: {
1918
+ "src/index.js": true,
1919
+ "src/index.ts": false,
1920
+ "package.json": {
1921
+ contents: expect.objectContaining({
1922
+ scripts: {
1923
+ start: "test-start",
1924
+ deploy: "test-publish",
1925
+ },
1926
+ }),
1927
+ },
1928
+ },
1929
+ });
1930
+ expect(std.out).toMatchInlineSnapshot(`
1931
+ "✨ Created wrangler.toml
1932
+ ✨ Created src/index.js
1933
+
1934
+ To start developing your Worker, run \`npx wrangler dev\`
1935
+ To publish your Worker to the Internet, run \`npx wrangler publish\`"
1936
+ `);
1937
+ });
1938
+
1939
+ it("should not offer to create a worker in a non-ts project if a file already exists at the location", async () => {
1940
+ mockConfirm(
1941
+ {
1942
+ text: "Would you like to use git to manage this Worker?",
1943
+ result: false,
1944
+ },
1945
+ {
1946
+ text: "Would you like to install wrangler into package.json?",
1947
+ result: false,
1948
+ },
1949
+ {
1950
+ text: "Would you like to use TypeScript?",
1951
+ result: false,
1952
+ }
1953
+ );
1954
+ const PLACEHOLDER = "/* placeholder text */";
1955
+ writeFiles({
1956
+ items: {
1957
+ "package.json": { contents: { name: "test", version: "1.0.0" } },
1958
+ "src/index.js": { contents: PLACEHOLDER },
1959
+ },
1960
+ });
1961
+
1962
+ await runWrangler("init");
1963
+
1964
+ checkFiles({
1965
+ items: {
1966
+ "src/index.js": { contents: PLACEHOLDER },
1967
+ "src/index.ts": false,
1968
+ },
1969
+ });
1970
+ expect(std).toMatchInlineSnapshot(`
1971
+ Object {
1972
+ "debug": "",
1973
+ "err": "",
1974
+ "info": "",
1975
+ "out": "✨ Created wrangler.toml",
1976
+ "warn": "",
1977
+ }
1978
+ `);
1979
+ });
1980
+
1981
+ it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1982
+ mockConfirm(
1983
+ {
1984
+ text: "Would you like to use git to manage this Worker?",
1985
+ result: false,
1986
+ },
1987
+ {
1988
+ text: `Would you like to install wrangler into ${path.join(
1989
+ "my-worker",
1990
+ "package.json"
1991
+ )}?`,
1992
+ result: false,
1993
+ },
1994
+ {
1995
+ text: "Would you like to use TypeScript?",
1996
+ result: false,
1997
+ }
1998
+ );
1999
+ const PLACEHOLDER = "/* placeholder text */";
2000
+ writeFiles({
2001
+ items: {
2002
+ "my-worker/package.json": {
2003
+ contents: { name: "test", version: "1.0.0" },
2004
+ },
2005
+ "my-worker/src/index.js": { contents: PLACEHOLDER },
2006
+ },
2007
+ });
2008
+
2009
+ await runWrangler("init my-worker");
2010
+
2011
+ checkFiles({
2012
+ items: {
2013
+ "my-worker/src/index.js": { contents: PLACEHOLDER },
2014
+ "my-worker/src/index.ts": false,
2015
+ },
2016
+ });
2017
+ expect(std).toMatchInlineSnapshot(`
2018
+ Object {
2019
+ "debug": "",
2020
+ "err": "",
2021
+ "info": "",
2022
+ "out": "✨ Created my-worker/wrangler.toml",
2023
+ "warn": "",
2024
+ }
2025
+ `);
2026
+ });
2027
+ });
2028
+
2029
+ describe("worker names", () => {
2030
+ it("should create a worker with a given name", async () => {
2031
+ await runWrangler("init my-worker -y");
2032
+
2033
+ checkFiles({
2034
+ items: {
2035
+ "my-worker/wrangler.toml": wranglerToml({
2036
+ ...MINIMAL_WRANGLER_TOML,
2037
+ name: "my-worker",
2038
+ }),
2039
+ },
2040
+ });
2041
+ });
2042
+
2043
+ it('should create a worker with the name of the current directory if "name" is .', async () => {
2044
+ await runWrangler("init . -y");
2045
+
2046
+ const workerName = path.basename(process.cwd()).toLowerCase();
2047
+ checkFiles({
2048
+ items: {
2049
+ "wrangler.toml": wranglerToml({
2050
+ ...MINIMAL_WRANGLER_TOML,
2051
+ name: workerName,
2052
+ }),
2053
+ "package.json": {
2054
+ contents: expect.objectContaining({
2055
+ name: expect.stringContaining("wrangler-tests"),
2056
+ version: "0.0.0",
2057
+ scripts: {
2058
+ deploy: "wrangler publish",
2059
+ start: "wrangler dev",
2060
+ test: "vitest",
2061
+ },
2062
+ devDependencies: {
2063
+ wrangler: expect.any(String),
2064
+ },
2065
+ }),
2066
+ },
2067
+ },
2068
+ });
2069
+ expect(std).toMatchInlineSnapshot(`
2070
+ Object {
2071
+ "debug": "",
2072
+ "err": "",
2073
+ "info": "Your project will use Vitest to run your tests.",
2074
+ "out": "✨ Created wrangler.toml
2075
+ ✨ Initialized git repository
2076
+ ✨ Created package.json
2077
+ ✨ Created tsconfig.json
2078
+ ✨ Created src/index.ts
2079
+ ✨ Created src/index.test.ts
2080
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2081
+
2082
+ To start developing your Worker, run \`npm start\`
2083
+ To start testing your Worker, run \`npm test\`
2084
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2085
+ "warn": "",
2086
+ }
2087
+ `);
2088
+ });
2089
+
2090
+ it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
2091
+ await runWrangler("init path/to/worker -y");
2092
+
2093
+ checkFiles({
2094
+ items: {
2095
+ "path/to/worker/wrangler.toml": wranglerToml({
2096
+ ...MINIMAL_WRANGLER_TOML,
2097
+ name: "worker",
2098
+ }),
2099
+ },
2100
+ });
2101
+ expect(std).toMatchInlineSnapshot(`
2102
+ Object {
2103
+ "debug": "",
2104
+ "err": "",
2105
+ "info": "Your project will use Vitest to run your tests.",
2106
+ "out": "✨ Created path/to/worker/wrangler.toml
2107
+ ✨ Initialized git repository at path/to/worker
2108
+ ✨ Created path/to/worker/package.json
2109
+ ✨ Created path/to/worker/tsconfig.json
2110
+ ✨ Created path/to/worker/src/index.ts
2111
+ ✨ Created path/to/worker/src/index.test.ts
2112
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2113
+
2114
+ To start developing your Worker, run \`cd path/to/worker && npm start\`
2115
+ To start testing your Worker, run \`npm test\`
2116
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2117
+ "warn": "",
2118
+ }
2119
+ `);
2120
+ });
2121
+
2122
+ it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
2123
+ await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
2124
+
2125
+ checkFiles({
2126
+ items: {
2127
+ "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml": wranglerToml({
2128
+ ...MINIMAL_WRANGLER_TOML,
2129
+ name: "weird_w0rkr_n4m3-js-tsx-tar-gz",
2130
+ }),
2131
+ },
2132
+ });
2133
+ expect(std).toMatchInlineSnapshot(`
2134
+ Object {
2135
+ "debug": "",
2136
+ "err": "",
2137
+ "info": "Your project will use Vitest to run your tests.",
2138
+ "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
2139
+ ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
2140
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
2141
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
2142
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
2143
+ ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.test.ts
2144
+ ✨ Installed @cloudflare/workers-types, typescript, and vitest into devDependencies
2145
+
2146
+ To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
2147
+ To start testing your Worker, run \`npm test\`
2148
+ To publish your Worker to the Internet, run \`npm run deploy\`",
2149
+ "warn": "",
2150
+ }
2151
+ `);
2152
+ });
2153
+
2154
+ it("should ignore ancestor files (such as wrangler.toml, package.json and tsconfig.json) if a name/path is given", async () => {
2155
+ mockConfirm(
2156
+ {
2157
+ text: "Would you like to use git to manage this Worker?",
2158
+ result: false,
2159
+ },
2160
+ {
2161
+ text: "No package.json found. Would you like to create one?",
2162
+ result: true,
2163
+ },
2164
+ {
2165
+ text: "Would you like to use TypeScript?",
2166
+ result: true,
2167
+ }
2168
+ );
2169
+ mockSelect({
2170
+ text: `Would you like to create a Worker at ${path.join(
2171
+ "sub",
2172
+ "folder",
2173
+ "worker",
2174
+ "src",
2175
+ "index.ts"
2176
+ )}?`,
2177
+ result: "fetch",
2178
+ });
2179
+ mockConfirm({
2180
+ text: "Would you like us to write your first test with Vitest?",
2181
+ result: true,
2182
+ });
2183
+ writeFiles({
2184
+ items: {
2185
+ "package.json": { contents: { name: "top-level" } },
2186
+ "tsconfig.json": { contents: { compilerOptions: {} } },
2187
+ "wrangler.toml": wranglerToml({
2188
+ name: "top-level",
2189
+ compatibility_date: "some-date",
2190
+ }),
2191
+ },
2192
+ });
2193
+
2194
+ await runWrangler("init sub/folder/worker");
2195
+
2196
+ // Ancestor files are untouched.
2197
+ checkFiles({
2198
+ items: {
2199
+ "package.json": { contents: { name: "top-level" } },
2200
+ "tsconfig.json": {
2201
+ contents: { config: { compilerOptions: {} }, error: undefined },
2202
+ },
2203
+ "wrangler.toml": wranglerToml({
2204
+ name: "top-level",
2205
+ compatibility_date: "some-date",
2206
+ }),
2207
+ },
2208
+ });
2209
+ // New initialized Worker has its own files.
2210
+ checkFiles({
2211
+ items: {
2212
+ "sub/folder/worker/package.json": {
2213
+ contents: expect.objectContaining({
2214
+ name: "worker",
2215
+ }),
2216
+ },
2217
+ "sub/folder/worker/tsconfig.json": true,
2218
+ "sub/folder/worker/wrangler.toml": wranglerToml({
2219
+ ...MINIMAL_WRANGLER_TOML,
2220
+ name: "worker",
2221
+ }),
2222
+ },
2223
+ });
2224
+ });
2225
+ });
2226
+
2227
+ describe("from dashboard", () => {
2228
+ mockApiToken();
2229
+ mockAccountId({ accountId: "LCARS" });
2230
+
2231
+ const mockDashboardScript = `
2232
+ export default {
2233
+ async fetch(request, env, ctx) {
2234
+ return new Response("Hello World!");
2235
+ },
2236
+ };
2237
+ `;
2238
+ const mockServiceMetadata = {
2239
+ id: "memory-crystal",
2240
+ default_environment: {
2241
+ environment: "test",
2242
+ created_on: "1987-9-27",
2243
+ modified_on: "1987-9-27",
2244
+ script: {
2245
+ id: "memory-crystal",
2246
+ tag: "test-tag",
2247
+ etag: "some-etag",
2248
+ handlers: [],
2249
+ modified_on: "1987-9-27",
2250
+ created_on: "1987-9-27",
2251
+ migration_tag: "some-migration-tag",
2252
+ usage_model: "bundled",
2253
+ compatibility_date: "1987-9-27",
2254
+ },
2255
+ },
2256
+ created_on: "1987-9-27",
2257
+ modified_on: "1987-9-27",
2258
+ usage_model: "bundled",
2259
+ environments: [
2260
+ {
2261
+ environment: "test",
2262
+ created_on: "1987-9-27",
2263
+ modified_on: "1987-9-27",
2264
+ },
2265
+ {
2266
+ environment: "staging",
2267
+ created_on: "1987-9-27",
2268
+ modified_on: "1987-9-27",
2269
+ },
2270
+ ],
2271
+ };
2272
+ const mockBindingsRes = [
2273
+ {
2274
+ type: "secret_text",
2275
+ name: "ABC",
2276
+ },
2277
+ {
2278
+ type: "plain_text",
2279
+ name: "ANOTHER-NAME",
2280
+ text: "thing-TEXT",
2281
+ },
2282
+ {
2283
+ type: "durable_object_namespace",
2284
+ name: "DURABLE_TEST",
2285
+ class_name: "Durability",
2286
+ script_name: "another-durable-object-worker",
2287
+ environment: "production",
2288
+ },
2289
+ {
2290
+ type: "kv_namespace",
2291
+ name: "kv_testing",
2292
+ namespace_id: "some-namespace-id",
2293
+ },
2294
+ {
2295
+ type: "r2_bucket",
2296
+ bucket_name: "test-bucket",
2297
+ name: "test-bucket",
2298
+ },
2299
+ {
2300
+ environment: "production",
2301
+ name: "website",
2302
+ service: "website",
2303
+ type: "service",
2304
+ },
2305
+ {
2306
+ type: "dispatch_namespace",
2307
+ name: "name-namespace-mock",
2308
+ namespace: "namespace-mock",
2309
+ },
2310
+ {
2311
+ name: "httplogs",
2312
+ type: "logfwdr",
2313
+ destination: "httplogs",
2314
+ },
2315
+ {
2316
+ name: "trace",
2317
+ type: "logfwdr",
2318
+ destination: "trace",
2319
+ },
2320
+ {
2321
+ type: "wasm_module",
2322
+ name: "WASM_MODULE_ONE",
2323
+ part: "./some_wasm.wasm",
2324
+ },
2325
+ {
2326
+ type: "wasm_module",
2327
+ name: "WASM_MODULE_TWO",
2328
+ part: "./more_wasm.wasm",
2329
+ },
2330
+ {
2331
+ type: "text_blob",
2332
+ name: "TEXT_BLOB_ONE",
2333
+ part: "./my-entire-app-depends-on-this.cfg",
2334
+ },
2335
+ {
2336
+ type: "text_blob",
2337
+ name: "TEXT_BLOB_TWO",
2338
+ part: "./the-entirety-of-human-knowledge.txt",
2339
+ },
2340
+ { type: "data_blob", name: "DATA_BLOB_ONE", part: "DATA_BLOB_ONE" },
2341
+ { type: "data_blob", name: "DATA_BLOB_TWO", part: "DATA_BLOB_TWO" },
2342
+ {
2343
+ type: "some unsafe thing",
2344
+ name: "UNSAFE_BINDING_ONE",
2345
+ data: { some: { unsafe: "thing" } },
2346
+ },
2347
+ {
2348
+ type: "another unsafe thing",
2349
+ name: "UNSAFE_BINDING_TWO",
2350
+ data: 1337,
2351
+ },
2352
+ ];
2353
+ const mockRoutesRes = [
2354
+ {
2355
+ id: "some-route-id",
2356
+ pattern: "delta.quadrant",
2357
+ },
2358
+ ];
2359
+ const mockConfigExpected = {
2360
+ main: "src/index.ts",
2361
+ compatibility_date: "1987-9-27",
2362
+ name: "isolinear-optical-chip",
2363
+ migrations: [
2364
+ {
2365
+ new_classes: ["Durability"],
2366
+ tag: "some-migration-tag",
2367
+ },
2368
+ ],
2369
+ durable_objects: {
2370
+ bindings: [
2371
+ {
2372
+ class_name: "Durability",
2373
+ name: "DURABLE_TEST",
2374
+ script_name: "another-durable-object-worker",
2375
+ environment: "production",
2376
+ },
2377
+ ],
2378
+ },
2379
+ kv_namespaces: [
2380
+ {
2381
+ binding: "kv_testing",
2382
+ id: "some-namespace-id",
2383
+ },
2384
+ ],
2385
+ r2_buckets: [
2386
+ {
2387
+ bucket_name: "test-bucket",
2388
+ binding: "test-bucket",
2389
+ },
2390
+ ],
2391
+ dispatch_namespaces: [
2392
+ {
2393
+ binding: "name-namespace-mock",
2394
+ namespace: "namespace-mock",
2395
+ },
2396
+ ],
2397
+ route: "delta.quadrant",
2398
+ services: [
2399
+ {
2400
+ environment: "production",
2401
+ binding: "website",
2402
+ service: "website",
2403
+ },
2404
+ ],
2405
+ triggers: {
2406
+ crons: ["0 0 0 * * *"],
2407
+ },
2408
+ usage_model: "bundled",
2409
+ vars: {
2410
+ "ANOTHER-NAME": "thing-TEXT",
2411
+ },
2412
+ env: {
2413
+ test: {},
2414
+ staging: {},
2415
+ },
2416
+ unsafe: {
2417
+ bindings: [
2418
+ {
2419
+ name: "UNSAFE_BINDING_ONE",
2420
+ type: "some unsafe thing",
2421
+ data: { some: { unsafe: "thing" } },
2422
+ },
2423
+ {
2424
+ name: "UNSAFE_BINDING_TWO",
2425
+ type: "another unsafe thing",
2426
+ data: 1337,
2427
+ },
2428
+ ],
2429
+ },
2430
+ wasm_modules: {
2431
+ WASM_MODULE_ONE: "./some_wasm.wasm",
2432
+ WASM_MODULE_TWO: "./more_wasm.wasm",
2433
+ },
2434
+ text_blobs: {
2435
+ TEXT_BLOB_ONE: "./my-entire-app-depends-on-this.cfg",
2436
+ TEXT_BLOB_TWO: "./the-entirety-of-human-knowledge.txt",
2437
+ },
2438
+ data_blobs: {
2439
+ DATA_BLOB_ONE: "DATA_BLOB_ONE",
2440
+ DATA_BLOB_TWO: "DATA_BLOB_TWO",
2441
+ },
2442
+ logfwdr: {
2443
+ schema: "",
2444
+ bindings: [
2445
+ {
2446
+ name: "httplogs",
2447
+ destination: "httplogs",
2448
+ },
2449
+ {
2450
+ name: "trace",
2451
+ destination: "trace",
2452
+ },
2453
+ ],
2454
+ },
2455
+ };
2456
+
2457
+ function mockSupportingDashRequests({
2458
+ expectedAccountId = "",
2459
+ expectedScriptName = "",
2460
+ expectedEnvironment = "",
2461
+ expectedCompatDate,
2462
+ }: {
2463
+ expectedAccountId: string;
2464
+ expectedScriptName: string;
2465
+ expectedEnvironment: string;
2466
+ expectedCompatDate: string | undefined;
2467
+ }) {
2468
+ msw.use(
2469
+ rest.get(
2470
+ `*/accounts/:accountId/workers/services/:scriptName`,
2471
+ (req, res, ctx) => {
2472
+ expect(req.params.accountId).toEqual(expectedAccountId);
2473
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2474
+
2475
+ if (expectedCompatDate === undefined)
2476
+ (mockServiceMetadata.default_environment.script
2477
+ .compatibility_date as unknown) = expectedCompatDate;
2478
+
2479
+ return res.once(
2480
+ ctx.status(200),
2481
+ ctx.json({
2482
+ success: true,
2483
+ errors: [],
2484
+ messages: [],
2485
+ result: mockServiceMetadata,
2486
+ })
2487
+ );
2488
+ }
2489
+ ),
2490
+ rest.get(
2491
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2492
+ (req, res, ctx) => {
2493
+ expect(req.params.accountId).toEqual(expectedAccountId);
2494
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2495
+ expect(req.params.environment).toEqual(expectedEnvironment);
2496
+
2497
+ return res.once(
2498
+ ctx.status(200),
2499
+ ctx.json({
2500
+ success: true,
2501
+ errors: [],
2502
+ messages: [],
2503
+ result: mockBindingsRes,
2504
+ })
2505
+ );
2506
+ }
2507
+ ),
2508
+ rest.get(
2509
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2510
+ (req, res, ctx) => {
2511
+ expect(req.params.accountId).toEqual(expectedAccountId);
2512
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2513
+ expect(req.params.environment).toEqual(expectedEnvironment);
2514
+
2515
+ return res.once(
2516
+ ctx.status(200),
2517
+ ctx.json({
2518
+ success: true,
2519
+ errors: [],
2520
+ messages: [],
2521
+ result: mockRoutesRes,
2522
+ })
2523
+ );
2524
+ }
2525
+ ),
2526
+ rest.get(
2527
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2528
+
2529
+ (req, res, ctx) => {
2530
+ expect(req.params.accountId).toEqual(expectedAccountId);
2531
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2532
+ expect(req.params.environment).toEqual(expectedEnvironment);
2533
+
2534
+ return res.once(
2535
+ ctx.status(200),
2536
+ ctx.json({
2537
+ success: true,
2538
+ errors: [],
2539
+ messages: [],
2540
+ result: mockServiceMetadata.default_environment,
2541
+ })
2542
+ );
2543
+ }
2544
+ ),
2545
+ rest.get(
2546
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
2547
+ (req, res, ctx) => {
2548
+ expect(req.params.accountId).toEqual(expectedAccountId);
2549
+ expect(req.params.scriptName).toEqual(expectedScriptName);
2550
+
2551
+ return res.once(
2552
+ ctx.status(200),
2553
+ ctx.json({
2554
+ success: true,
2555
+ errors: [],
2556
+ messages: [],
2557
+ result: {
2558
+ schedules: [
2559
+ {
2560
+ cron: "0 0 0 * * *",
2561
+ created_on: new Date(1987, 9, 27),
2562
+ modified_on: new Date(1987, 9, 27),
2563
+ },
2564
+ ],
2565
+ },
2566
+ })
2567
+ );
2568
+ }
2569
+ )
2570
+ );
2571
+ }
2572
+
2573
+ //TODO: Tests for a case when a worker name doesn't exist - JACOB & CASS
2574
+ it("should download source script from dashboard w/ positional <name> in TypeScript project", async () => {
2575
+ mockSupportingDashRequests({
2576
+ expectedAccountId: "LCARS",
2577
+ expectedScriptName: "memory-crystal",
2578
+ expectedEnvironment: "test",
2579
+ expectedCompatDate: "1987-9-27",
2580
+ });
2581
+ setMockFetchDashScript(mockDashboardScript);
2582
+ mockConfirm(
2583
+ {
2584
+ text: "Would you like to use git to manage this Worker?",
2585
+ result: false,
2586
+ },
2587
+ {
2588
+ text: "No package.json found. Would you like to create one?",
2589
+ result: true,
2590
+ },
2591
+ {
2592
+ text: "Would you like to use TypeScript?",
2593
+ result: true,
2594
+ }
2595
+ );
2596
+
2597
+ await runWrangler(
2598
+ "init isolinear-optical-chip --from-dash memory-crystal"
2599
+ );
2600
+
2601
+ expect(std.out).toContain("cd isolinear-optical-chip");
2602
+
2603
+ checkFiles({
2604
+ items: {
2605
+ "isolinear-optical-chip/src/index.js": false,
2606
+ "isolinear-optical-chip/src/index.ts": {
2607
+ contents: mockDashboardScript,
2608
+ },
2609
+ "isolinear-optical-chip/package.json": {
2610
+ contents: expect.objectContaining({
2611
+ name: "isolinear-optical-chip",
2612
+ }),
2613
+ },
2614
+ "isolinear-optical-chip/tsconfig.json": true,
2615
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2616
+ ...mockConfigExpected,
2617
+ }),
2618
+ },
2619
+ });
2620
+ });
2621
+
2622
+ it("should fail on init --from-dash on non-existent worker name", async () => {
2623
+ msw.use(
2624
+ rest.get(
2625
+ `*/accounts/:accountId/workers/services/:scriptName`,
2626
+ (req, res, ctx) => {
2627
+ return res.once(
2628
+ ctx.status(200),
2629
+ ctx.json({
2630
+ success: true,
2631
+ errors: [],
2632
+ messages: [],
2633
+ result: mockServiceMetadata,
2634
+ })
2635
+ );
2636
+ }
2637
+ )
2638
+ );
2639
+ setMockFetchDashScript(mockDashboardScript);
2640
+ mockConfirm(
2641
+ {
2642
+ text: "Would you like to use git to manage this Worker?",
2643
+ result: false,
2644
+ },
2645
+ {
2646
+ text: "No package.json found. Would you like to create one?",
2647
+ result: true,
2648
+ },
2649
+ {
2650
+ text: "Would you like to use TypeScript?",
2651
+ result: true,
2652
+ }
2653
+ );
2654
+
2655
+ await expect(
2656
+ runWrangler("init isolinear-optical-chip --from-dash i-dont-exist")
2657
+ ).rejects.toThrowError();
2658
+ });
2659
+
2660
+ it("should download source script from dashboard w/ out positional <name>", async () => {
2661
+ mockSupportingDashRequests({
2662
+ expectedAccountId: "LCARS",
2663
+ expectedScriptName: "isolinear-optical-chip",
2664
+ expectedEnvironment: "test",
2665
+ expectedCompatDate: "1987-9-27",
2666
+ });
2667
+ setMockFetchDashScript(mockDashboardScript);
2668
+ mockConfirm(
2669
+ {
2670
+ text: "Would you like to use git to manage this Worker?",
2671
+ result: false,
2672
+ },
2673
+ {
2674
+ text: "No package.json found. Would you like to create one?",
2675
+ result: true,
2676
+ },
2677
+ {
2678
+ text: "Would you like to use TypeScript?",
2679
+ result: true,
2680
+ }
2681
+ );
2682
+
2683
+ await runWrangler("init --from-dash isolinear-optical-chip");
2684
+
2685
+ expect(fs.readFileSync("./isolinear-optical-chip/wrangler.toml", "utf8"))
2686
+ .toMatchInlineSnapshot(`
2687
+ "name = \\"isolinear-optical-chip\\"
2688
+ main = \\"src/index.ts\\"
2689
+ compatibility_date = \\"1987-9-27\\"
2690
+ route = \\"delta.quadrant\\"
2691
+ usage_model = \\"bundled\\"
2692
+
2693
+ [[migrations]]
2694
+ tag = \\"some-migration-tag\\"
2695
+ new_classes = [ \\"Durability\\" ]
2696
+
2697
+ [triggers]
2698
+ crons = [ \\"0 0 0 * * *\\" ]
2699
+
2700
+ [env]
2701
+ test = { }
2702
+ staging = { }
2703
+
2704
+ [vars]
2705
+ ANOTHER-NAME = \\"thing-TEXT\\"
2706
+
2707
+ [[durable_objects.bindings]]
2708
+ name = \\"DURABLE_TEST\\"
2709
+ class_name = \\"Durability\\"
2710
+ script_name = \\"another-durable-object-worker\\"
2711
+ environment = \\"production\\"
2712
+
2713
+ [[kv_namespaces]]
2714
+ id = \\"some-namespace-id\\"
2715
+ binding = \\"kv_testing\\"
2716
+
2717
+ [[r2_buckets]]
2718
+ binding = \\"test-bucket\\"
2719
+ bucket_name = \\"test-bucket\\"
2720
+
2721
+ [[services]]
2722
+ binding = \\"website\\"
2723
+ service = \\"website\\"
2724
+ environment = \\"production\\"
2725
+
2726
+ [[dispatch_namespaces]]
2727
+ binding = \\"name-namespace-mock\\"
2728
+ namespace = \\"namespace-mock\\"
2729
+
2730
+ [logfwdr]
2731
+ schema = \\"\\"
2732
+
2733
+ [[logfwdr.bindings]]
2734
+ name = \\"httplogs\\"
2735
+ destination = \\"httplogs\\"
2736
+
2737
+ [[logfwdr.bindings]]
2738
+ name = \\"trace\\"
2739
+ destination = \\"trace\\"
2740
+
2741
+ [wasm_modules]
2742
+ WASM_MODULE_ONE = \\"./some_wasm.wasm\\"
2743
+ WASM_MODULE_TWO = \\"./more_wasm.wasm\\"
2744
+
2745
+ [text_blobs]
2746
+ TEXT_BLOB_ONE = \\"./my-entire-app-depends-on-this.cfg\\"
2747
+ TEXT_BLOB_TWO = \\"./the-entirety-of-human-knowledge.txt\\"
2748
+
2749
+ [data_blobs]
2750
+ DATA_BLOB_ONE = \\"DATA_BLOB_ONE\\"
2751
+ DATA_BLOB_TWO = \\"DATA_BLOB_TWO\\"
2752
+
2753
+ [unsafe]
2754
+ [[unsafe.bindings]]
2755
+ type = \\"some unsafe thing\\"
2756
+ name = \\"UNSAFE_BINDING_ONE\\"
2757
+
2758
+ [unsafe.bindings.data.some]
2759
+ unsafe = \\"thing\\"
2760
+
2761
+ [[unsafe.bindings]]
2762
+ type = \\"another unsafe thing\\"
2763
+ name = \\"UNSAFE_BINDING_TWO\\"
2764
+ data = 1_337
2765
+ "
2766
+ `);
2767
+ expect(std.out).toContain("cd isolinear-optical-chip");
2768
+
2769
+ checkFiles({
2770
+ items: {
2771
+ "isolinear-optical-chip/src/index.js": false,
2772
+ "isolinear-optical-chip/src/index.ts": {
2773
+ contents: mockDashboardScript,
2774
+ },
2775
+ "isolinear-optical-chip/package.json": {
2776
+ contents: expect.objectContaining({
2777
+ name: "isolinear-optical-chip",
2778
+ }),
2779
+ },
2780
+ "isolinear-optical-chip/tsconfig.json": true,
2781
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2782
+ ...mockConfigExpected,
2783
+ name: "isolinear-optical-chip",
2784
+ }),
2785
+ },
2786
+ });
2787
+ });
2788
+
2789
+ it("should download source script from dashboard as plain JavaScript", async () => {
2790
+ mockSupportingDashRequests({
2791
+ expectedAccountId: "LCARS",
2792
+ expectedScriptName: "isolinear-optical-chip",
2793
+ expectedEnvironment: "test",
2794
+ expectedCompatDate: "1987-9-27",
2795
+ });
2796
+ setMockFetchDashScript(mockDashboardScript);
2797
+ mockConfirm(
2798
+ {
2799
+ text: "Would you like to use git to manage this Worker?",
2800
+ result: false,
2801
+ },
2802
+ {
2803
+ text: "No package.json found. Would you like to create one?",
2804
+ result: true,
2805
+ },
2806
+ {
2807
+ text: "Would you like to use TypeScript?",
2808
+ result: false,
2809
+ }
2810
+ );
2811
+
2812
+ await runWrangler("init --from-dash isolinear-optical-chip");
2813
+
2814
+ checkFiles({
2815
+ items: {
2816
+ "isolinear-optical-chip/src/index.js": {
2817
+ contents: mockDashboardScript,
2818
+ },
2819
+ "isolinear-optical-chip/src/index.ts": false,
2820
+ "isolinear-optical-chip/package.json": {
2821
+ contents: expect.objectContaining({
2822
+ name: "isolinear-optical-chip",
2823
+ }),
2824
+ },
2825
+ "isolinear-optical-chip/tsconfig.json": false,
2826
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2827
+ ...mockConfigExpected,
2828
+ name: "isolinear-optical-chip",
2829
+ main: "src/index.js",
2830
+ }),
2831
+ },
2832
+ });
2833
+ });
2834
+
2835
+ it("should use fallback compatibility date if none is upstream", async () => {
2836
+ const mockDate = "1988-08-07";
2837
+ jest
2838
+ .spyOn(Date.prototype, "toISOString")
2839
+ .mockImplementation(() => `${mockDate}T00:00:00.000Z`);
2840
+
2841
+ mockSupportingDashRequests({
2842
+ expectedAccountId: "LCARS",
2843
+ expectedScriptName: "isolinear-optical-chip",
2844
+ expectedEnvironment: "test",
2845
+ expectedCompatDate: undefined,
2846
+ });
2847
+ setMockFetchDashScript(mockDashboardScript);
2848
+ mockConfirm(
2849
+ {
2850
+ text: "Would you like to use git to manage this Worker?",
2851
+ result: false,
2852
+ },
2853
+ {
2854
+ text: "No package.json found. Would you like to create one?",
2855
+ result: true,
2856
+ },
2857
+ {
2858
+ text: "Would you like to use TypeScript?",
2859
+ result: true,
2860
+ }
2861
+ );
2862
+
2863
+ await runWrangler("init --from-dash isolinear-optical-chip");
2864
+
2865
+ mockConfigExpected.compatibility_date = "1988-08-07";
2866
+ checkFiles({
2867
+ items: {
2868
+ "isolinear-optical-chip/src/index.ts": {
2869
+ contents: mockDashboardScript,
2870
+ },
2871
+ "isolinear-optical-chip/package.json": {
2872
+ contents: expect.objectContaining({
2873
+ name: "isolinear-optical-chip",
2874
+ }),
2875
+ },
2876
+ "isolinear-optical-chip/tsconfig.json": true,
2877
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
2878
+ ...mockConfigExpected,
2879
+ name: "isolinear-optical-chip",
2880
+ }),
2881
+ },
2882
+ });
2883
+ });
2884
+
2885
+ it("should throw an error to retry if a request fails", async () => {
2886
+ msw.use(
2887
+ rest.get(
2888
+ `*/accounts/:accountId/workers/services/:scriptName`,
2889
+ (req, res, ctx) => {
2890
+ expect(req.params.accountId).toBe("LCARS");
2891
+ expect(req.params.scriptName).toBe("isolinear-optical-chip");
2892
+ return res.once(
2893
+ ctx.status(200),
2894
+ ctx.json({
2895
+ success: true,
2896
+ errors: [],
2897
+ messages: [],
2898
+ result: mockServiceMetadata,
2899
+ })
2900
+ );
2901
+ }
2902
+ ),
2903
+ rest.get(
2904
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2905
+ (req, res) => {
2906
+ return res.networkError("Mock Network Error");
2907
+ }
2908
+ )
2909
+ );
2910
+
2911
+ setMockFetchDashScript(mockDashboardScript);
2912
+ mockConfirm(
2913
+ {
2914
+ text: "Would you like to use git to manage this Worker?",
2915
+ result: false,
2916
+ },
2917
+ {
2918
+ text: "Would you like to use TypeScript?",
2919
+ result: false,
2920
+ }
2921
+ );
2922
+
2923
+ await expect(
2924
+ runWrangler("init --from-dash isolinear-optical-chip")
2925
+ ).rejects.toThrowError();
2926
+ });
2927
+
2928
+ it("should not include migrations in config file when none are necessary", async () => {
2929
+ const mockDate = "1988-08-07";
2930
+ jest
2931
+ .spyOn(Date.prototype, "toISOString")
2932
+ .mockImplementation(() => `${mockDate}T00:00:00.000Z`);
2933
+ const mockData = {
2934
+ id: "memory-crystal",
2935
+ default_environment: {
2936
+ environment: "test",
2937
+ created_on: "1988-08-07",
2938
+ modified_on: "1988-08-07",
2939
+ script: {
2940
+ id: "memory-crystal",
2941
+ tag: "test-tag",
2942
+ etag: "some-etag",
2943
+ handlers: [],
2944
+ modified_on: "1988-08-07",
2945
+ created_on: "1988-08-07",
2946
+ usage_model: "bundled",
2947
+ compatibility_date: "1988-08-07",
2948
+ },
2949
+ },
2950
+ environments: [],
2951
+ };
2952
+ msw.use(
2953
+ rest.get(
2954
+ `*/accounts/:accountId/workers/services/:scriptName`,
2955
+ (req, res, ctx) => {
2956
+ return res.once(
2957
+ ctx.status(200),
2958
+ ctx.json({
2959
+ success: true,
2960
+ errors: [],
2961
+ messages: [],
2962
+ result: mockData,
2963
+ })
2964
+ );
2965
+ }
2966
+ ),
2967
+ rest.get(
2968
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/bindings`,
2969
+ (req, res, ctx) => {
2970
+ return res.once(
2971
+ ctx.status(200),
2972
+ ctx.json({
2973
+ success: true,
2974
+ errors: [],
2975
+ messages: [],
2976
+ result: [],
2977
+ })
2978
+ );
2979
+ }
2980
+ ),
2981
+ rest.get(
2982
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment/routes`,
2983
+ (req, res, ctx) => {
2984
+ return res.once(
2985
+ ctx.status(200),
2986
+ ctx.json({
2987
+ success: true,
2988
+ errors: [],
2989
+ messages: [],
2990
+ result: [],
2991
+ })
2992
+ );
2993
+ }
2994
+ ),
2995
+ rest.get(
2996
+ `*/accounts/:accountId/workers/services/:scriptName/environments/:environment`,
2997
+ (req, res, ctx) => {
2998
+ return res.once(
2999
+ ctx.status(200),
3000
+ ctx.json({
3001
+ success: true,
3002
+ errors: [],
3003
+ messages: [],
3004
+ result: mockServiceMetadata.default_environment,
3005
+ })
3006
+ );
3007
+ }
3008
+ ),
3009
+ rest.get(
3010
+ `*/accounts/:accountId/workers/scripts/:scriptName/schedules`,
3011
+ (req, res, ctx) => {
3012
+ return res.once(
3013
+ ctx.status(200),
3014
+ ctx.json({
3015
+ success: true,
3016
+ errors: [],
3017
+ messages: [],
3018
+ result: { schedules: [] },
3019
+ })
3020
+ );
3021
+ }
3022
+ )
3023
+ );
3024
+
3025
+ setMockFetchDashScript(mockDashboardScript);
3026
+
3027
+ mockConfirm(
3028
+ {
3029
+ text: "Would you like to use git to manage this Worker?",
3030
+ result: false,
3031
+ },
3032
+
3033
+ {
3034
+ text: "No package.json found. Would you like to create one?",
3035
+ result: true,
3036
+ },
3037
+ {
3038
+ text: "Would you like to use TypeScript?",
3039
+ result: true,
3040
+ }
3041
+ );
3042
+
3043
+ await runWrangler("init --from-dash isolinear-optical-chip");
3044
+
3045
+ checkFiles({
3046
+ items: {
3047
+ "isolinear-optical-chip/wrangler.toml": wranglerToml({
3048
+ compatibility_date: "1988-08-07",
3049
+ env: {},
3050
+ main: "src/index.ts",
3051
+ triggers: {
3052
+ crons: [],
3053
+ },
3054
+ usage_model: "bundled",
3055
+ name: "isolinear-optical-chip",
3056
+ }),
3057
+ },
3058
+ });
3059
+ });
3060
+
3061
+ it("should not continue if no worker name is provided", async () => {
3062
+ await expect(
3063
+ runWrangler("init --from-dash")
3064
+ ).rejects.toMatchInlineSnapshot(
3065
+ `[Error: Not enough arguments following: from-dash]`
3066
+ );
3067
+ checkFiles({
3068
+ items: {
3069
+ "isolinear-optical-chip/src/index.js": false,
3070
+ "isolinear-optical-chip/src/index.ts": false,
3071
+ "isolinear-optical-chip/package.json": false,
3072
+ "isolinear-optical-chip/tsconfig.json": false,
3073
+ "isolinear-optical-chip/wrangler.toml": false,
3074
+ },
3075
+ });
3076
+ });
3077
+ });
1516
3078
  });
3079
+
3080
+ function getDefaultBranchName() {
3081
+ try {
3082
+ const { stdout: defaultBranchName } = execaSync("git", [
3083
+ "config",
3084
+ "--get",
3085
+ "init.defaultBranch",
3086
+ ]);
3087
+
3088
+ return defaultBranchName;
3089
+ } catch {
3090
+ // ew
3091
+ return "master";
3092
+ }
3093
+ }
3094
+
3095
+ /**
3096
+ * Mock setter for usage within test blocks for dashboard script
3097
+ */
3098
+ export function setMockFetchDashScript(mockResponse: string) {
3099
+ msw.use(
3100
+ rest.get(
3101
+ `*/accounts/:accountId/workers/services/:fromDashScriptName/environments/:environment/content`,
3102
+ (_, res, ctx) => {
3103
+ return res(ctx.text(mockResponse));
3104
+ }
3105
+ )
3106
+ );
3107
+ }
3108
+
3109
+ /**
3110
+ * Change the current working directory, ensuring that this exists.
3111
+ */
3112
+ function setWorkingDirectory(directory: string) {
3113
+ fs.mkdirSync(directory, { recursive: true });
3114
+ process.chdir(directory);
3115
+ }
3116
+
3117
+ /**
3118
+ * Write the given folder/files to disk at the given `cwd`.
3119
+ */
3120
+ function writeFiles(folder: TestFolder, cwd = process.cwd()) {
3121
+ for (const name in folder.items) {
3122
+ const item = folder.items[name];
3123
+ const itemPath = path.resolve(cwd, name);
3124
+ if (typeof item !== "boolean") {
3125
+ if ("contents" in item) {
3126
+ fs.mkdirSync(path.dirname(itemPath), { recursive: true });
3127
+ fs.writeFileSync(itemPath, stringify(name, item.contents));
3128
+ } else {
3129
+ fs.mkdirSync(itemPath, { recursive: true });
3130
+ writeFiles(item, itemPath);
3131
+ }
3132
+ } else {
3133
+ throw new Error("Cannot write boolean flags to disk: " + itemPath);
3134
+ }
3135
+ }
3136
+ }
3137
+
3138
+ /**
3139
+ * Check that the given test folders/files match what is in on disk.
3140
+ */
3141
+ function checkFiles(folder: TestFolder, cwd = process.cwd()) {
3142
+ for (const name in folder.items) {
3143
+ const item = folder.items[name];
3144
+ const itemPath = path.resolve(cwd, name);
3145
+ if (typeof item === "boolean") {
3146
+ if (fs.existsSync(itemPath) !== item) {
3147
+ throw new Error(`Expected ${itemPath} ${item ? "" : "not "}to exist.`);
3148
+ }
3149
+ } else if ("contents" in item) {
3150
+ const actualContents = parse(name, fs.readFileSync(itemPath, "utf-8"));
3151
+ expect(actualContents).toEqual(item.contents);
3152
+ } else if ("items" in item) {
3153
+ checkFiles(item, itemPath);
3154
+ } else {
3155
+ throw new Error("Unexpected TestFile object.");
3156
+ }
3157
+ }
3158
+ }
3159
+
3160
+ function stringify(name: string, value: unknown) {
3161
+ if (name.endsWith(".toml")) {
3162
+ return TOML.stringify(value as TOML.JsonMap);
3163
+ }
3164
+ if (name.endsWith(".json")) {
3165
+ return JSON.stringify(value);
3166
+ }
3167
+ return `${value}`;
3168
+ }
3169
+
3170
+ function parse(name: string, value: string): unknown {
3171
+ if (name.endsWith(".toml")) {
3172
+ return TOML.parse(value);
3173
+ }
3174
+ if (name.endsWith("tsconfig.json")) {
3175
+ return parseConfigFileTextToJson(name, value);
3176
+ }
3177
+ if (name.endsWith(".json")) {
3178
+ return JSON.parse(value);
3179
+ }
3180
+ return value;
3181
+ }
3182
+
3183
+ function wranglerToml(options: TOML.JsonMap = {}): TestFile {
3184
+ return {
3185
+ contents: options,
3186
+ };
3187
+ }
3188
+
3189
+ interface TestFile {
3190
+ contents: unknown;
3191
+ }
3192
+ interface TestFolder {
3193
+ items: {
3194
+ [id: string]: TestFile | TestFolder | boolean;
3195
+ };
3196
+ }