wrangler 0.0.0-fb43821 → 0.0.0-fbfc24d7

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 (221) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +51 -14
  3. package/bin/wrangler.js +160 -28
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +5542 -0
  6. package/package.json +172 -113
  7. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  8. package/src/__tests__/config-cache.test.ts +42 -0
  9. package/src/__tests__/configuration.test.ts +4026 -0
  10. package/src/__tests__/dev.test.tsx +1316 -0
  11. package/src/__tests__/generate.test.ts +91 -0
  12. package/src/__tests__/guess-worker-format.test.ts +85 -0
  13. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  14. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  15. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  16. package/src/__tests__/helpers/mock-bin.ts +36 -0
  17. package/src/__tests__/helpers/mock-cfetch.ts +296 -0
  18. package/src/__tests__/helpers/mock-console.ts +97 -0
  19. package/src/__tests__/helpers/mock-dialogs.ts +153 -0
  20. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  21. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  22. package/src/__tests__/helpers/mock-istty.ts +74 -0
  23. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  24. package/src/__tests__/helpers/mock-kv.ts +33 -0
  25. package/src/__tests__/helpers/mock-oauth-flow.ts +250 -0
  26. package/src/__tests__/helpers/mock-process.ts +39 -0
  27. package/src/__tests__/helpers/mock-stdin.ts +108 -0
  28. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  29. package/src/__tests__/helpers/run-in-tmp.ts +38 -0
  30. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  31. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  32. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  33. package/src/__tests__/https-options.test.ts +163 -0
  34. package/src/__tests__/index.test.ts +242 -419
  35. package/src/__tests__/init.test.ts +2170 -0
  36. package/src/__tests__/jest.setup.ts +157 -0
  37. package/src/__tests__/kv.test.ts +1671 -0
  38. package/src/__tests__/logger.test.ts +141 -0
  39. package/src/__tests__/metrics.test.ts +450 -0
  40. package/src/__tests__/package-manager.test.ts +353 -0
  41. package/src/__tests__/pages.test.ts +1275 -0
  42. package/src/__tests__/parse.test.ts +330 -0
  43. package/src/__tests__/publish.test.ts +6903 -0
  44. package/src/__tests__/pubsub.test.ts +367 -0
  45. package/src/__tests__/r2.test.ts +336 -0
  46. package/src/__tests__/route.test.ts +45 -0
  47. package/src/__tests__/secret.test.ts +492 -0
  48. package/src/__tests__/tail.test.ts +802 -0
  49. package/src/__tests__/test-old-node-version.js +31 -0
  50. package/src/__tests__/user.test.ts +155 -0
  51. package/src/__tests__/validate-dev-props.test.ts +56 -0
  52. package/src/__tests__/version.test.ts +35 -0
  53. package/src/__tests__/whoami.test.tsx +289 -0
  54. package/src/__tests__/worker-namespace.test.ts +329 -0
  55. package/src/abort.d.ts +3 -0
  56. package/src/api/dev.ts +93 -0
  57. package/src/api/index.ts +1 -0
  58. package/src/bundle-reporter.tsx +29 -0
  59. package/src/bundle.ts +473 -0
  60. package/src/cfetch/index.ts +121 -0
  61. package/src/cfetch/internal.ts +237 -0
  62. package/src/cli.ts +25 -8
  63. package/src/config/README.md +107 -0
  64. package/src/config/config.ts +263 -0
  65. package/src/config/diagnostics.ts +80 -0
  66. package/src/config/environment.ts +485 -0
  67. package/src/config/index.ts +251 -0
  68. package/src/config/validation-helpers.ts +597 -0
  69. package/src/config/validation.ts +1862 -0
  70. package/src/config-cache.ts +85 -0
  71. package/src/create-worker-preview.ts +257 -0
  72. package/src/create-worker-upload-form.ts +288 -0
  73. package/src/dev/dev-vars.ts +36 -0
  74. package/src/dev/dev.tsx +545 -0
  75. package/src/dev/local.tsx +449 -0
  76. package/src/dev/remote.tsx +421 -0
  77. package/src/dev/use-esbuild.ts +178 -0
  78. package/src/dev/validate-dev-props.ts +31 -0
  79. package/src/dev-registry.tsx +160 -0
  80. package/src/dev.tsx +612 -764
  81. package/src/dialogs.tsx +117 -59
  82. package/src/dispatch-namespace.ts +206 -0
  83. package/src/durable.ts +102 -0
  84. package/src/entry.ts +328 -0
  85. package/src/environment-variables.ts +35 -0
  86. package/src/errors.ts +11 -0
  87. package/src/faye-websocket.d.ts +6 -0
  88. package/src/generate.ts +131 -0
  89. package/src/git-client.ts +42 -0
  90. package/src/global-wrangler-config-path.ts +26 -0
  91. package/src/https-options.ts +127 -0
  92. package/src/index.tsx +2213 -1857
  93. package/src/init.ts +622 -0
  94. package/src/inspect.ts +747 -0
  95. package/src/intl-polyfill.d.ts +139 -0
  96. package/src/is-ci.ts +14 -0
  97. package/src/is-interactive.ts +12 -0
  98. package/src/kv.ts +430 -0
  99. package/src/logger.ts +73 -0
  100. package/src/metrics/index.ts +5 -0
  101. package/src/metrics/metrics-config.ts +239 -0
  102. package/src/metrics/metrics-dispatcher.ts +96 -0
  103. package/src/metrics/metrics-usage-headers.ts +24 -0
  104. package/src/metrics/send-event.ts +92 -0
  105. package/src/miniflare-cli/README.md +30 -0
  106. package/src/miniflare-cli/assets.ts +554 -0
  107. package/src/miniflare-cli/enum-keys.ts +17 -0
  108. package/src/miniflare-cli/index.ts +207 -0
  109. package/src/miniflare-cli/request-context.ts +40 -0
  110. package/src/module-collection.ts +247 -53
  111. package/src/open-in-browser.ts +17 -0
  112. package/src/package-manager.ts +219 -0
  113. package/src/pages/build.tsx +238 -0
  114. package/src/pages/constants.ts +12 -0
  115. package/src/pages/deployments.tsx +106 -0
  116. package/src/pages/dev.tsx +528 -0
  117. package/src/pages/errors.ts +22 -0
  118. package/src/pages/functions/buildPlugin.ts +109 -0
  119. package/src/pages/functions/buildWorker.ts +155 -0
  120. package/src/pages/functions/filepath-routing.test.ts +234 -0
  121. package/src/pages/functions/filepath-routing.ts +189 -0
  122. package/src/pages/functions/identifiers.ts +78 -0
  123. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  124. package/src/pages/functions/routes-consolidation.ts +73 -0
  125. package/src/pages/functions/routes-transformation.test.ts +271 -0
  126. package/src/pages/functions/routes-transformation.ts +122 -0
  127. package/src/pages/functions/routes.ts +151 -0
  128. package/src/pages/functions.tsx +96 -0
  129. package/src/pages/index.tsx +94 -0
  130. package/src/pages/projects.tsx +169 -0
  131. package/src/pages/publish.tsx +391 -0
  132. package/src/pages/types.ts +49 -0
  133. package/src/pages/upload.tsx +413 -0
  134. package/src/pages/utils.ts +12 -0
  135. package/src/parse.ts +286 -0
  136. package/src/paths.ts +26 -0
  137. package/src/preview.ts +31 -0
  138. package/src/proxy.ts +486 -0
  139. package/src/publish.ts +796 -335
  140. package/src/pubsub/index.ts +286 -0
  141. package/src/pubsub/pubsub-commands.tsx +623 -0
  142. package/src/r2.ts +131 -0
  143. package/src/selfsigned.d.ts +29 -0
  144. package/src/sites.tsx +357 -100
  145. package/src/tail/filters.ts +281 -0
  146. package/src/tail/index.ts +312 -0
  147. package/src/tail/printing.ts +107 -0
  148. package/src/update-check.ts +19 -0
  149. package/src/user/choose-account.tsx +69 -0
  150. package/src/user/env-vars.ts +46 -0
  151. package/src/user/generate-auth-url.ts +33 -0
  152. package/src/user/generate-random-state.ts +16 -0
  153. package/src/user/index.ts +3 -0
  154. package/src/user/user.tsx +1191 -0
  155. package/src/whoami.tsx +169 -0
  156. package/src/worker.ts +203 -0
  157. package/src/zones.ts +167 -0
  158. package/templates/checked-fetch.js +17 -0
  159. package/templates/first-party-worker-module-facade.ts +18 -0
  160. package/templates/format-dev-errors.ts +32 -0
  161. package/templates/gitignore +171 -0
  162. package/templates/new-worker-scheduled.js +17 -0
  163. package/templates/new-worker-scheduled.ts +32 -0
  164. package/templates/new-worker.js +15 -0
  165. package/templates/new-worker.ts +30 -0
  166. package/templates/no-op-worker.js +10 -0
  167. package/templates/pages-shim.ts +9 -0
  168. package/templates/pages-template-plugin.ts +167 -0
  169. package/templates/pages-template-worker.ts +172 -0
  170. package/templates/serve-static-assets.ts +63 -0
  171. package/templates/service-bindings-module-facade.js +54 -0
  172. package/templates/service-bindings-sw-facade.js +42 -0
  173. package/templates/tsconfig.json +105 -0
  174. package/wrangler-dist/cli.d.ts +338 -0
  175. package/wrangler-dist/cli.js +127880 -101761
  176. package/src/__tests__/mock-cfetch.js +0 -42
  177. package/src/api/form_data.ts +0 -141
  178. package/src/api/inspect.ts +0 -430
  179. package/src/api/preview.ts +0 -128
  180. package/src/api/worker.ts +0 -161
  181. package/src/cfetch.ts +0 -72
  182. package/src/config.ts +0 -122
  183. package/src/kv.tsx +0 -211
  184. package/src/pages.tsx +0 -818
  185. package/src/tail.tsx +0 -71
  186. package/src/user.tsx +0 -1029
  187. package/static-asset-facade.js +0 -47
  188. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  189. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  190. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  191. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  192. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  193. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  194. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  195. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  196. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  197. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  198. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  199. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  200. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  201. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  202. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  203. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  204. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  205. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  206. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  207. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  208. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  209. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  210. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  211. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  212. package/vendor/wrangler-mime/LICENSE +0 -21
  213. package/vendor/wrangler-mime/Mime.js +0 -97
  214. package/vendor/wrangler-mime/README.md +0 -187
  215. package/vendor/wrangler-mime/cli.js +0 -46
  216. package/vendor/wrangler-mime/index.js +0 -4
  217. package/vendor/wrangler-mime/lite.js +0 -4
  218. package/vendor/wrangler-mime/package.json +0 -52
  219. package/vendor/wrangler-mime/types/other.js +0 -1
  220. package/vendor/wrangler-mime/types/standard.js +0 -1
  221. package/wrangler-dist/cli.js.map +0 -7
