wrangler 0.0.0-e6733a3 → 0.0.0-e6c71262

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