wrangler 0.0.0-e452a35 → 0.0.0-e464fcdb

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 (373) hide show
  1. package/README.md +24 -4
  2. package/bin/wrangler.js +170 -62
  3. package/kv-asset-handler.js +1 -0
  4. package/miniflare-dist/index.mjs +1637 -81
  5. package/package.json +198 -152
  6. package/src/__tests__/access.test.ts +25 -0
  7. package/src/__tests__/api-dev.test.ts +238 -0
  8. package/src/__tests__/api-devregistry.test.ts +121 -0
  9. package/src/__tests__/api.test.ts +102 -0
  10. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  11. package/src/__tests__/config-cache.test.ts +30 -24
  12. package/src/__tests__/configuration.test.ts +4498 -3321
  13. package/src/__tests__/constellation.test.ts +371 -0
  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 +257 -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 -874
  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 -236
  65. package/src/__tests__/init.test.ts +3167 -1528
  66. package/src/__tests__/jest.setup.ts +152 -50
  67. package/src/__tests__/kv.test.ts +1789 -1471
  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 +589 -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 -252
  84. package/src/__tests__/paths.test.ts +39 -0
  85. package/src/__tests__/publish.test.ts +8736 -5355
  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 -228
  89. package/src/__tests__/route.test.ts +18 -18
  90. package/src/__tests__/secret.test.ts +681 -469
  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 +1 -1
  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 +869 -161
  113. package/src/cfetch/index.ts +120 -83
  114. package/src/cfetch/internal.ts +214 -95
  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 +555 -357
  119. package/src/config/index.ts +374 -196
  120. package/src/config/validation-helpers.ts +379 -357
  121. package/src/config/validation.ts +2170 -1358
  122. package/src/config-cache.ts +81 -41
  123. package/src/constellation/createProject.tsx +51 -0
  124. package/src/constellation/deleteProject.ts +51 -0
  125. package/src/constellation/deleteProjectModel.ts +68 -0
  126. package/src/constellation/index.ts +75 -0
  127. package/src/constellation/listCatalog.tsx +35 -0
  128. package/src/constellation/listModel.tsx +41 -0
  129. package/src/constellation/listProject.tsx +28 -0
  130. package/src/constellation/listRuntime.tsx +28 -0
  131. package/src/constellation/options.ts +17 -0
  132. package/src/constellation/types.ts +17 -0
  133. package/src/constellation/uploadModel.tsx +64 -0
  134. package/src/constellation/utils.ts +90 -0
  135. package/src/create-worker-preview.ts +250 -142
  136. package/src/create-worker-upload-form.ts +343 -238
  137. package/src/d1/backups.tsx +219 -0
  138. package/src/d1/constants.ts +6 -0
  139. package/src/d1/create.tsx +103 -0
  140. package/src/d1/delete.ts +53 -0
  141. package/src/d1/execute.tsx +371 -0
  142. package/src/d1/formatTimeAgo.ts +14 -0
  143. package/src/d1/index.ts +100 -0
  144. package/src/d1/list.tsx +62 -0
  145. package/src/d1/migrations/apply.tsx +235 -0
  146. package/src/d1/migrations/create.tsx +79 -0
  147. package/src/d1/migrations/helpers.ts +171 -0
  148. package/src/d1/migrations/index.ts +3 -0
  149. package/src/d1/migrations/list.tsx +95 -0
  150. package/src/d1/migrations/options.ts +23 -0
  151. package/src/d1/options.ts +22 -0
  152. package/src/d1/splitter.ts +161 -0
  153. package/src/d1/types.ts +32 -0
  154. package/src/d1/utils.ts +49 -0
  155. package/src/delete.ts +100 -0
  156. package/src/deployments.ts +368 -0
  157. package/src/deprecated/index.ts +144 -0
  158. package/src/dev/dev-vars.ts +20 -17
  159. package/src/dev/dev.tsx +533 -355
  160. package/src/dev/get-local-persistence-path.ts +31 -0
  161. package/src/dev/local.tsx +928 -298
  162. package/src/dev/remote.tsx +620 -215
  163. package/src/dev/start-server.ts +534 -0
  164. package/src/dev/use-esbuild.ts +182 -97
  165. package/src/dev/validate-dev-props.ts +40 -0
  166. package/src/dev-registry.ts +202 -0
  167. package/src/dev.tsx +929 -0
  168. package/src/dialogs.ts +136 -0
  169. package/src/dispatch-namespace.ts +211 -0
  170. package/src/docs/helpers.ts +50 -0
  171. package/src/docs/index.ts +54 -0
  172. package/src/durable.ts +87 -87
  173. package/src/entry.ts +246 -231
  174. package/src/environment-variables/factory.ts +89 -0
  175. package/src/environment-variables/misc-variables.ts +30 -0
  176. package/src/errors.ts +6 -6
  177. package/src/generate/index.ts +298 -0
  178. package/src/git-client.ts +135 -0
  179. package/src/global-wrangler-config-path.ts +26 -0
  180. package/src/https-options.ts +84 -80
  181. package/src/index.ts +768 -0
  182. package/src/init.ts +1032 -0
  183. package/src/inspect.ts +713 -593
  184. package/src/intl-polyfill.d.ts +123 -123
  185. package/src/is-ci.ts +14 -0
  186. package/src/is-interactive.ts +16 -0
  187. package/src/jest.d.ts +4 -0
  188. package/src/kv/helpers.ts +433 -0
  189. package/src/kv/index.ts +594 -0
  190. package/src/logger.ts +100 -43
  191. package/src/metrics/index.ts +5 -0
  192. package/src/metrics/metrics-config.ts +239 -0
  193. package/src/metrics/metrics-dispatcher.ts +96 -0
  194. package/src/metrics/metrics-usage-headers.ts +24 -0
  195. package/src/metrics/send-event.ts +99 -0
  196. package/src/miniflare-cli/assets.ts +251 -0
  197. package/src/miniflare-cli/index.ts +202 -33
  198. package/src/miniflare-cli/request-context.ts +18 -18
  199. package/src/miniflare-cli/tsconfig.json +9 -0
  200. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  201. package/src/miniflare-cli/types.ts +11 -0
  202. package/src/module-collection.ts +290 -215
  203. package/src/mtls-certificate/cli.ts +155 -0
  204. package/src/open-in-browser.ts +4 -6
  205. package/src/package-manager.ts +182 -90
  206. package/src/pages/build.ts +410 -0
  207. package/src/pages/buildFunctions.ts +140 -0
  208. package/src/pages/constants.ts +18 -0
  209. package/src/pages/deployment-tails.ts +281 -0
  210. package/src/pages/deployments.tsx +84 -0
  211. package/src/pages/dev.ts +716 -0
  212. package/src/pages/errors.ts +67 -0
  213. package/src/pages/functions/buildPlugin.ts +113 -0
  214. package/src/pages/functions/buildWorker.ts +291 -0
  215. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  216. package/src/pages/functions/filepath-routing.ts +189 -0
  217. package/src/pages/functions/identifiers.ts +78 -0
  218. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  219. package/src/pages/functions/routes-consolidation.ts +73 -0
  220. package/src/pages/functions/routes-transformation.test.ts +282 -0
  221. package/src/pages/functions/routes-transformation.ts +115 -0
  222. package/src/pages/functions/routes-validation.test.ts +403 -0
  223. package/src/pages/functions/routes-validation.ts +202 -0
  224. package/src/pages/functions/routes.ts +151 -0
  225. package/src/pages/functions/tsconfig.json +8 -0
  226. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  227. package/src/pages/functions.ts +86 -0
  228. package/src/pages/hash.ts +13 -0
  229. package/src/pages/index.ts +102 -0
  230. package/src/pages/projects.tsx +159 -0
  231. package/src/pages/prompt-select-project.tsx +31 -0
  232. package/src/pages/publish.tsx +267 -0
  233. package/src/pages/types.ts +46 -0
  234. package/src/pages/upload.tsx +469 -0
  235. package/src/pages/utils.ts +23 -0
  236. package/src/parse.ts +232 -126
  237. package/src/paths.ts +53 -8
  238. package/src/proxy.ts +623 -394
  239. package/src/publish/index.ts +274 -0
  240. package/src/publish/publish.ts +1060 -0
  241. package/src/pubsub/index.ts +286 -0
  242. package/src/pubsub/pubsub-commands.ts +623 -0
  243. package/src/queues/cli/commands/consumer/add.ts +71 -0
  244. package/src/queues/cli/commands/consumer/index.ts +19 -0
  245. package/src/queues/cli/commands/consumer/remove.ts +31 -0
  246. package/src/queues/cli/commands/create.ts +25 -0
  247. package/src/queues/cli/commands/delete.ts +26 -0
  248. package/src/queues/cli/commands/index.ts +35 -0
  249. package/src/queues/cli/commands/list.ts +25 -0
  250. package/src/queues/client.ts +136 -0
  251. package/src/queues/utils.ts +18 -0
  252. package/src/r2/constants.ts +4 -0
  253. package/src/r2/helpers.ts +132 -0
  254. package/src/r2/index.ts +289 -0
  255. package/src/routes.ts +140 -0
  256. package/src/secret/index.ts +376 -0
  257. package/src/selfsigned.d.ts +23 -23
  258. package/src/sites.ts +484 -0
  259. package/src/tail/createTail.ts +406 -0
  260. package/src/tail/filters.ts +109 -111
  261. package/src/tail/index.ts +208 -296
  262. package/src/tail/printing.ts +101 -46
  263. package/src/traverse-module-graph.ts +53 -0
  264. package/src/tsconfig-sanity.ts +16 -0
  265. package/src/type-generation.ts +181 -0
  266. package/src/update-check.ts +11 -11
  267. package/src/user/access.ts +68 -0
  268. package/src/user/auth-variables.ts +113 -0
  269. package/src/user/choose-account.tsx +39 -0
  270. package/src/user/generate-auth-url.ts +33 -0
  271. package/src/user/generate-random-state.ts +16 -0
  272. package/src/user/index.ts +2 -0
  273. package/src/user/user.ts +1234 -0
  274. package/src/utils/collectKeyValues.ts +14 -0
  275. package/src/utils/render.ts +93 -0
  276. package/src/whoami.ts +135 -0
  277. package/src/worker.ts +200 -106
  278. package/src/yargs-types.ts +37 -0
  279. package/src/zones.ts +191 -0
  280. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  281. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  282. package/templates/__tests__/tsconfig.json +8 -0
  283. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  284. package/templates/checked-fetch.js +23 -0
  285. package/templates/d1-beta-facade.js +244 -0
  286. package/templates/facade.d.ts +14 -0
  287. package/templates/first-party-worker-module-facade.ts +19 -0
  288. package/templates/format-dev-errors.ts +34 -0
  289. package/templates/gitignore +1 -0
  290. package/templates/init-tests/test-jest-new-worker.js +23 -0
  291. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  292. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  293. package/templates/middleware/common.ts +62 -0
  294. package/templates/middleware/loader-modules.ts +84 -0
  295. package/templates/middleware/loader-sw.ts +233 -0
  296. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  297. package/templates/middleware/middleware-pretty-error.ts +40 -0
  298. package/templates/middleware/middleware-scheduled.ts +15 -0
  299. package/templates/new-worker-scheduled.js +17 -0
  300. package/templates/new-worker-scheduled.ts +32 -0
  301. package/templates/new-worker.js +3 -3
  302. package/templates/new-worker.ts +18 -15
  303. package/templates/no-op-worker.js +10 -0
  304. package/templates/pages-dev-pipeline.ts +31 -0
  305. package/templates/pages-dev-util.ts +52 -0
  306. package/templates/pages-shim.ts +12 -0
  307. package/templates/pages-template-plugin.ts +180 -0
  308. package/templates/pages-template-worker.ts +189 -0
  309. package/templates/serve-static-assets.ts +66 -0
  310. package/templates/service-bindings-module-facade.js +55 -0
  311. package/templates/service-bindings-sw-facade.js +42 -0
  312. package/templates/tsconfig-sanity.ts +11 -0
  313. package/templates/tsconfig.init.json +105 -0
  314. package/templates/tsconfig.json +6 -103
  315. package/templates/tsconfig.tsbuildinfo +1 -0
  316. package/wrangler-dist/cli.d.ts +1641 -0
  317. package/wrangler-dist/cli.js +139681 -112322
  318. package/wrangler-dist/wasm-sync.wasm +0 -0
  319. package/pages/functions/buildPlugin.ts +0 -105
  320. package/pages/functions/buildWorker.ts +0 -151
  321. package/pages/functions/filepath-routing.ts +0 -189
  322. package/pages/functions/identifiers.ts +0 -78
  323. package/pages/functions/routes.ts +0 -156
  324. package/pages/functions/template-plugin.ts +0 -147
  325. package/pages/functions/template-worker.ts +0 -143
  326. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  327. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  328. package/src/__tests__/pages.test.ts +0 -657
  329. package/src/dialogs.tsx +0 -87
  330. package/src/environment-variables.ts +0 -35
  331. package/src/index.tsx +0 -2789
  332. package/src/kv.ts +0 -408
  333. package/src/miniflare-cli/enum-keys.ts +0 -17
  334. package/src/pages.tsx +0 -2027
  335. package/src/publish.ts +0 -583
  336. package/src/r2.ts +0 -50
  337. package/src/sites.tsx +0 -295
  338. package/src/user.tsx +0 -1203
  339. package/src/whoami.tsx +0 -70
  340. package/templates/static-asset-facade.js +0 -47
  341. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  342. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  343. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  344. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  345. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  346. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  347. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  348. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  349. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  350. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  351. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  352. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  353. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  354. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  355. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  356. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  357. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  358. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  359. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  360. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  361. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  362. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  363. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  364. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  365. package/vendor/wrangler-mime/LICENSE +0 -21
  366. package/vendor/wrangler-mime/Mime.js +0 -97
  367. package/vendor/wrangler-mime/README.md +0 -187
  368. package/vendor/wrangler-mime/cli.js +0 -46
  369. package/vendor/wrangler-mime/index.js +0 -4
  370. package/vendor/wrangler-mime/lite.js +0 -4
  371. package/vendor/wrangler-mime/package.json +0 -52
  372. package/vendor/wrangler-mime/types/other.js +0 -1
  373. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,142 +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
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
50
- ✨ Created src/index.ts
51
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
52
-
53
- To start developing your Worker, run \`npm start\`
54
- To publish your Worker to the Internet, run \`npm run deploy\`"
55
- `);
56
- expect(std.err).toMatchInlineSnapshot(`""`);
57
- expect(std.warn).toMatchInlineSnapshot(`""`);
58
- });
59
-
60
- it("should initialize with no interactive prompts if `--yes` is used (named worker)", async () => {
61
- await runWrangler("init my-worker --yes");
62
-
63
- expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
64
- expect(fs.existsSync("./my-worker/src/index.ts")).toBe(true);
65
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(true);
66
- expect(fs.existsSync("./my-worker/package.json")).toBe(true);
67
- expect(fs.existsSync("./my-worker/wrangler.toml")).toBe(true);
68
- const parsedWranglerToml = TOML.parse(
69
- fs.readFileSync("./my-worker/wrangler.toml", "utf-8")
70
- );
71
- expect(parsedWranglerToml.main).toEqual("src/index.ts");
72
- expect(std.out).toMatchInlineSnapshot(`
73
- "✨ Created my-worker/wrangler.toml
74
- ✨ Initialized git repository at my-worker
75
- ✨ Created my-worker/package.json
76
- ✨ Created my-worker/tsconfig.json
77
- ✨ Created my-worker/src/index.ts
78
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
79
-
80
- To start developing your Worker, run \`cd my-worker && npm start\`
81
- To publish your Worker to the Internet, run \`npm run deploy\`"
82
- `);
83
- expect(std.err).toMatchInlineSnapshot(`""`);
84
- expect(std.warn).toMatchInlineSnapshot(`""`);
85
- });
86
-
87
- it("should initialize with no interactive prompts if `-y` is used", async () => {
88
- await runWrangler("init -y");
89
-
90
- expect(fs.existsSync("./src/index.js")).toBe(false);
91
- expect(fs.existsSync("./src/index.ts")).toBe(true);
92
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
93
- expect(fs.existsSync("./package.json")).toBe(true);
94
- expect(fs.existsSync("./wrangler.toml")).toBe(true);
95
- expect(std).toMatchInlineSnapshot(`
96
- Object {
97
- "debug": "",
98
- "err": "",
99
- "out": "✨ Created wrangler.toml
100
- ✨ Initialized git repository
101
- ✨ Created package.json
102
- ✨ Created tsconfig.json
103
- ✨ Created src/index.ts
104
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
105
-
106
- To start developing your Worker, run \`npm start\`
107
- To publish your Worker to the Internet, run \`npm run deploy\`",
108
- "warn": "",
109
- }
110
- `);
111
- });
112
-
113
- it("should error if `--type javascript` is used", async () => {
114
- await expect(
115
- runWrangler("init --type javascript")
116
- ).rejects.toThrowErrorMatchingInlineSnapshot(
117
- `"The --type option is no longer supported."`
118
- );
119
- });
120
-
121
- it("should error if `--type rust` is used", async () => {
122
- await expect(
123
- runWrangler("init --type rust")
124
- ).rejects.toThrowErrorMatchingInlineSnapshot(
125
- `"The --type option is no longer supported."`
126
- );
127
- });
128
-
129
- it("should error if `--type webpack` is used", async () => {
130
- await expect(runWrangler("init --type webpack")).rejects
131
- .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(`
132
168
  "The --type option is no longer supported.
133
169
  If you wish to use webpack then you will need to create a custom build."
134
170
  `);
135
- });
171
+ });
136
172
 