@@ -1,422 +1,245 @@
1
- import * as fs from "node:fs";
2
- import * as fsp from "node:fs/promises";
3
- import * as os from "node:os";
4
- import * as path from "node:path";
5
- import * as TOML from "@iarna/toml";
6
- import { main } from "../index";
7
- import { setMock, unsetAllMocks } from "./mock-cfetch";
8
- import { confirm } from "../dialogs";
9
-
10
- jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
11
-
12
- jest.mock("../dialogs", () => {
13
- return {
14
- ...jest.requireActual<object>("../dialogs"),
15
- confirm: jest
16
- .fn()
17
- .mockName("confirmMock")
18
- .mockImplementation(() => {
19
- // By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
20
- throw new Error("Unexpected call to `confirm()`.");
21
- }),
22
- };
23
- });
24
-
25
- /**
26
- * Mock the implementation of `confirm()` that will respond with configured results
27
- * for configured confirmation text messages.
28
- *
29
- * If there is a call to `confirm()` that does not match any of the expectations
30
- * then an error is thrown.
31
- */
32
- function mockConfirm(...expectations: { text: string; result: boolean }[]) {
33
- (confirm as jest.Mock).mockImplementation((text: string) => {
34
- for (const { text: expectedText, result } of expectations) {
35
- if (text === expectedText) {
36
- return result;
37
- }
38
- }
39
- throw new Error(`Unexpected confirmation message: ${text}`);
40
- });
41
- }
42
-
43
- async function w(cmd?: string) {
44
- const logSpy = jest.spyOn(console, "log").mockImplementation();
45
- const errorSpy = jest.spyOn(console, "error").mockImplementation();
46
- const warnSpy = jest.spyOn(console, "warn").mockImplementation();
47
- try {
48
- await main(cmd?.split(" ") ?? []);
49
- return {
50
- stdout: logSpy.mock.calls.flat(2).join("\n"),
51
- stderr: errorSpy.mock.calls.flat(2).join("\n"),
52
- warnings: warnSpy.mock.calls.flat(2).join("\n"),
53
- };
54
- } finally {
55
- logSpy.mockRestore();
56
- errorSpy.mockRestore();
57
- warnSpy.mockRestore();
58
- }
59
- }
1
+ import { getPackageManager } from "../package-manager";
2
+ import { mockConsoleMethods } from "./helpers/mock-console";
3
+ import { runInTempDir } from "./helpers/run-in-tmp";
4
+ import { runWrangler } from "./helpers/run-wrangler";
5
+ import { writeWorkerSource } from "./helpers/write-worker-source";
6
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
7
+ import type { PackageManager } from "../package-manager";
60
8
 
