wrangler 0.0.0-e64812e → 0.0.0-e665ccc3

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