137
- it("should error if `--site` is used", async () => {
138
- await expect(runWrangler("init --site")).rejects
139
- .toThrowErrorMatchingInlineSnapshot(`
173
+ it("should error if `--site` is used", async () => {
174
+ await expect(runWrangler("init --site")).rejects
175
+ .toThrowErrorMatchingInlineSnapshot(`
140
176
  "The --site option is no longer supported.
141
177
  If you wish to create a brand new Worker Sites project then clone the \`worker-sites-template\` starter repository:
142
178
 
@@ -148,1410 +184,3013 @@ describe("init", () => {
148
184
  Find out more about how to create and maintain Sites projects at https://developers.cloudflare.com/workers/platform/sites.
149
185
  Have you considered using Cloudflare Pages instead? See https://pages.cloudflare.com/."
150
186
  `);
151
- });
152
- });
153
-
154
- describe("wrangler.toml", () => {
155
- it("should create a wrangler.toml", async () => {
156
- mockConfirm(
157
- {
158
- text: "Would you like to use git to manage this Worker?",
159
- result: false,
160
- },
161
- {
162
- text: "No package.json found. Would you like to create one?",
163
- result: false,
164
- }
165
- );
166
- await runWrangler("init");
167
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
168
- expect(typeof parsed.compatibility_date).toBe("string");
169
- expect(parsed.name).toContain("wrangler-tests");
170
- expect(fs.existsSync("./package.json")).toBe(false);
171
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
172
- expect(std).toMatchInlineSnapshot(`
173
- Object {
174
- "debug": "",
175
- "err": "",
176
- "out": "✨ Created wrangler.toml",
177
- "warn": "",
178
- }
179
- `);
180
- });
181
-
182
- it("should create a wrangler.toml and a directory for a named Worker ", async () => {
183
- mockConfirm(
184
- {
185
- text: "Would you like to use git to manage this Worker?",
186
- result: false,
187
- },
188
- {
189
- text: "No package.json found. Would you like to create one?",
190
- result: false,
191
- }
192
- );
193
- await runWrangler("init my-worker");
194
- const parsed = TOML.parse(
195
- await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
196
- );
197
- expect(typeof parsed.compatibility_date).toBe("string");
198
- expect(parsed.name).toBe("my-worker");
199
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
200
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
201
-
202
- expect(std).toMatchInlineSnapshot(`
203
- Object {
204
- "debug": "",
205
- "err": "",
206
- "out": "✨ Created my-worker/wrangler.toml",
207
- "warn": "",
208
- }
209
- `);
210
- });
211
-
212
- it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
213
- fs.writeFileSync(
214
- "./wrangler.toml",
215
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
216
- "utf-8"
217
- );
218
- mockConfirm({
219
- text: "Do you want to continue initializing this project?",
220
- result: false,
221
- });
222
- await runWrangler("init");
223
- expect(std.warn).toContain("wrangler.toml already exists!");
224
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
225
- expect(parsed.compatibility_date).toBe("something-else");
226
-
227
- expect(std).toMatchInlineSnapshot(`
228
- Object {
229
- "debug": "",
230
- "err": "",
231
- "out": "",
232
- "warn": "▲ [WARNING] wrangler.toml already exists!
233
-
234
- ",
235
- }
236
- `);
237
- });
238
-
239
- it("should display warning when wrangler.toml already exists in the target directory, and exit if user does not want to carry on", async () => {
240
- fs.mkdirSync("path/to/worker", { recursive: true });
241
- fs.writeFileSync(
242
- "path/to/worker/wrangler.toml",
243
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
244
- "utf-8"
245
- );
246
- mockConfirm({
247
- text: "Do you want to continue initializing this project?",
248
- result: false,
249
- });
250
- await runWrangler("init path/to/worker");
251
- expect(std.warn).toContain("wrangler.toml already exists!");
252
- const parsed = TOML.parse(
253
- await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
254
- );
255
- expect(parsed.compatibility_date).toBe("something-else");
256
- expect(std).toMatchInlineSnapshot(`
257
- Object {
258
- "debug": "",
259
- "err": "",
260
- "out": "",
261
- "warn": "▲ [WARNING] path/to/worker/wrangler.toml already exists!
262
-
263
- ",
264
- }
265
- `);
266
- });
267
-
268
- it("should not overwrite an existing wrangler.toml, after agreeing to other prompts", async () => {
269
- fs.writeFileSync(
270
- "./wrangler.toml",
271
- 'compatibility_date="something-else"', // use a fake value to make sure the file is not overwritten
272
- "utf-8"
273
- );
274
- mockConfirm(
275
- {
276
- text: "Would you like to use git to manage this Worker?",
277
- result: true,
278
- },
279
- {
280
- text: "Do you want to continue initializing this project?",
281
- result: true,
282
- },
283
- {
284
- text: "No package.json found. Would you like to create one?",
285
- result: true,
286
- },
287
- {
288
- text: "Would you like to use TypeScript?",
289
- result: true,
290
- },
291
- {
292
- text: "Would you like to create a Worker at src/index.ts?",
293
- result: true,
294
- }
295
- );
296
-
297
- await runWrangler("init");
298
- expect(fs.readFileSync("./wrangler.toml", "utf-8")).toMatchInlineSnapshot(
299
- `"compatibility_date=\\"something-else\\""`
300
- );
301
- });
302
-
303
- it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
304
- fs.writeFileSync(
305
- "./wrangler.toml",
306
- `compatibility_date="something-else"`,
307
- "utf-8"
308
- );
309
- mockConfirm(
310
- {
311
- text: "Do you want to continue initializing this project?",
312
- result: true,
313
- },
314
- {
315
- text: "Would you like to use git to manage this Worker?",
316
- result: false,
317
- },
318
- {
319
- text: "No package.json found. Would you like to create one?",
320
- result: false,
321
- }
322
- );
323
- await runWrangler("init");
324
- expect(std.warn).toContain("wrangler.toml already exists!");
325
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
326
- expect(parsed.compatibility_date).toBe("something-else");
327
- expect(std).toMatchInlineSnapshot(`
328
- Object {
329
- "debug": "",
330
- "err": "",
331
- "out": "",
332
- "warn": "▲ [WARNING] wrangler.toml already exists!
333
-
334
- ",
335
- }
336
- `);
337
- });
338
- });
339
-
340
- describe("git init", () => {
341
- it("should offer to initialize a git repository", async () => {
342
- mockConfirm(
343
- {
344
- text: "Would you like to use git to manage this Worker?",
345
- result: true,
346
- },
347
- {
348
- text: "No package.json found. Would you like to create one?",
349
- result: false,
350
- }
351
- );
352
- await runWrangler("init");
353
- expect(std).toMatchInlineSnapshot(`
354
- Object {
355
- "debug": "",
356
- "err": "",
357
- "out": "✨ Created wrangler.toml
358
- ✨ Initialized git repository",
359
- "warn": "",
360
- }
361
- `);
362
- expect(fs.lstatSync(".git").isDirectory()).toBe(true);
363
- expect(fs.lstatSync(".gitignore").isFile()).toBe(true);
364
- });
365
-
366
- it("should not offer to initialize a git repo if it's already inside one", async () => {
367
- await execa("git", ["init"]);
368
- fs.mkdirSync("some-folder");
369
- process.chdir("some-folder");
370
- await runWrangler("init -y");
371
-
372
- // Note the lack of "✨ Initialized git repository" in the log
373
- expect(std).toMatchInlineSnapshot(`
374
- Object {
375
- "debug": "",
376
- "err": "",
377
- "out": "✨ Created wrangler.toml
378
- ✨ Created package.json
379
- ✨ Created tsconfig.json
380
- ✨ Created src/index.ts
381
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
382
-
383
- To start developing your Worker, run \`npm start\`
384
- To publish your Worker to the Internet, run \`npm run deploy\`",
385
- "warn": "",
386
- }
387
- `);
388
- });
389
-
390
- it("should not offer to initialize a git repo if it's already inside one (when using a path as name)", async () => {
391
- fs.mkdirSync("path/to/worker", { recursive: true });
392
- await execa("git", ["init"], { cwd: "path/to/worker" });
393
- expect(fs.lstatSync("path/to/worker/.git").isDirectory()).toBe(true);
394
-
395
- await runWrangler("init path/to/worker/my-worker -y");
396
-
397
- // Note the lack of "✨ Initialized git repository" in the log
398
- expect(std).toMatchInlineSnapshot(`
399
- Object {
400
- "debug": "",
401
- "err": "",
402
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
403
- ✨ Created path/to/worker/my-worker/package.json
404
- ✨ Created path/to/worker/my-worker/tsconfig.json
405
- ✨ Created path/to/worker/my-worker/src/index.ts
406
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
407
-
408
- To start developing your Worker, run \`cd path/to/worker/my-worker && npm start\`
409
- To publish your Worker to the Internet, run \`npm run deploy\`",
410
- "warn": "",
411
- }
412
- `);
413
- });
414
-
415
- // I... don't know how to test this lol
416
- it.todo(
417
- "should not offer to initialize a git repo if git is not installed"
418
- );
419
-
420
- it("should initialize git repo with `main` default branch", async () => {
421
- mockConfirm(
422
- {
423
- text: "Would you like to use git to manage this Worker?",
424
- result: true,
425
- },
426
- {
427
- text: "No package.json found. Would you like to create one?",
428
- result: false,
429
- }
430
- );
431
- await runWrangler("init");
432
- expect(std).toMatchInlineSnapshot(`
433
- Object {
434
- "debug": "",
435
- "err": "",
436
- "out": "✨ Created wrangler.toml
437
- ✨ Initialized git repository",
438
- "warn": "",
439
- }
440
- `);
441
-
442
- expect(execaSync("git", ["symbolic-ref", "HEAD"]).stdout).toEqual(
443
- "refs/heads/main"
444
- );
445
- });
446
- });
447
-
448
- describe("package.json", () => {
449
- it("should create a package.json if none is found and user confirms", async () => {
450
- mockConfirm(
451
- {
452
- text: "Would you like to use git to manage this Worker?",
453
- result: false,
454
- },
455
- {
456
- text: "No package.json found. Would you like to create one?",
457
- result: true,
458
- },
459
- {
460
- text: "Would you like to use TypeScript?",
461
- result: false,
462
- },
463
- {
464
- text: "Would you like to create a Worker at src/index.js?",
465
- result: false,
466
- }
467
- );
468
- await runWrangler("init");
469
- expect(fs.existsSync("./package.json")).toBe(true);
470
- const packageJson = JSON.parse(
471
- fs.readFileSync("./package.json", "utf-8")
472
- );
473
- expect(packageJson.name).toContain("wrangler-tests");
474
- expect(packageJson.version).toEqual("0.0.0");
475
- expect(packageJson.devDependencies).toEqual({
476
- wrangler: expect.any(String),
477
- });
478
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
479
- expect(mockPackageManager.install).toHaveBeenCalled();
480
- expect(std).toMatchInlineSnapshot(`
481
- Object {
482
- "debug": "",
483
- "err": "",
484
- "out": "✨ Created wrangler.toml
485
- ✨ Created package.json",
486
- "warn": "",
487
- }
488
- `);
489
- });
490
-
491
- it("should create a package.json, with the specified name, if none is found and user confirms", async () => {
492
- mockConfirm(
493
- {
494
- text: "Would you like to use git to manage this Worker?",
495
- result: false,
496
- },
497
- {
498
- text: "No package.json found. Would you like to create one?",
499
- result: true,
500
- },
501
- {
502
- text: "Would you like to use TypeScript?",
503
- result: false,
504
- },
505
- {
506
- text: "Would you like to create a Worker at my-worker/src/index.js?",
507
- result: false,
508
- }
509
- );
510
- await runWrangler("init my-worker");
511
- const packageJson = JSON.parse(
512
- fs.readFileSync("./my-worker/package.json", "utf-8")
513
- );
514
- expect(packageJson.name).toBe("my-worker");
515
- expect(std).toMatchInlineSnapshot(`
516
- Object {
517
- "debug": "",
518
- "err": "",
519
- "out": "✨ Created my-worker/wrangler.toml
520
- ✨ Created my-worker/package.json",
521
- "warn": "",
522
- }
523
- `);
524
- });
525
-
526
- it("should not touch an existing package.json in the same directory", async () => {
527
- mockConfirm(
528
- {
529
- text: "Would you like to use git to manage this Worker?",
530
- result: false,
531
- },
532
- {
533
- text: "Would you like to install wrangler into package.json?",
534
- result: false,
535
- },
536
- {
537
- text: "Would you like to use TypeScript?",
538
- result: false,
539
- },
540
- {
541
- text: "Would you like to create a Worker at src/index.js?",
542
- result: false,
543
- }
544
- );
545
-
546
- fs.writeFileSync(
547
- "./package.json",
548
- JSON.stringify({ name: "test", version: "1.0.0" }),
549
- "utf-8"
550
- );
551
-
552
- await runWrangler("init");
553
- const packageJson = JSON.parse(
554
- fs.readFileSync("./package.json", "utf-8")
555
- );
556
- expect(packageJson.name).toEqual("test");
557
- expect(packageJson.version).toEqual("1.0.0");
558
- expect(std).toMatchInlineSnapshot(`
559
- Object {
560
- "debug": "",
561
- "err": "",
562
- "out": "✨ Created wrangler.toml",
563
- "warn": "",
564
- }
565
- `);
566
- });
567
-
568
- it("should not touch an existing package.json in a target directory", async () => {
569
- mockConfirm(
570
- {
571
- text: "Would you like to use git to manage this Worker?",
572
- result: false,
573
- },
574
- {
575
- text: "Would you like to install wrangler into path/to/worker/package.json?",
576
- result: false,
577
- },
578
- {
579
- text: "Would you like to use TypeScript?",
580
- result: false,
581
- },
582
- {
583
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
584
- result: false,
585
- }
586
- );
587
-
588
- fs.mkdirSync("path/to/worker", { recursive: true });
589
- fs.writeFileSync(
590
- "path/to/worker/package.json",
591
- JSON.stringify({ name: "test", version: "1.0.0" }),
592
- "utf-8"
593
- );
594
-
595
- await runWrangler("init path/to/worker/my-worker");
596
- const packageJson = JSON.parse(
597
- fs.readFileSync("path/to/worker/package.json", "utf-8")
598
- );
599
- expect(packageJson.name).toEqual("test");
600
- expect(packageJson.version).toEqual("1.0.0");
601
- expect(std).toMatchInlineSnapshot(`
602
- Object {
603
- "debug": "",
604
- "err": "",
605
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml",
606
- "warn": "",
607
- }
608
- `);
609
- });
610
-
611
- it("should offer to install wrangler into an existing package.json", async () => {
612
- mockConfirm(
613
- {
614
- text: "Would you like to use git to manage this Worker?",
615
- result: false,
616
- },
617
- {
618
- text: "Would you like to install wrangler into package.json?",
619
- result: true,
620
- },
621
- {
622
- text: "Would you like to use TypeScript?",
623
- result: false,
624
- },
625
- {
626
- text: "Would you like to create a Worker at src/index.js?",
627
- result: false,
628
- }
629
- );
630
-
631
- fs.writeFileSync(
632
- "./package.json",
633
- JSON.stringify({ name: "test", version: "1.0.0" }),
634
- "utf-8"
635
- );
636
-
637
- await runWrangler("init");
638
- const packageJson = JSON.parse(
639
- fs.readFileSync("./package.json", "utf-8")
640
- );
641
- expect(packageJson.name).toEqual("test");
642
- expect(packageJson.version).toEqual("1.0.0");
643
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
644
- `wrangler@${wranglerVersion}`
645
- );
646
- expect(std).toMatchInlineSnapshot(`
647
- Object {
648
- "debug": "",
649
- "err": "",
650
- "out": "✨ Created wrangler.toml
651
- ✨ Installed wrangler into devDependencies",
652
- "warn": "",
653
- }
654
- `);
655
- });
656
-
657
- it("should offer to install wrangler into a package.json relative to the target directory", async () => {
658
- mockConfirm(
659
- {
660
- text: "Would you like to use git to manage this Worker?",
661
- result: false,
662
- },
663
- {
664
- text: "Would you like to install wrangler into path/to/worker/package.json?",
665
- result: true,
666
- },
667
- {
668
- text: "Would you like to use TypeScript?",
669
- result: false,
670
- },
671
- {
672
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.js?",
673
- result: false,
674
- }
675
- );
676
-
677
- fs.mkdirSync("path/to/worker", { recursive: true });
678
- fs.writeFileSync(
679
- "path/to/worker/package.json",
680
- JSON.stringify({ name: "test", version: "1.0.0" }),
681
- "utf-8"
682
- );
683
-
684
- await runWrangler("init path/to/worker/my-worker");
685
- const packageJson = JSON.parse(
686
- fs.readFileSync("path/to/worker/package.json", "utf-8")
687
- );
688
- expect(packageJson.name).toEqual("test");
689
- expect(packageJson.version).toEqual("1.0.0");
690
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
691
- `wrangler@${wranglerVersion}`
692
- );
693
- expect(mockPackageManager.cwd).toBe(process.cwd());
694
- expect(std).toMatchInlineSnapshot(`
695
- Object {
696
- "debug": "",
697
- "err": "",
698
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
699
- ✨ Installed wrangler into devDependencies",
700
- "warn": "",
701
- }
702
- `);
703
- });
704
-
705
- it("should not touch an existing package.json in an ancestor directory", async () => {
706
- mockConfirm(
707
- {
708
- text: "Would you like to use git to manage this Worker?",
709
- result: false,
710
- },
711
- {
712
- text: "Would you like to install wrangler into ../../package.json?",
713
- result: false,
714
- },
715
- {
716
- text: "Would you like to use TypeScript?",
717
- result: false,
718
- },
719
- {
720
- text: "Would you like to create a Worker at src/index.js?",
721
- result: false,
722
- }
723
- );
724
-
725
- fs.writeFileSync(
726
- "./package.json",
727
- JSON.stringify({ name: "test", version: "1.0.0" }),
728
- "utf-8"
729
- );
730
-
731
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
732
- process.chdir("./sub-1/sub-2");
733
-
734
- await runWrangler("init");
735
- expect(fs.existsSync("./package.json")).toBe(false);
736
- expect(fs.existsSync("../../package.json")).toBe(true);
737
-
738
- const packageJson = JSON.parse(
739
- fs.readFileSync("../../package.json", "utf-8")
740
- );
741
- expect(packageJson).toMatchInlineSnapshot(`
742
- Object {
743
- "name": "test",
744
- "version": "1.0.0",
745
- }
746
- `);
747
- expect(std).toMatchInlineSnapshot(`
748
- Object {
749
- "debug": "",
750
- "err": "",
751
- "out": "✨ Created wrangler.toml",
752
- "warn": "",
753
- }
754
- `);
755
- });
756
- });
757
-
758
- describe("typescript", () => {
759
- it("should offer to create a worker in a non-typescript project", async () => {
760
- mockConfirm(
761
- {
762
- text: "Would you like to use git to manage this Worker?",
763
- result: false,
764
- },
765
- {
766
- text: "Would you like to install wrangler into package.json?",
767
- result: false,
768
- },
769
- {
770
- text: "Would you like to use TypeScript?",
771
- result: false,
772
- },
773
- {
774
- text: "Would you like to create a Worker at src/index.js?",
775
- result: true,
776
- }
777
- );
778
-
779
- fs.writeFileSync(
780
- "./package.json",
781
- JSON.stringify({ name: "test", version: "1.0.0" }),
782
- "utf-8"
783
- );
784
-
785
- await runWrangler("init");
786
- expect(fs.existsSync("./src/index.js")).toBe(true);
787
- expect(fs.existsSync("./src/index.ts")).toBe(false);
788
- expect(std).toMatchInlineSnapshot(`
789
- Object {
790
- "debug": "",
791
- "err": "",
792
- "out": "✨ Created wrangler.toml
793
- ✨ Created src/index.js
794
-
795
- To start developing your Worker, run \`npx wrangler dev\`
796
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
797
- "warn": "",
798
- }
799
- `);
800
- });
801
-
802
- it("should offer to create a worker in a typescript project", async () => {
803
- mockConfirm(
804
- {
805
- text: "Would you like to use git to manage this Worker?",
806
- result: false,
807
- },
808
- {
809
- text: "Would you like to install wrangler into package.json?",
810
- result: false,
811
- },
812
- {
813
- text: "Would you like to use TypeScript?",
814
- result: true,
815
- },
816
- {
817
- text: "Would you like to create a Worker at src/index.ts?",
818
- result: true,
819
- }
820
- );
821
-
822
- fs.writeFileSync(
823
- "./package.json",
824
- JSON.stringify({ name: "test", version: "1.0.0" }),
825
- "utf-8"
826
- );
827
-
828
- await runWrangler("init");
829
- expect(fs.existsSync("./src/index.js")).toBe(false);
830
- expect(fs.existsSync("./src/index.ts")).toBe(true);
831
- expect(std).toMatchInlineSnapshot(`
832
- Object {
833
- "debug": "",
834
- "err": "",
835
- "out": "✨ Created wrangler.toml
836
- ✨ Created tsconfig.json
837
- ✨ Created src/index.ts
838
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
839
-
840
- To start developing your Worker, run \`npx wrangler dev\`
841
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
842
- "warn": "",
843
- }
844
- `);
845
- });
846
-
847
- it("should add scripts for a typescript project with .ts extension", async () => {
848
- mockConfirm(
849
- {
850
- text: "Would you like to use git to manage this Worker?",
851
- result: false,
852
- },
853
- {
854
- text: "No package.json found. Would you like to create one?",
855
- result: true,
856
- },
857
- {
858
- text: "Would you like to install wrangler into package.json?",
859
- result: false,
860
- },
861
- {
862
- text: "Would you like to use TypeScript?",
863
- result: true,
864
- },
865
- {
866
- text: "Would you like to create a Worker at src/index.ts?",
867
- result: true,
868
- }
869
- );
870
- await runWrangler("init");
871
-
872
- expect(fs.existsSync("./package.json")).toBe(true);
873
- const packageJson = JSON.parse(
874
- fs.readFileSync("./package.json", "utf-8")
875
- );
876
-
877
- expect(fs.existsSync("./src/index.js")).toBe(false);
878
- expect(fs.existsSync("./src/index.ts")).toBe(true);
879
-
880
- expect(packageJson.scripts.start).toBe("wrangler dev");
881
- expect(packageJson.scripts.deploy).toBe("wrangler publish");
882
- expect(packageJson.name).toContain("wrangler-tests");
883
- expect(packageJson.version).toEqual("0.0.0");
884
- expect(std.out).toMatchInlineSnapshot(`
885
- "✨ Created wrangler.toml
886
- ✨ Created package.json
887
- ✨ Created tsconfig.json
888
- ✨ Created src/index.ts
889
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
890
-
891
- To start developing your Worker, run \`npm start\`
892
- To publish your Worker to the Internet, run \`npm run deploy\`"
893
- `);
894
- });
895
-
896
- it("should not overwrite package.json scripts for a typescript project", async () => {
897
- mockConfirm(
898
- {
899
- text: "Would you like to use git to manage this Worker?",
900
- result: false,
901
- },
902
- {
903
- text: "Would you like to install wrangler into package.json?",
904
- result: false,
905
- },
906
- {
907
- text: "Would you like to use TypeScript?",
908
- result: true,
909
- },
910
- {
911
- text: "Would you like to create a Worker at src/index.ts?",
912
- result: true,
913
- }
914
- );
915
- await fsp.writeFile(
916
- "./package.json",
917
- JSON.stringify({
918
- scripts: {
919
- start: "test-start",
920
- deploy: "test-publish",
921
- },
922
- })
923
- );
924
- const packageJson = JSON.parse(
925
- fs.readFileSync("./package.json", "utf-8")
926
- );
927
- await runWrangler("init");
928
-
929
- expect(fs.existsSync("./src/index.js")).toBe(false);
930
- expect(fs.existsSync("./src/index.ts")).toBe(true);
931
-
932
- expect(packageJson.scripts.start).toBe("test-start");
933
- expect(packageJson.scripts.deploy).toBe("test-publish");
934
- expect(std.out).toMatchInlineSnapshot(`
935
- "✨ Created wrangler.toml
936
- ✨ Created tsconfig.json
937
- ✨ Created src/index.ts
938
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
939
-
940
- To start developing your Worker, run \`npx wrangler dev\`
941
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
942
- `);
943
- });
944
-
945
- it("should not offer to create a worker in a ts project if a file already exists at the location", async () => {
946
- mockConfirm(
947
- {
948
- text: "Would you like to use git to manage this Worker?",
949
- result: false,
950
- },
951
- {
952
- text: "Would you like to install wrangler into package.json?",
953
- result: false,
954
- },
955
- {
956
- text: "Would you like to use TypeScript?",
957
- result: true,
958
- }
959
- );
960
-
961
- fs.writeFileSync(
962
- "./package.json",
963
- JSON.stringify({ name: "test", version: "1.0.0" }),
964
- "utf-8"
965
- );
966
- fs.mkdirSync("./src", { recursive: true });
967
- const PLACEHOLDER = "/* placeholder text */";
968
- fs.writeFileSync("./src/index.ts", PLACEHOLDER, "utf-8");
969
-
970
- await runWrangler("init");
971
- expect(fs.existsSync("./src/index.js")).toBe(false);
972
- expect(fs.readFileSync("./src/index.ts", "utf-8")).toBe(PLACEHOLDER);
973
- expect(std).toMatchInlineSnapshot(`
974
- Object {
975
- "debug": "",
976
- "err": "",
977
- "out": "✨ Created wrangler.toml
978
- ✨ Created tsconfig.json
979
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
980
- "warn": "",
981
- }
982
- `);
983
- });
984
-
985
- 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 () => {
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 package.json?",
993
- result: false,
994
- },
995
- {
996
- text: "Would you like to use TypeScript?",
997
- result: true,
998
- }
999
- );
1000
-
1001
- fs.writeFileSync(
1002
- "./package.json",
1003
- JSON.stringify({ name: "test", version: "1.0.0" }),
1004
- "utf-8"
1005
- );
1006
- fs.mkdirSync("./my-worker/src", { recursive: true });
1007
- const PLACEHOLDER = "/* placeholder text */";
1008
- fs.writeFileSync("./my-worker/src/index.ts", PLACEHOLDER, "utf-8");
1009
-
1010
- await runWrangler("init my-worker");
1011
- expect(fs.existsSync("./my-worker/src/index.js")).toBe(false);
1012
- expect(fs.readFileSync("./my-worker/src/index.ts", "utf-8")).toBe(
1013
- PLACEHOLDER
1014
- );
1015
- expect(std).toMatchInlineSnapshot(`
1016
- Object {
1017
- "debug": "",
1018
- "err": "",
1019
- "out": "✨ Created my-worker/wrangler.toml
1020
- ✨ Created my-worker/tsconfig.json
1021
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1022
- "warn": "",
1023
- }
1024
- `);
1025
- });
1026
-
1027
- it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
1028
- mockConfirm(
1029
- {
1030
- text: "Would you like to use git to manage this Worker?",
1031
- result: false,
1032
- },
1033
- {
1034
- text: "No package.json found. Would you like to create one?",
1035
- result: true,
1036
- },
1037
- {
1038
- text: "Would you like to use TypeScript?",
1039
- result: true,
1040
- },
1041
- {
1042
- text: "Would you like to create a Worker at src/index.ts?",
1043
- result: false,
1044
- }
1045
- );
1046
- await runWrangler("init");
1047
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
1048
- const { config: tsconfigJson, error: tsConfigParseError } =
1049
- parseConfigFileTextToJson(
1050
- "./tsconfig.json",
1051
- fs.readFileSync("./tsconfig.json", "utf-8")
1052
- );
1053
- expect(tsConfigParseError).toBeUndefined();
1054
- expect(tsconfigJson.compilerOptions.types).toEqual([
1055
- "@cloudflare/workers-types",
1056
- ]);
1057
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1058
- "@cloudflare/workers-types",
1059
- "typescript"
1060
- );
1061
- expect(std).toMatchInlineSnapshot(`
1062
- Object {
1063
- "debug": "",
1064
- "err": "",
1065
- "out": "✨ Created wrangler.toml
1066
- ✨ Created package.json
1067
- ✨ Created tsconfig.json
1068
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies",
1069
- "warn": "",
1070
- }
1071
- `);
1072
- });
1073
-
1074
- it("should not touch an existing tsconfig.json in the same directory", async () => {
1075
- fs.writeFileSync(
1076
- "./package.json",
1077
- JSON.stringify({
1078
- name: "test",
1079
- version: "1.0.0",
1080
- devDependencies: {
1081
- wrangler: "0.0.0",
1082
- "@cloudflare/workers-types": "0.0.0",
1083
- },
1084
- }),
1085
- "utf-8"
1086
- );
1087
- fs.writeFileSync(
1088
- "./tsconfig.json",
1089
- JSON.stringify({ compilerOptions: {} }),
1090
- "utf-8"
1091
- );
1092
-
1093
- mockConfirm(
1094
- {
1095
- text: "Would you like to use git to manage this Worker?",
1096
- result: false,
1097
- },
1098
- {
1099
- text: "Would you like to create a Worker at src/index.ts?",
1100
- result: true,
1101
- }
1102
- );
1103
-
1104
- await runWrangler("init");
1105
- const tsconfigJson = JSON.parse(
1106
- fs.readFileSync("./tsconfig.json", "utf-8")
1107
- );
1108
- expect(tsconfigJson.compilerOptions).toEqual({});
1109
- expect(std).toMatchInlineSnapshot(`
1110
- Object {
1111
- "debug": "",
1112
- "err": "",
1113
- "out": "✨ Created wrangler.toml
1114
- ✨ Created src/index.ts
1115
-
1116
- To start developing your Worker, run \`npx wrangler dev\`
1117
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1118
- "warn": "",
1119
- }
1120
- `);
1121
- });
1122
-
1123
- it("should not touch an existing tsconfig.json in the ancestor of a target directory", async () => {
1124
- fs.mkdirSync("path/to/worker", { recursive: true });
1125
- fs.writeFileSync(
1126
- "path/to/worker/package.json",
1127
- JSON.stringify({
1128
- name: "test",
1129
- version: "1.0.0",
1130
- devDependencies: {
1131
- wrangler: "0.0.0",
1132
- "@cloudflare/workers-types": "0.0.0",
1133
- },
1134
- }),
1135
- "utf-8"
1136
- );
1137
- fs.writeFileSync(
1138
- "path/to/worker/tsconfig.json",
1139
- JSON.stringify({ compilerOptions: {} }),
1140
- "utf-8"
1141
- );
1142
-
1143
- mockConfirm(
1144
- {
1145
- text: "Would you like to use git to manage this Worker?",
1146
- result: false,
1147
- },
1148
- {
1149
- text: "Would you like to create a Worker at path/to/worker/my-worker/src/index.ts?",
1150
- result: true,
1151
- }
1152
- );
1153
-
1154
- await runWrangler("init path/to/worker/my-worker");
1155
- const tsconfigJson = JSON.parse(
1156
- fs.readFileSync("path/to/worker/tsconfig.json", "utf-8")
1157
- );
1158
- expect(tsconfigJson.compilerOptions).toEqual({});
1159
- expect(std).toMatchInlineSnapshot(`
1160
- Object {
1161
- "debug": "",
1162
- "err": "",
1163
- "out": "✨ Created path/to/worker/my-worker/wrangler.toml
1164
- ✨ Created path/to/worker/my-worker/src/index.ts
1165
-
1166
- To start developing your Worker, run \`npx wrangler dev\`
1167
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1168
- "warn": "",
1169
- }
1170
- `);
1171
- });
1172
-
1173
- it("should offer to install type definitions in an existing typescript project", async () => {
1174
- mockConfirm(
1175
- {
1176
- text: "Would you like to use git to manage this Worker?",
1177
- result: false,
1178
- },
1179
- {
1180
- text: "Would you like to install wrangler into package.json?",
1181
- result: false,
1182
- },
1183
- {
1184
- text: "Would you like to install the type definitions for Workers into your package.json?",
1185
- result: true,
1186
- },
1187
- {
1188
- text: "Would you like to create a Worker at src/index.ts?",
1189
- result: false,
1190
- }
1191
- );
1192
- fs.writeFileSync(
1193
- "./package.json",
1194
- JSON.stringify({
1195
- name: "test",
1196
- version: "1.0.0",
1197
- }),
1198
- "utf-8"
1199
- );
1200
- fs.writeFileSync(
1201
- "./tsconfig.json",
1202
- JSON.stringify({ compilerOptions: {} }),
1203
- "utf-8"
1204
- );
1205
-
1206
- await runWrangler("init");
1207
- const tsconfigJson = JSON.parse(
1208
- fs.readFileSync("./tsconfig.json", "utf-8")
1209
- );
1210
- // unchanged tsconfig
1211
- expect(tsconfigJson.compilerOptions).toEqual({});
1212
- expect(mockPackageManager.addDevDeps).toHaveBeenCalledWith(
1213
- "@cloudflare/workers-types"
1214
- );
1215
- expect(std).toMatchInlineSnapshot(`
1216
- Object {
1217
- "debug": "",
1218
- "err": "",
1219
- "out": "✨ Created wrangler.toml
1220
- ✨ Installed @cloudflare/workers-types into devDependencies
1221
- 🚨 Please add \\"@cloudflare/workers-types\\" to compilerOptions.types in tsconfig.json",
1222
- "warn": "",
1223
- }
1224
- `);
1225
- });
1226
-
1227
- it("should not touch an existing tsconfig.json in an ancestor directory", async () => {
1228
- fs.writeFileSync(
1229
- "./package.json",
1230
- JSON.stringify({
1231
- name: "test",
1232
- version: "1.0.0",
1233
- devDependencies: {
1234
- wrangler: "0.0.0",
1235
- "@cloudflare/workers-types": "0.0.0",
1236
- },
1237
- }),
1238
- "utf-8"
1239
- );
1240
- fs.writeFileSync(
1241
- "./tsconfig.json",
1242
- JSON.stringify({ compilerOptions: {} }),
1243
- "utf-8"
1244
- );
1245
-
1246
- mockConfirm(
1247
- {
1248
- text: "Would you like to use git to manage this Worker?",
1249
- result: false,
1250
- },
1251
- {
1252
- text: "Would you like to create a Worker at src/index.ts?",
1253
- result: true,
1254
- }
1255
- );
1256
-
1257
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
1258
- process.chdir("./sub-1/sub-2");
1259
-
1260
- await runWrangler("init");
1261
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
1262
- expect(fs.existsSync("../../tsconfig.json")).toBe(true);
1263
-
1264
- const tsconfigJson = JSON.parse(
1265
- fs.readFileSync("../../tsconfig.json", "utf-8")
1266
- );
1267
- expect(tsconfigJson.compilerOptions).toEqual({});
1268
-
1269
- expect(std).toMatchInlineSnapshot(`
1270
- Object {
1271
- "debug": "",
1272
- "err": "",
1273
- "out": "✨ Created wrangler.toml
1274
- ✨ Created src/index.ts
1275
-
1276
- To start developing your Worker, run \`npx wrangler dev\`
1277
- To publish your Worker to the Internet, run \`npx wrangler publish\`",
1278
- "warn": "",
1279
- }
1280
- `);
1281
- });
1282
- });
1283
-
1284
- describe("javascript", () => {
1285
- it("should add missing scripts for a non-ts project with .js extension", async () => {
1286
- mockConfirm(
1287
- {
1288
- text: "Would you like to use git to manage this Worker?",
1289
- result: false,
1290
- },
1291
- {
1292
- text: "No package.json found. Would you like to create one?",
1293
- result: true,
1294
- },
1295
- {
1296
- text: "Would you like to install wrangler into package.json?",
1297
- result: false,
1298
- },
1299
- {
1300
- text: "Would you like to use TypeScript?",
1301
- result: false,
1302
- },
1303
- {
1304
- text: "Would you like to create a Worker at src/index.js?",
1305
- result: true,
1306
- }
1307
- );
1308
- await runWrangler("init");
1309
-
1310
- expect(fs.existsSync("./package.json")).toBe(true);
1311
- const packageJson = JSON.parse(
1312
- fs.readFileSync("./package.json", "utf-8")
1313
- );
1314
-
1315
- expect(fs.existsSync("./src/index.js")).toBe(true);
1316
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1317
-
1318
- expect(packageJson.scripts.start).toBe("wrangler dev");
1319
- expect(packageJson.scripts.deploy).toBe("wrangler publish");
1320
- expect(packageJson.name).toContain("wrangler-tests");
1321
- expect(packageJson.version).toEqual("0.0.0");
1322
- expect(std.out).toMatchInlineSnapshot(`
1323
- "✨ Created wrangler.toml
1324
- ✨ Created package.json
1325
- ✨ Created src/index.js
1326
-
1327
- To start developing your Worker, run \`npm start\`
1328
- To publish your Worker to the Internet, run \`npm run deploy\`"
1329
- `);
1330
- });
1331
-
1332
- it("should not overwrite package.json scripts for a non-ts project with .js extension", async () => {
1333
- mockConfirm(
1334
- {
1335
- text: "Would you like to use git to manage this Worker?",
1336
- result: false,
1337
- },
1338
- {
1339
- text: "Would you like to install wrangler into package.json?",
1340
- result: false,
1341
- },
1342
- {
1343
- text: "Would you like to use TypeScript?",
1344
- result: false,
1345
- },
1346
- {
1347
- text: "Would you like to create a Worker at src/index.js?",
1348
- result: true,
1349
- }
1350
- );
1351
- await fsp.writeFile(
1352
- "./package.json",
1353
- JSON.stringify({
1354
- scripts: {
1355
- start: "test-start",
1356
- deploy: "test-publish",
1357
- },
1358
- })
1359
- );
1360
- const packageJson = JSON.parse(
1361
- fs.readFileSync("./package.json", "utf-8")
1362
- );
1363
- await runWrangler("init");
1364
-
1365
- expect(fs.existsSync("./src/index.js")).toBe(true);
1366
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1367
-
1368
- expect(packageJson.scripts.start).toBe("test-start");
1369
- expect(packageJson.scripts.deploy).toBe("test-publish");
1370
- expect(std.out).toMatchInlineSnapshot(`
1371
- "✨ Created wrangler.toml
1372
- ✨ Created src/index.js
1373
-
1374
- To start developing your Worker, run \`npx wrangler dev\`
1375
- To publish your Worker to the Internet, run \`npx wrangler publish\`"
1376
- `);
1377
- });
1378
-
1379
- it("should not offer to create a worker in a non-ts 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("./src", { recursive: true });
1401
- const PLACEHOLDER = "/* placeholder text */";
1402
- fs.writeFileSync("./src/index.js", PLACEHOLDER, "utf-8");
1403
-
1404
- await runWrangler("init");
1405
- expect(fs.readFileSync("./src/index.js", "utf-8")).toBe(PLACEHOLDER);
1406
- expect(fs.existsSync("./src/index.ts")).toBe(false);
1407
- expect(std).toMatchInlineSnapshot(`
1408
- Object {
1409
- "debug": "",
1410
- "err": "",
1411
- "out": "✨ Created wrangler.toml",
1412
- "warn": "",
1413
- }
1414
- `);
1415
- });
1416
-
1417
- it("should not offer to create a worker in a non-ts named worker project if a file already exists at the location", async () => {
1418
- mockConfirm(
1419
- {
1420
- text: "Would you like to use git to manage this Worker?",
1421
- result: false,
1422
- },
1423
- {
1424
- text: "Would you like to install wrangler into package.json?",
1425
- result: false,
1426
- },
1427
- {
1428
- text: "Would you like to use TypeScript?",
1429
- result: false,
1430
- }
1431
- );
1432
-
1433
- fs.writeFileSync(
1434
- "./package.json",
1435
- JSON.stringify({ name: "test", version: "1.0.0" }),
1436
- "utf-8"
1437
- );
1438
- fs.mkdirSync("./my-worker/src", { recursive: true });
1439
- const PLACEHOLDER = "/* placeholder text */";
1440
- fs.writeFileSync("./my-worker/src/index.js", PLACEHOLDER, "utf-8");
1441
-
1442
- await runWrangler("init my-worker");
1443
- expect(fs.readFileSync("./my-worker/src/index.js", "utf-8")).toBe(
1444
- PLACEHOLDER
1445
- );
1446
- expect(fs.existsSync("./my-worker/src/index.ts")).toBe(false);
1447
- expect(std).toMatchInlineSnapshot(`
1448
- Object {
1449
- "debug": "",
1450
- "err": "",
1451
- "out": "✨ Created my-worker/wrangler.toml",
1452
- "warn": "",
1453
- }
1454
- `);
1455
- });
1456
- });
1457
-
1458
- describe("worker names", () => {
1459
- it("should create a worker with a given name", async () => {
1460
- await runWrangler("init my-worker -y");
1461
-
1462
- const parsed = TOML.parse(
1463
- await fsp.readFile("./my-worker/wrangler.toml", "utf-8")
1464
- );
1465
-
1466
- expect(typeof parsed.compatibility_date).toBe("string");
1467
- expect(parsed.name).toBe("my-worker");
1468
- });
1469
-
1470
- it('should create a worker with the name of the current directory if "name" is .', async () => {
1471
- await runWrangler("init . -y");
1472
-
1473
- const parsed = TOML.parse(await fsp.readFile("wrangler.toml", "utf-8"));
1474
-
1475
- expect(typeof parsed.compatibility_date).toBe("string");
1476
- expect(parsed.name).toBe(path.basename(process.cwd()).toLowerCase());
1477
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1478
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1479
- expect(std).toMatchInlineSnapshot(`
1480
- Object {
1481
- "debug": "",
1482
- "err": "",
1483
- "out": "✨ Created wrangler.toml
1484
- ✨ Initialized git repository
1485
- ✨ Created package.json
1486
- ✨ Created tsconfig.json
1487
- ✨ Created src/index.ts
1488
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1489
-
1490
- To start developing your Worker, run \`npm start\`
1491
- To publish your Worker to the Internet, run \`npm run deploy\`",
1492
- "warn": "",
1493
- }
1494
- `);
1495
- });
1496
-
1497
- it('should create a worker in a nested directory if "name" is path/to/worker', async () => {
1498
- await runWrangler("init path/to/worker -y");
1499
-
1500
- const parsed = TOML.parse(
1501
- await fsp.readFile("path/to/worker/wrangler.toml", "utf-8")
1502
- );
1503
-
1504
- expect(typeof parsed.compatibility_date).toBe("string");
1505
- expect(parsed.name).toBe("worker");
1506
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1507
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1508
- expect(std).toMatchInlineSnapshot(`
1509
- Object {
1510
- "debug": "",
1511
- "err": "",
1512
- "out": "✨ Created path/to/worker/wrangler.toml
1513
- ✨ Initialized git repository at path/to/worker
1514
- ✨ Created path/to/worker/package.json
1515
- ✨ Created path/to/worker/tsconfig.json
1516
- ✨ Created path/to/worker/src/index.ts
1517
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1518
-
1519
- To start developing your Worker, run \`cd path/to/worker && npm start\`
1520
- To publish your Worker to the Internet, run \`npm run deploy\`",
1521
- "warn": "",
1522
- }
1523
- `);
1524
- });
1525
-
1526
- it("should normalize characters that aren't lowercase alphanumeric, underscores, or dashes", async () => {
1527
- await runWrangler("init WEIRD_w0rkr_N4m3.js.tsx.tar.gz -y");
1528
- const parsed = TOML.parse(
1529
- await fsp.readFile(
1530
- "WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml",
1531
- "utf-8"
1532
- )
1533
- );
1534
-
1535
- expect(typeof parsed.compatibility_date).toBe("string");
1536
- expect(parsed.name).toBe("weird_w0rkr_n4m3-js-tsx-tar-gz");
1537
- expect(fs.existsSync("./my-worker/package.json")).toBe(false);
1538
- expect(fs.existsSync("./my-worker/tsconfig.json")).toBe(false);
1539
- expect(std).toMatchInlineSnapshot(`
1540
- Object {
1541
- "debug": "",
1542
- "err": "",
1543
- "out": "✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/wrangler.toml
1544
- ✨ Initialized git repository at WEIRD_w0rkr_N4m3.js.tsx.tar.gz
1545
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/package.json
1546
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/tsconfig.json
1547
- ✨ Created WEIRD_w0rkr_N4m3.js.tsx.tar.gz/src/index.ts
1548
- ✨ Installed @cloudflare/workers-types and typescript into devDependencies
1549
-
1550
- To start developing your Worker, run \`cd WEIRD_w0rkr_N4m3.js.tsx.tar.gz && npm start\`
1551
- To publish your Worker to the Internet, run \`npm run deploy\`",
1552
- "warn": "",
1553
- }
1554
- `);
1555
- });
1556
- });
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
+ });
1557
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
+ }