wrangler 0.0.0-e61fba8 → 0.0.0-e665ccc3

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