wrangler 0.0.0-e322475 → 0.0.0-e33294b0

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