wrangler 0.0.0-e60dc909 → 0.0.0-e61444cb

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