wrangler 0.0.0-e6e1daba → 0.0.0-e6fea1318

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