61
9
  describe("wrangler", () => {
62
- describe("no command", () => {
63
- it("should display a list of available commands", async () => {
64
- const { stdout, stderr } = await w();
65
-
66
- expect(stdout).toMatchInlineSnapshot(`
67
- "wrangler
68
-
69
- Commands:
70
- wrangler init [name] 📥 Create a wrangler.toml configuration file
71
- wrangler dev <filename> 👂 Start a local server for developing your worker
72
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
73
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
74
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
75
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
76
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
77
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
78
- wrangler pages ⚡️ Configure Cloudflare Pages
79
-
80
- Flags:
81
- -c, --config Path to .toml configuration file [string]
82
- -h, --help Show help [boolean]
83
- -v, --version Show version number [boolean]
84
-
85
- Options:
86
- -l, --local Run on my machine [boolean] [default: false]"
87
- `);
88
-
89
- expect(stderr).toMatchInlineSnapshot(`""`);
90
- });
91
- });
92
-
93
- describe("invalid command", () => {
94
- it("should display an error", async () => {
95
- const { stdout, stderr } = await w("invalid-command");
96
-
97
- expect(stdout).toMatchInlineSnapshot(`
98
- "wrangler
99
-
100
- Commands:
101
- wrangler init [name] 📥 Create a wrangler.toml configuration file
102
- wrangler dev <filename> 👂 Start a local server for developing your worker
103
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
104
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
105
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
106
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
107
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
108
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
109
- wrangler pages ⚡️ Configure Cloudflare Pages
110
-
111
- Flags:
112
- -c, --config Path to .toml configuration file [string]
113
- -h, --help Show help [boolean]
114
- -v, --version Show version number [boolean]
115
-
116
- Options:
117
- -l, --local Run on my machine [boolean] [default: false]"
118
- `);
119
-
120
- expect(stderr).toMatchInlineSnapshot(`
121
- "
122
- Unknown command: invalid-command."
123
- `);
124
- });
125
- });
126
-
127
- describe("init", () => {
128
- const ogcwd = process.cwd();
129
- let tmpDir: string;
130
-
131
- beforeEach(async () => {
132
- tmpDir = await fsp.mkdtemp(path.join(os.tmpdir(), "init-"));
133
- process.chdir(tmpDir);
134
- });
135
-
136
- afterEach(async () => {
137
- process.chdir(ogcwd);
138
- await fsp.rm(tmpDir, { recursive: true });
139
- });
140
-
141
- it("should create a wrangler.toml", async () => {
142
- mockConfirm({
143
- text: "No package.json found. Would you like to create one?",
144
- result: false,
145
- });
146
- await w("init");
147
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
148
- expect(typeof parsed.compatibility_date).toBe("string");
149
- expect(fs.existsSync("./package.json")).toBe(false);
150
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
151
- });
152
-
153
- it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
154
- fs.closeSync(fs.openSync("./wrangler.toml", "w"));
155
- mockConfirm({
156
- text: "Do you want to continue initializing this project?",
157
- result: false,
158
- });
159
- const { stderr } = await w("init");
160
- expect(stderr).toContain("wrangler.toml file already exists!");
161
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
162
- expect(typeof parsed.compatibility_date).toBe("undefined");
163
- });
164
-
165
- it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
166
- fs.closeSync(fs.openSync("./wrangler.toml", "w"));
167
- mockConfirm(
168
- {
169
- text: "Do you want to continue initializing this project?",
170
- result: true,
171
- },
172
- {
173
- text: "No package.json found. Would you like to create one?",
174
- result: false,
175
- }
176
- );
177
- const { stderr } = await w("init");
178
- expect(stderr).toContain("wrangler.toml file already exists!");
179
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
180
- expect(typeof parsed.compatibility_date).toBe("string");
181
- });
182
-
183
- it("should create a package.json if none is found and user confirms", async () => {
184
- mockConfirm(
185
- {
186
- text: "No package.json found. Would you like to create one?",
187
- result: true,
188
- },
189
- {
190
- text: "Would you like to use typescript?",
191
- result: false,
192
- }
193
- );
194
- await w("init");
195
- expect(fs.existsSync("./package.json")).toBe(true);
196
- const packageJson = JSON.parse(
197
- fs.readFileSync("./package.json", "utf-8")
198
- );
199
- expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
200
- expect(packageJson.version).toEqual("0.0.1");
201
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
202
- });
203
-
204
- it("should not touch an existing package.json in the same directory", async () => {
205
- mockConfirm({
206
- text: "Would you like to use typescript?",
207
- result: false,
208
- });
209
-
210
- fs.writeFileSync(
211
- "./package.json",
212
- JSON.stringify({ name: "test", version: "1.0.0" }),
213
- "utf-8"
214
- );
215
-
216
- await w("init");
217
- const packageJson = JSON.parse(
218
- fs.readFileSync("./package.json", "utf-8")
219
- );
220
- expect(packageJson.name).toEqual("test");
221
- expect(packageJson.version).toEqual("1.0.0");
222
- });
223
-
224
- it("should not touch an existing package.json in an ancestor directory", async () => {
225
- mockConfirm({
226
- text: "Would you like to use typescript?",
227
- result: false,
228
- });
229
-
230
- fs.writeFileSync(
231
- "./package.json",
232
- JSON.stringify({ name: "test", version: "1.0.0" }),
233
- "utf-8"
234
- );
235
-
236
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
237
- process.chdir("./sub-1/sub-2");
238
-
239
- await w("init");
240
- expect(fs.existsSync("./package.json")).toBe(false);
241
- expect(fs.existsSync("../../package.json")).toBe(true);
242
-
243
- const packageJson = JSON.parse(
244
- fs.readFileSync("../../package.json", "utf-8")
245
- );
246
- expect(packageJson.name).toEqual("test");
247
- expect(packageJson.version).toEqual("1.0.0");
248
- });
249
-
250
- it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
251
- mockConfirm(
252
- {
253
- text: "No package.json found. Would you like to create one?",
254
- result: true,
255
- },
256
- {
257
- text: "Would you like to use typescript?",
258
- result: true,
259
- }
260
- );
261
- await w("init");
262
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
263
- const tsconfigJson = JSON.parse(
264
- fs.readFileSync("./tsconfig.json", "utf-8")
265
- );
266
- expect(tsconfigJson.compilerOptions.types).toEqual([
267
- "@cloudflare/workers-types",
268
- ]);
269
- const packageJson = JSON.parse(
270
- fs.readFileSync("./package.json", "utf-8")
271
- );
272
- expect(packageJson.devDependencies).toEqual({
273
- "@cloudflare/workers-types": expect.any(String),
274
- });
275
- });
276
-
277
- it("should not touch an existing tsconfig.json in the same directory", async () => {
278
- fs.writeFileSync(
279
- "./package.json",
280
- JSON.stringify({ name: "test", version: "1.0.0" }),
281
- "utf-8"
282
- );
283
- fs.writeFileSync(
284
- "./tsconfig.json",
285
- JSON.stringify({ compilerOptions: {} }),
286
- "utf-8"
287
- );
288
-
289
- await w("init");
290
- const tsconfigJson = JSON.parse(
291
- fs.readFileSync("./tsconfig.json", "utf-8")
292
- );
293
- expect(tsconfigJson.compilerOptions).toEqual({});
294
- });
295
-
296
- it("should not touch an existing package.json in an ancestor directory", async () => {
297
- fs.writeFileSync(
298
- "./package.json",
299
- JSON.stringify({ name: "test", version: "1.0.0" }),
300
- "utf-8"
301
- );
302
- fs.writeFileSync(
303
- "./tsconfig.json",
304
- JSON.stringify({ compilerOptions: {} }),
305
- "utf-8"
306
- );
307
-
308
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
309
- process.chdir("./sub-1/sub-2");
310
-
311
- await w("init");
312
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
313
- expect(fs.existsSync("../../tsconfig.json")).toBe(true);
314
-
315
- const tsconfigJson = JSON.parse(
316
- fs.readFileSync("../../tsconfig.json", "utf-8")
317
- );
318
- expect(tsconfigJson.compilerOptions).toEqual({});
319
- });
320
-
321
- it("should error if `--type` is used", async () => {
322
- const noValue = await w("init --type");
323
- expect(noValue.stderr).toMatchInlineSnapshot(
324
- `"The --type option is no longer supported."`
325
- );
326
- });
327
-
328
- it("should error if `--type javascript` is used", async () => {
329
- const javascriptValue = await w("init --type javascript");
330
- expect(javascriptValue.stderr).toMatchInlineSnapshot(
331
- `"The --type option is no longer supported."`
332
- );
333
- });
334
-
335
- it("should error if `--type rust` is used", async () => {
336
- const rustValue = await w("init --type rust");
337
- expect(rustValue.stderr).toMatchInlineSnapshot(
338
- `"The --type option is no longer supported."`
339
- );
340
- });
341
-
342
- it("should error if `--type webpack` is used", async () => {
343
- const webpackValue = await w("init --type webpack");
344
- expect(webpackValue.stderr).toMatchInlineSnapshot(`
345
- "The --type option is no longer supported.
346
- If you wish to use webpack then you will need to create a custom build."
347
- `);
348
- });
349
- });
350
-
351
- describe("kv:namespace", () => {
352
- afterEach(() => {
353
- unsetAllMocks();
354
- });
355
-
356
- it("can create a namespace", async () => {
357
- const KVNamespaces: { title: string; id: string }[] = [];
358
- setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
359
- expect(init.method === "POST");
360
- expect(uri[0]).toEqual(
361
- "/accounts/some-account-id/storage/kv/namespaces"
362
- );
363
- const { title } = JSON.parse(init.body);
364
- expect(title).toEqual("worker-UnitTestNamespace");
365
- KVNamespaces.push({ title, id: "some-namespace-id" });
366
- return { id: "some-namespace-id" };
367
- });
368
-
369
- await w("kv:namespace create UnitTestNamespace");
370
-
371
- expect(KVNamespaces).toEqual([
372
- {
373
- title: "worker-UnitTestNamespace",
374
- id: "some-namespace-id",
375
- },
376
- ]);
377
- });
378
-
379
- it("can list namespaces", async () => {
380
- const KVNamespaces: { title: string; id: string }[] = [
381
- { title: "title-1", id: "id-1" },
382
- { title: "title-2", id: "id-2" },
383
- ];
384
- setMock(
385
- "/accounts/:accountId/storage/kv/namespaces\\?:qs",
386
- (uri, init) => {
387
- expect(uri[0]).toContain(
388
- "/accounts/some-account-id/storage/kv/namespaces"
389
- );
390
- expect(uri[2]).toContain("per_page=100");
391
- expect(uri[2]).toContain("order=title");
392
- expect(uri[2]).toContain("direction=asc");
393
- expect(uri[2]).toContain("page=1");
394
- expect(init).toBe(undefined);
395
- return KVNamespaces;
396
- }
397
- );
398
- const { stdout } = await w("kv:namespace list");
399
- const namespaces = JSON.parse(stdout) as { id: string; title: string }[];
400
- expect(namespaces).toEqual(KVNamespaces);
401
- });
402
-
403
- it("can delete a namespace", async () => {
404
- let accountId = "";
405
- let namespaceId = "";
406
- setMock(
407
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId",
408
- (uri, init) => {
409
- accountId = uri[1];
410
- namespaceId = uri[2];
411
- expect(uri[0]).toEqual(
412
- "/accounts/some-account-id/storage/kv/namespaces/some-namespace-id"
413
- );
414
- expect(init.method).toBe("DELETE");
415
- }
416
- );
417
- await w(`kv:namespace delete --namespace-id some-namespace-id`);
418
- expect(accountId).toEqual("some-account-id");
419
- expect(namespaceId).toEqual("some-namespace-id");
420
- });
421
- });
10
+ let mockPackageManager: PackageManager;
11
+ runInTempDir();
12
+
13
+ beforeEach(() => {
14
+ mockPackageManager = {
15
+ cwd: process.cwd(),
16
+ // @ts-expect-error we're making a fake package manager here
17
+ type: "mockpm",
18
+ addDevDeps: jest.fn(),
19
+ install: jest.fn(),
20
+ };
21
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
22
+ });
23
+
24
+ const std = mockConsoleMethods();
25
+
26
+ describe("no command", () => {
27
+ it("should display a list of available commands", async () => {
28
+ await runWrangler();
29
+
30
+ expect(std.out).toMatchInlineSnapshot(`
31
+ "wrangler
32
+
33
+ Commands:
34
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
35
+ wrangler dev [script] 👂 Start a local server for developing your worker
36
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
37
+ wrangler tail [worker] 🦚 Starts a log tailing session for a published Worker.
38
+ wrangler secret 🤫 Generate a secret that can be referenced in a Worker
39
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
40
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
41
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
42
+ wrangler pages ⚡️ Configure Cloudflare Pages
43
+ wrangler r2 📦 Interact with an R2 store
44
+ wrangler dispatch-namespace 📦 Interact with a dispatch namespace
45
+ wrangler pubsub 📮 Interact and manage Pub/Sub Brokers
46
+ wrangler login 🔓 Login to Cloudflare
47
+ wrangler logout 🚪 Logout from Cloudflare
48
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
49
+
50
+ Flags:
51
+ -c, --config Path to .toml configuration file [string]
52
+ -h, --help Show help [boolean]
53
+ -v, --version Show version number [boolean]"
54
+ `);
55
+
56
+ expect(std.err).toMatchInlineSnapshot(`""`);
57
+ });
58
+ });
59
+
60
+ describe("invalid command", () => {
61
+ it("should display an error", async () => {
62
+ await expect(
63
+ runWrangler("invalid-command")
64
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
65
+ `"Unknown argument: invalid-command"`
66
+ );
67
+
68
+ expect(std.out).toMatchInlineSnapshot(`
69
+ "
70
+ wrangler
71
+
72
+ Commands:
73
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
74
+ wrangler dev [script] 👂 Start a local server for developing your worker
75
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
76
+ wrangler tail [worker] 🦚 Starts a log tailing session for a published Worker.
77
+ wrangler secret 🤫 Generate a secret that can be referenced in a Worker
78
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
79
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
80
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
81
+ wrangler pages ⚡️ Configure Cloudflare Pages
82
+ wrangler r2 📦 Interact with an R2 store
83
+ wrangler dispatch-namespace 📦 Interact with a dispatch namespace
84
+ wrangler pubsub 📮 Interact and manage Pub/Sub Brokers
85
+ wrangler login 🔓 Login to Cloudflare
86
+ wrangler logout 🚪 Logout from Cloudflare
87
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
88
+
89
+ Flags:
90
+ -c, --config Path to .toml configuration file [string]
91
+ -h, --help Show help [boolean]
92
+ -v, --version Show version number [boolean]"
93
+ `);
94
+ expect(std.err).toMatchInlineSnapshot(`
95
+ "X [ERROR] Unknown argument: invalid-command
96
+
97
+ "
98
+ `);
99
+ });
100
+ });
101
+
102
+ describe("preview", () => {
103
+ it("should throw an error if the deprecated command is used with positional arguments", async () => {
104
+ await expect(runWrangler("preview GET")).rejects
105
+ .toThrowErrorMatchingInlineSnapshot(`
106
+ "Deprecation:
107
+ The \`wrangler preview\` command has been deprecated.
108
+ Try using \`wrangler dev\` to to try out a worker during development.
109
+ "
110
+ `);
111
+ await expect(runWrangler(`preview GET "SomeBody"`)).rejects
112
+ .toThrowErrorMatchingInlineSnapshot(`
113
+ "Deprecation:
114
+ The \`wrangler preview\` command has been deprecated.
115
+ Try using \`wrangler dev\` to to try out a worker during development.
116
+ "
117
+ `);
118
+ });
119
+ });
120
+
121
+ describe("subcommand implicit help ran on incomplete command execution", () => {
122
+ it("no subcommand for 'secret' should display a list of available subcommands", async () => {
123
+ await runWrangler("secret");
124
+ await endEventLoop();
125
+ expect(std.out).toMatchInlineSnapshot(`
126
+ "wrangler secret
127
+
128
+ 🤫 Generate a secret that can be referenced in a Worker
129
+
130
+ Commands:
131
+ wrangler secret put <key> Create or update a secret variable for a Worker
132
+ wrangler secret delete <key> Delete a secret variable from a Worker
133
+ wrangler secret list List all secrets for a Worker
134
+
135
+ Flags:
136
+ -c, --config Path to .toml configuration file [string]
137
+ -h, --help Show help [boolean]
138
+ -v, --version Show version number [boolean]"
139
+ `);
140
+ });
141
+
142
+ it("no subcommand 'kv:namespace' should display a list of available subcommands", async () => {
143
+ await runWrangler("kv:namespace");
144
+ await endEventLoop();
145
+ expect(std.out).toMatchInlineSnapshot(`
146
+ "wrangler kv:namespace
147
+
148
+ 🗂️ Interact with your Workers KV Namespaces
149
+
150
+ Commands:
151
+ wrangler kv:namespace create <namespace> Create a new namespace
152
+ wrangler kv:namespace list Outputs a list of all KV namespaces associated with your account id.
153
+ wrangler kv:namespace delete Deletes a given namespace.
154
+
155
+ Flags:
156
+ -c, --config Path to .toml configuration file [string]
157
+ -h, --help Show help [boolean]
158
+ -v, --version Show version number [boolean]"
159
+ `);
160
+ });
161
+
162
+ it("no subcommand 'kv:key' should display a list of available subcommands", async () => {
163
+ await runWrangler("kv:key");
164
+ await endEventLoop();
165
+ expect(std.out).toMatchInlineSnapshot(`
166
+ "wrangler kv:key
167
+
168
+ 🔑 Individually manage Workers KV key-value pairs
169
+
170
+ Commands:
171
+ wrangler kv:key put <key> [value] Writes a single key/value pair to the given namespace.
172
+ wrangler kv:key list Outputs a list of all keys in a given namespace.
173
+ wrangler kv:key get <key> Reads a single value by key from the given namespace.
174
+ wrangler kv:key delete <key> Removes a single key value pair from the given namespace.
175
+
176
+ Flags:
177
+ -c, --config Path to .toml configuration file [string]
178
+ -h, --help Show help [boolean]
179
+ -v, --version Show version number [boolean]"
180
+ `);
181
+ });
182
+
183
+ it("no subcommand 'kv:bulk' should display a list of available subcommands", async () => {
184
+ await runWrangler("kv:bulk");
185
+ await endEventLoop();
186
+ expect(std.out).toMatchInlineSnapshot(`
187
+ "wrangler kv:bulk
188
+
189
+ 💪 Interact with multiple Workers KV key-value pairs at once
190
+
191
+ Commands:
192
+ wrangler kv:bulk put <filename> Upload multiple key-value pairs to a namespace
193
+ wrangler kv:bulk delete <filename> Delete multiple key-value pairs from a namespace
194
+
195
+ Flags:
196
+ -c, --config Path to .toml configuration file [string]
197
+ -h, --help Show help [boolean]
198
+ -v, --version Show version number [boolean]"
199
+ `);
200
+ });
201
+
202
+ it("no subcommand 'r2' should display a list of available subcommands", async () => {
203
+ await runWrangler("r2");
204
+ await endEventLoop();
205
+ expect(std.out).toMatchInlineSnapshot(`
206
+ "wrangler r2
207
+
208
+ 📦 Interact with an R2 store
209
+
210
+ Commands:
211
+ wrangler r2 object Manage R2 objects
212
+ wrangler r2 bucket Manage R2 buckets
213
+
214
+ Flags:
215
+ -c, --config Path to .toml configuration file [string]
216
+ -h, --help Show help [boolean]
217
+ -v, --version Show version number [boolean]"
218
+ `);
219
+ });
220
+ });
221
+
222
+ it("should print a deprecation message for 'build' and then try to run `publish --dry-run --outdir`", async () => {
223
+ writeWranglerToml({
224
+ main: "index.js",
225
+ });
226
+ writeWorkerSource();
227
+ await runWrangler("build");
228
+ await endEventLoop();
229
+ expect(std.out).toMatchInlineSnapshot(`
230
+ "▲ [WARNING] Deprecation: \`wrangler build\` has been deprecated.
231
+
232
+ Please refer to https://developers.cloudflare.com/workers/wrangler/migration/deprecations/#build
233
+ for more information.
234
+ Attempting to run \`wrangler publish --dry-run --outdir=dist\` for you instead:
235
+
236
+
237
+ --dry-run: exiting now.
238
+ Total Upload: 0xx KiB / gzip: 0xx KiB"
239
+ `);
240
+ });
422
241
  });
242
+
243
+ function endEventLoop() {
244
+ return new Promise((resolve) => setImmediate(resolve));
245
+ }