wrangler 0.0.0-e64812e → 0.0.0-e7a0cd5b

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