wrangler 0.0.0-e6733a3 → 0.0.0-e6e1daba

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