wrangler 0.0.0-e1b8ac4 → 0.0.0-e1c2f5b9

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