wrangler 0.0.0-e1c2f5b9 → 0.0.0-e1d2fd668

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