wrangler 0.0.0-e928f94 → 0.0.0-e93063e9

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 (338) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -14
  3. package/bin/wrangler.js +180 -28
  4. package/kv-asset-handler.js +1 -0
  5. package/miniflare-dist/index.mjs +6364 -0
  6. package/package.json +194 -113
  7. package/src/__tests__/access.test.ts +25 -0
  8. package/src/__tests__/api-dev.test.ts +238 -0
  9. package/src/__tests__/api-devregistry.test.js +64 -0
  10. package/src/__tests__/api.test.ts +80 -0
  11. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  12. package/src/__tests__/config-cache.test.ts +42 -0
  13. package/src/__tests__/configuration.test.ts +4163 -0
  14. package/src/__tests__/d1/d1.test.ts +123 -0
  15. package/src/__tests__/d1/splitter.test.ts +255 -0
  16. package/src/__tests__/delete.test.ts +266 -0
  17. package/src/__tests__/deployments.test.ts +105 -0
  18. package/src/__tests__/dev.test.tsx +1586 -0
  19. package/src/__tests__/generate.test.ts +238 -0
  20. package/src/__tests__/get-host-from-url.test.ts +16 -0
  21. package/src/__tests__/guess-worker-format.test.ts +85 -0
  22. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  23. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  24. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  25. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  26. package/src/__tests__/helpers/mock-bin.ts +36 -0
  27. package/src/__tests__/helpers/mock-cfetch.ts +278 -0
  28. package/src/__tests__/helpers/mock-console.ts +106 -0
  29. package/src/__tests__/helpers/mock-dialogs.ts +137 -0
  30. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  31. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  32. package/src/__tests__/helpers/mock-istty.ts +74 -0
  33. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  34. package/src/__tests__/helpers/mock-kv.ts +33 -0
  35. package/src/__tests__/helpers/mock-oauth-flow.ts +271 -0
  36. package/src/__tests__/helpers/mock-process.ts +34 -0
  37. package/src/__tests__/helpers/mock-stdin.ts +108 -0
  38. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  39. package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
  40. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  41. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  42. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  43. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  44. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  45. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  46. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  47. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  48. package/src/__tests__/helpers/msw/index.ts +21 -0
  49. package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
  50. package/src/__tests__/helpers/run-in-tmp.ts +38 -0
  51. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  52. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  53. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  54. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  55. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  56. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  57. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  58. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  59. package/src/__tests__/https-options.test.ts +163 -0
  60. package/src/__tests__/index.test.ts +264 -149
  61. package/src/__tests__/init.test.ts +3147 -0
  62. package/src/__tests__/jest.setup.ts +198 -0
  63. package/src/__tests__/kv.test.ts +1788 -0
  64. package/src/__tests__/logger.test.ts +141 -0
  65. package/src/__tests__/logout.test.ts +47 -0
  66. package/src/__tests__/metrics.test.ts +493 -0
  67. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  68. package/src/__tests__/middleware.test.ts +750 -0
  69. package/src/__tests__/package-manager.test.ts +353 -0
  70. package/src/__tests__/pages-deployment-tail.test.ts +884 -0
  71. package/src/__tests__/pages.test.ts +2899 -0
  72. package/src/__tests__/parse.test.ts +330 -0
  73. package/src/__tests__/paths.test.ts +39 -0
  74. package/src/__tests__/publish.test.ts +7769 -0
  75. package/src/__tests__/pubsub.test.ts +490 -0
  76. package/src/__tests__/queues.test.ts +522 -0
  77. package/src/__tests__/r2.test.ts +393 -0
  78. package/src/__tests__/route.test.ts +45 -0
  79. package/src/__tests__/secret.test.ts +701 -0
  80. package/src/__tests__/tail.test.ts +905 -0
  81. package/src/__tests__/test-old-node-version.js +31 -0
  82. package/src/__tests__/tsconfig-sanity.ts +12 -0
  83. package/src/__tests__/tsconfig.json +8 -0
  84. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  85. package/src/__tests__/type-generation.test.ts +233 -0
  86. package/src/__tests__/user.test.ts +129 -0
  87. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  88. package/src/__tests__/validate-dev-props.test.ts +56 -0
  89. package/src/__tests__/version.test.ts +35 -0
  90. package/src/__tests__/whoami.test.tsx +183 -0
  91. package/src/__tests__/worker-namespace.test.ts +326 -0
  92. package/src/abort.d.ts +3 -0
  93. package/src/api/dev.ts +247 -0
  94. package/src/api/index.ts +2 -0
  95. package/src/bundle-reporter.ts +68 -0
  96. package/src/bundle.ts +823 -0
  97. package/src/cfetch/index.ts +158 -0
  98. package/src/cfetch/internal.ts +261 -0
  99. package/src/cli.ts +26 -8
  100. package/src/config/README.md +107 -0
  101. package/src/config/config.ts +275 -0
  102. package/src/config/diagnostics.ts +80 -0
  103. package/src/config/environment.ts +577 -0
  104. package/src/config/index.ts +358 -0
  105. package/src/config/validation-helpers.ts +597 -0
  106. package/src/config/validation.ts +2151 -0
  107. package/src/config-cache.ts +85 -0
  108. package/src/create-worker-preview.ts +282 -0
  109. package/src/create-worker-upload-form.ts +325 -0
  110. package/src/d1/backups.tsx +223 -0
  111. package/src/d1/constants.ts +2 -0
  112. package/src/d1/create.tsx +63 -0
  113. package/src/d1/delete.ts +56 -0
  114. package/src/d1/execute.tsx +323 -0
  115. package/src/d1/formatTimeAgo.ts +14 -0
  116. package/src/d1/index.ts +101 -0
  117. package/src/d1/list.tsx +48 -0
  118. package/src/d1/migrations/apply.tsx +197 -0
  119. package/src/d1/migrations/create.tsx +77 -0
  120. package/src/d1/migrations/helpers.ts +147 -0
  121. package/src/d1/migrations/index.ts +3 -0
  122. package/src/d1/migrations/list.tsx +79 -0
  123. package/src/d1/migrations/options.ts +18 -0
  124. package/src/d1/options.ts +22 -0
  125. package/src/d1/splitter.ts +161 -0
  126. package/src/d1/types.ts +24 -0
  127. package/src/d1/utils.ts +48 -0
  128. package/src/delete.ts +103 -0
  129. package/src/deployments.ts +83 -0
  130. package/src/deprecated/index.ts +145 -0
  131. package/src/dev/dev-vars.ts +39 -0
  132. package/src/dev/dev.tsx +614 -0
  133. package/src/dev/get-local-persistence-path.ts +31 -0
  134. package/src/dev/local.tsx +932 -0
  135. package/src/dev/remote.tsx +630 -0
  136. package/src/dev/start-server.ts +543 -0
  137. package/src/dev/use-esbuild.ts +204 -0
  138. package/src/dev/validate-dev-props.ts +31 -0
  139. package/src/dev-registry.ts +201 -0
  140. package/src/dev.tsx +929 -704
  141. package/src/dialogs.ts +136 -0
  142. package/src/dispatch-namespace.ts +207 -0
  143. package/src/docs/index.ts +97 -0
  144. package/src/durable.ts +102 -0
  145. package/src/entry.ts +329 -0
  146. package/src/environment-variables/factory.ts +88 -0
  147. package/src/environment-variables/misc-variables.ts +30 -0
  148. package/src/errors.ts +11 -0
  149. package/src/generate/index.ts +300 -0
  150. package/src/git-client.ts +135 -0
  151. package/src/global-wrangler-config-path.ts +26 -0
  152. package/src/https-options.ts +127 -0
  153. package/src/index.ts +694 -0
  154. package/src/init.ts +1023 -0
  155. package/src/inspect.ts +767 -0
  156. package/src/intl-polyfill.d.ts +139 -0
  157. package/src/is-ci.ts +14 -0
  158. package/src/is-interactive.ts +16 -0
  159. package/src/jest.d.ts +4 -0
  160. package/src/kv/helpers.ts +430 -0
  161. package/src/kv/index.ts +596 -0
  162. package/src/logger.ts +104 -0
  163. package/src/metrics/index.ts +5 -0
  164. package/src/metrics/metrics-config.ts +239 -0
  165. package/src/metrics/metrics-dispatcher.ts +96 -0
  166. package/src/metrics/metrics-usage-headers.ts +24 -0
  167. package/src/metrics/send-event.ts +97 -0
  168. package/src/miniflare-cli/README.md +30 -0
  169. package/src/miniflare-cli/assets.ts +250 -0
  170. package/src/miniflare-cli/index.ts +210 -0
  171. package/src/miniflare-cli/request-context.ts +40 -0
  172. package/src/miniflare-cli/tsconfig.json +9 -0
  173. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  174. package/src/miniflare-cli/types.ts +11 -0
  175. package/src/module-collection.ts +256 -53
  176. package/src/open-in-browser.ts +17 -0
  177. package/src/package-manager.ts +219 -0
  178. package/src/pages/build.ts +270 -0
  179. package/src/pages/constants.ts +18 -0
  180. package/src/pages/deployment-tails.ts +284 -0
  181. package/src/pages/deployments.tsx +84 -0
  182. package/src/pages/dev.ts +711 -0
  183. package/src/pages/errors.ts +66 -0
  184. package/src/pages/functions/buildPlugin.ts +107 -0
  185. package/src/pages/functions/buildWorker.ts +166 -0
  186. package/src/pages/functions/filepath-routing.test.ts +234 -0
  187. package/src/pages/functions/filepath-routing.ts +189 -0
  188. package/src/pages/functions/identifiers.ts +78 -0
  189. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  190. package/src/pages/functions/routes-consolidation.ts +73 -0
  191. package/src/pages/functions/routes-transformation.test.ts +282 -0
  192. package/src/pages/functions/routes-transformation.ts +115 -0
  193. package/src/pages/functions/routes-validation.test.ts +403 -0
  194. package/src/pages/functions/routes-validation.ts +202 -0
  195. package/src/pages/functions/routes.ts +151 -0
  196. package/src/pages/functions/tsconfig.json +8 -0
  197. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  198. package/src/pages/functions.ts +82 -0
  199. package/src/pages/hash.ts +13 -0
  200. package/src/pages/index.ts +102 -0
  201. package/src/pages/projects.tsx +167 -0
  202. package/src/pages/prompt-select-project.tsx +31 -0
  203. package/src/pages/publish.tsx +462 -0
  204. package/src/pages/types.ts +41 -0
  205. package/src/pages/upload.tsx +447 -0
  206. package/src/pages/utils.ts +23 -0
  207. package/src/parse.ts +286 -0
  208. package/src/paths.ts +71 -0
  209. package/src/proxy.ts +694 -0
  210. package/src/publish/index.ts +270 -0
  211. package/src/publish/publish.ts +1038 -0
  212. package/src/pubsub/index.ts +286 -0
  213. package/src/pubsub/pubsub-commands.ts +624 -0
  214. package/src/queues/cli/commands/consumer/add.ts +71 -0
  215. package/src/queues/cli/commands/consumer/index.ts +22 -0
  216. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  217. package/src/queues/cli/commands/create.ts +25 -0
  218. package/src/queues/cli/commands/delete.ts +26 -0
  219. package/src/queues/cli/commands/index.ts +36 -0
  220. package/src/queues/cli/commands/list.ts +25 -0
  221. package/src/queues/client.ts +135 -0
  222. package/src/queues/utils.ts +18 -0
  223. package/src/r2/constants.ts +4 -0
  224. package/src/r2/helpers.ts +131 -0
  225. package/src/r2/index.ts +289 -0
  226. package/src/routes.ts +140 -0
  227. package/src/secret/index.ts +379 -0
  228. package/src/selfsigned.d.ts +29 -0
  229. package/src/sites.ts +372 -0
  230. package/src/tail/createTail.ts +379 -0
  231. package/src/tail/filters.ts +277 -0
  232. package/src/tail/index.ts +213 -0
  233. package/src/tail/printing.ts +107 -0
  234. package/src/tsconfig-sanity.ts +16 -0
  235. package/src/type-generation.ts +181 -0
  236. package/src/update-check.ts +19 -0
  237. package/src/user/access.ts +68 -0
  238. package/src/user/auth-variables.ts +113 -0
  239. package/src/user/choose-account.tsx +39 -0
  240. package/src/user/generate-auth-url.ts +33 -0
  241. package/src/user/generate-random-state.ts +16 -0
  242. package/src/user/index.ts +2 -0
  243. package/src/user/user.ts +1232 -0
  244. package/src/utils/collectKeyValues.ts +14 -0
  245. package/src/whoami.ts +135 -0
  246. package/src/worker.ts +251 -0
  247. package/src/yargs-types.ts +24 -0
  248. package/src/zones.ts +191 -0
  249. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  250. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  251. package/templates/__tests__/tsconfig.json +8 -0
  252. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  253. package/templates/checked-fetch.js +17 -0
  254. package/templates/d1-beta-facade.js +241 -0
  255. package/templates/facade.d.ts +14 -0
  256. package/templates/first-party-worker-module-facade.ts +19 -0
  257. package/templates/format-dev-errors.ts +33 -0
  258. package/templates/gitignore +171 -0
  259. package/templates/init-tests/test-jest-new-worker.js +23 -0
  260. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  261. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  262. package/templates/middleware/common.ts +62 -0
  263. package/templates/middleware/loader-modules.ts +82 -0
  264. package/templates/middleware/loader-sw.ts +233 -0
  265. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  266. package/templates/middleware/middleware-pretty-error.ts +40 -0
  267. package/templates/middleware/middleware-scheduled.ts +15 -0
  268. package/templates/new-worker-scheduled.js +17 -0
  269. package/templates/new-worker-scheduled.ts +32 -0
  270. package/templates/new-worker.js +15 -0
  271. package/templates/new-worker.ts +30 -0
  272. package/templates/no-op-worker.js +10 -0
  273. package/templates/pages-dev-pipeline.ts +31 -0
  274. package/templates/pages-dev-util.ts +52 -0
  275. package/templates/pages-shim.ts +12 -0
  276. package/templates/pages-template-plugin.ts +176 -0
  277. package/templates/pages-template-worker.ts +189 -0
  278. package/templates/serve-static-assets.ts +65 -0
  279. package/templates/service-bindings-module-facade.js +54 -0
  280. package/templates/service-bindings-sw-facade.js +42 -0
  281. package/templates/tsconfig-sanity.ts +11 -0
  282. package/templates/tsconfig.init.json +106 -0
  283. package/templates/tsconfig.json +8 -0
  284. package/templates/tsconfig.tsbuildinfo +1 -0
  285. package/wrangler-dist/cli.d.ts +906 -0
  286. package/wrangler-dist/cli.js +133533 -113146
  287. package/wrangler-dist/wasm-sync.wasm +0 -0
  288. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  289. package/src/__tests__/mock-cfetch.js +0 -46
  290. package/src/api/form_data.ts +0 -141
  291. package/src/api/inspect.ts +0 -441
  292. package/src/api/preview.ts +0 -128
  293. package/src/api/worker.ts +0 -161
  294. package/src/cfetch.ts +0 -72
  295. package/src/config.ts +0 -122
  296. package/src/dialogs.tsx +0 -85
  297. package/src/index.tsx +0 -1858
  298. package/src/kv.tsx +0 -211
  299. package/src/pages.tsx +0 -843
  300. package/src/publish.ts +0 -346
  301. package/src/sites.tsx +0 -115
  302. package/src/tail.tsx +0 -71
  303. package/src/user.tsx +0 -1029
  304. package/static-asset-facade.js +0 -47
  305. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  306. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  307. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  308. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  309. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  310. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  311. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  312. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  313. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  314. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  315. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  316. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  317. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  318. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  319. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  320. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  321. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  322. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  323. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  324. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  325. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  326. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  327. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  328. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  329. package/vendor/wrangler-mime/LICENSE +0 -21
  330. package/vendor/wrangler-mime/Mime.js +0 -97
  331. package/vendor/wrangler-mime/README.md +0 -187
  332. package/vendor/wrangler-mime/cli.js +0 -46
  333. package/vendor/wrangler-mime/index.js +0 -4
  334. package/vendor/wrangler-mime/lite.js +0 -4
  335. package/vendor/wrangler-mime/package.json +0 -52
  336. package/vendor/wrangler-mime/types/other.js +0 -1
  337. package/vendor/wrangler-mime/types/standard.js +0 -1
  338. package/wrangler-dist/cli.js.map +0 -7
package/src/index.tsx DELETED
@@ -1,1858 +0,0 @@
1
- import React from "react";
2
- import { render } from "ink";
3
- import { Dev } from "./dev";
4
- import { readFile } from "node:fs/promises";
5
- import makeCLI from "yargs";
6
- import { hideBin } from "yargs/helpers";
7
- import type yargs from "yargs";
8
- import { findUp } from "find-up";
9
- import TOML from "@iarna/toml";
10
- import type { Config } from "./config";
11
- import { confirm, prompt } from "./dialogs";
12
- import { version as wranglerVersion } from "../package.json";
13
- import {
14
- login,
15
- logout,
16
- listScopes,
17
- initialise as initialiseUserConfig,
18
- loginOrRefreshIfRequired,
19
- getAccountId,
20
- } from "./user";
21
- import {
22
- getNamespaceId,
23
- listNamespaces,
24
- listNamespaceKeys,
25
- putKeyValue,
26
- putBulkKeyValue,
27
- deleteBulkKeyValue,
28
- } from "./kv";
29
-
30
- import { pages } from "./pages";
31
-
32
- import cfetch from "./cfetch";
33
-
34
- import publish from "./publish";
35
- import path from "path/posix";
36
- import { writeFile } from "node:fs/promises";
37
- import { toFormData } from "./api/form_data";
38
-
39
- import { createTail } from "./tail";
40
- import onExit from "signal-exit";
41
- import { setTimeout } from "node:timers/promises";
42
- import * as fs from "node:fs";
43
- import { execa } from "execa";
44
-
45
- async function readConfig(path?: string): Promise<Config> {
46
- const config: Config = {};
47
- if (!path) {
48
- path = await findUp("wrangler.toml");
49
- // TODO - terminate this early instead of going all the way to the root
50
- }
51
-
52
- if (path) {
53
- const tml: string = await readFile(path, "utf-8");
54
- const parsed = TOML.parse(tml) as Config;
55
- Object.assign(config, parsed);
56
- }
57
-
58
- const inheritedFields = [
59
- "name",
60
- "account_id",
61
- "workers_dev",
62
- "compatibility_date",
63
- "compatibility_flags",
64
- "zone_id",
65
- "routes",
66
- "route",
67
- "jsx_factory",
68
- "jsx_fragment",
69
- "site",
70
- "triggers",
71
- "usage_model",
72
- ];
73
-
74
- Object.keys(config.env || {}).forEach((env) => {
75
- inheritedFields.forEach((field) => {
76
- if (config[field] !== undefined && config.env[env][field] === undefined) {
77
- config.env[env][field] = config[field]; // TODO: - shallow copy?
78
- }
79
- });
80
- });
81
-
82
- const mirroredFields = ["vars", "kv_namespaces", "durable_objects"];
83
- Object.keys(config.env || {}).forEach((env) => {
84
- mirroredFields.forEach((field) => {
85
- // if it exists on top level, it should exist on env defns
86
- Object.keys(config[field] || {}).forEach((fieldKey) => {
87
- if (!(fieldKey in config.env[env][field])) {
88
- console.error(
89
- `In your configuration, "${field}.${fieldKey}" exists at a top level, but not on "env.${env}". This is not what you probably want, since the field "${field}" is not inherited by environments. Please add "${field}.${fieldKey}" to "env.${env}".`
90
- );
91
- }
92
- });
93
- });
94
- });
95
-
96
- // todo: validate, add defaults
97
- // let's just do some basics for now
98
-
99
- // @ts-expect-error we're being sneaky here for now
100
- config.__path__ = path;
101
-
102
- return config;
103
- }
104
-
105
- // a helper to demand one of a set of options
106
- // via https://github.com/yargs/yargs/issues/1093#issuecomment-491299261
107
- function demandOneOfOption(...options: string[]) {
108
- return function (argv: yargs.Arguments) {
109
- const count = options.filter((option) => argv[option]).length;
110
- const lastOption = options.pop();
111
-
112
- if (count === 0) {
113
- throw new Error(
114
- `Exactly one of the arguments ${options.join(
115
- ", "
116
- )} and ${lastOption} is required`
117
- );
118
- } else if (count > 1) {
119
- throw new Error(
120
- `Arguments ${options.join(
121
- ", "
122
- )} and ${lastOption} are mutually exclusive`
123
- );
124
- }
125
-
126
- return true;
127
- };
128
- }
129
-
130
- export async function main(argv: string[]): Promise<void> {
131
- const yargs = makeCLI(hideBin(process.argv))
132
- .command(
133
- // the default is to simply print the help menu
134
- ["*"],
135
- false,
136
- () => {},
137
- () => {
138
- yargs.showHelp("log");
139
- }
140
- )
141
- .scriptName("wrangler")
142
- .wrap(null);
143
-
144
- // you will note that we use the form for all commands where we use the builder function
145
- // to define options and subcommands. Further we return the result of this builder even
146
- // tho it's not completely necessary. The reason is that it's required for type inference
147
- // of the args in the handle function.I wish we could enforce this pattern, but this
148
- // comment will have to do for now.
149
-
150
- // also annoying that choices[] doesn't get inferred as an enum. bleh.
151
-
152
- // [DEPRECATED] generate
153
- yargs.command(
154
- // we definitely want to move away from us cloning github templates
155
- // we can do something better here, let's see
156
- "generate [name] [template]",
157
- false,
158
- (yargs) => {
159
- return yargs
160
- .positional("name", {
161
- describe: "Name of the Workers project",
162
- default: "worker",
163
- })
164
- .positional("template", {
165
- describe: "a link to a GitHub template",
166
- default: "https://github.com/cloudflare/worker-template",
167
- });
168
- },
169
- () => {
170
- // "👯 [DEPRECATED]. Scaffold a Cloudflare Workers project from a public GitHub repository.",
171
- console.error(
172
- "`wrangler generate` has been deprecated, please refer to TODO://some/path for alternatives"
173
- );
174
- }
175
- );
176
-
177
- // init
178
- yargs.command(
179
- "init [name]",
180
- "📥 Create a wrangler.toml configuration file",
181
- (yargs) => {
182
- return yargs.positional("name", {
183
- describe: "The name of your worker.",
184
- type: "string",
185
- });
186
- },
187
- async () => {
188
- const destination = path.join(process.cwd(), "wrangler.toml");
189
- if (fs.existsSync(destination)) {
190
- console.error(`${destination} already exists.`);
191
- } else {
192
- const compatibilityDate = new Date().toISOString().substring(0, 10);
193
- try {
194
- await writeFile(
195
- destination,
196
- `compatibility_date = "${compatibilityDate}"` + "\n"
197
- );
198
- console.log(`✨ Succesfully created wrangler.toml`);
199
- // TODO: suggest next steps?
200
- } catch (err) {
201
- console.error(`Failed to create wrangler.toml`);
202
- console.error(err);
203
- throw err;
204
- }
205
- }
206
-
207
- // if no package.json, ask, and if yes, create one
208
- let pathToPackageJson = await findUp("package.json");
209
-
210
- if (!pathToPackageJson) {
211
- if (
212
- await confirm("No package.json found. Would you like to create one?")
213
- ) {
214
- await writeFile(
215
- path.join(process.cwd(), "package.json"),
216
- JSON.stringify(
217
- {
218
- name: "worker",
219
- version: "0.0.1",
220
- },
221
- null,
222
- " "
223
- ) + "\n"
224
- );
225
- console.log(`✨ Created package.json`);
226
- pathToPackageJson = path.join(process.cwd(), "package.json");
227
- } else {
228
- return;
229
- }
230
- }
231
-
232
- // if workers-types doesn't exist as a dependency
233
- // offer to install it
234
- // and make a tsconfig?
235
- let pathToTSConfig = await findUp("tsconfig.json");
236
- if (!pathToTSConfig) {
237
- if (await confirm("Would you like to use typescript?")) {
238
- await writeFile(
239
- path.join(process.cwd(), "tsconfig.json"),
240
- JSON.stringify(
241
- {
242
- compilerOptions: {
243
- target: "ES2020",
244
- module: "CommonJS",
245
- lib: ["ES2020"],
246
- types: ["@cloudflare/workers-types"],
247
- },
248
- },
249
- null,
250
- " "
251
- ) + "\n"
252
- );
253
- await execa("npm", [
254
- "install",
255
- "@cloudflare/workers-types",
256
- "--save-dev",
257
- ]);
258
- console.log(
259
- `✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies`
260
- );
261
- pathToTSConfig = path.join(process.cwd(), "tsconfig.json");
262
- }
263
- }
264
- }
265
- );
266
-
267
- // build
268
- yargs.command(
269
- "build",
270
- false,
271
- (yargs) => {
272
- return yargs.option("env", {
273
- describe: "Perform on a specific environment",
274
- });
275
- },
276
- () => {
277
- // "[DEPRECATED] 🦀 Build your project (if applicable)",
278
- console.error(
279
- "`wrangler build` has been deprecated, please refer to TODO://some/path for alternatives"
280
- );
281
- }
282
- );
283
-
284
- // login
285
- yargs.command(
286
- // this needs scopes as an option?
287
- "login",
288
- false, // we don't need to show this in the menu
289
- // "🔓 Login to Cloudflare",
290
- (yargs) => {
291
- // TODO: This needs some copy editing
292
- // I mean, this entire app does, but this too.
293
- return yargs
294
- .option("scopes-list", {
295
- describe: "list all the available OAuth scopes with descriptions.",
296
- })
297
- .option("scopes", {
298
- describe: "allows to choose your set of OAuth scopes.",
299
- array: true,
300
- type: "string",
301
- });
302
-
303
- // TODO: scopes
304
- },
305
- async (args) => {
306
- if (args["scopes-list"]) {
307
- listScopes();
308
- return;
309
- }
310
- if (args.scopes) {
311
- if (args.scopes.length === 0) {
312
- // don't allow no scopes to be passed, that would be weird
313
- listScopes();
314
- return;
315
- }
316
- await login({ scopes: args.scopes });
317
- return;
318
- }
319
- await login();
320
-
321
- // TODO: would be nice if it optionally saved login
322
- // creds inside node_modules/.cache or something
323
- // this way you could have multiple users on a single machine
324
- }
325
- );
326
-
327
- // logout
328
- yargs.command(
329
- // this needs scopes as an option?
330
- "logout",
331
- false, // we don't need to show this in the menu
332
- // "🚪 Logout from Cloudflare",
333
- () => {},
334
- async () => {
335
- await logout();
336
- }
337
- );
338
-
339
- // whoami
340
- yargs.command(
341
- "whoami",
342
- false, // we don't need to show this the menu
343
- // "🕵️ Retrieve your user info and test your auth config",
344
- () => {},
345
- (args) => {
346
- console.log(":whoami", args);
347
- }
348
- );
349
-
350
- // config
351
- yargs.command(
352
- "config",
353
- false,
354
- () => {},
355
- () => {
356
- // "🕵️ Authenticate Wrangler with a Cloudflare API Token",
357
- console.error(
358
- "`wrangler config` has been deprecated, please refer to TODO://some/path for alternatives"
359
- );
360
- }
361
- );
362
-
363
- // dev
364
- yargs.command(
365
- "dev <filename>",
366
- "👂 Start a local server for developing your worker",
367
- (yargs) => {
368
- return yargs
369
- .positional("filename", { describe: "entry point", type: "string" })
370
- .option("name", {
371
- describe: "name of the script",
372
- type: "string",
373
- })
374
- .option("format", {
375
- choices: ["modules", "service-worker"] as const,
376
- describe: "Choose an entry type",
377
- })
378
- .option("env", {
379
- describe: "Perform on a specific environment",
380
- type: "string",
381
- // TODO: get choices for the toml file?
382
- })
383
- .option("ip", {
384
- describe: "IP address to listen on",
385
- type: "string",
386
- default: "127.0.0.1",
387
- })
388
- .option("port", {
389
- describe: "Port to listen on, defaults to 8787",
390
- type: "number",
391
- default: 8787,
392
- })
393
- .option("host", {
394
- type: "string",
395
- describe:
396
- "Host to forward requests to, defaults to the zone of project",
397
- })
398
- .option("local-protocol", {
399
- default: "http",
400
- describe: "Protocol to listen to requests on, defaults to http.",
401
- choices: ["http", "https"],
402
- })
403
- .option("public", {
404
- describe: "Static assets to be served",
405
- type: "string",
406
- })
407
- .option("site", {
408
- describe: "Root folder of static assets for Workers Sites",
409
- type: "string",
410
- })
411
- .option("upstream-protocol", {
412
- default: "https",
413
- describe:
414
- "Protocol to forward requests to host on, defaults to https.",
415
- choices: ["http", "https"],
416
- })
417
- .option("jsx-factory", {
418
- describe: "The function that is called for each JSX element",
419
- type: "string",
420
- })
421
- .option("jsx-fragment", {
422
- describe: "The function that is called for each JSX fragment",
423
- type: "string",
424
- });
425
- },
426
- async (args) => {
427
- const { filename, format } = args;
428
- const config = args.config as Config;
429
-
430
- // -- snip, extract --
431
-
432
- if (!args.local) {
433
- const loggedIn = await loginOrRefreshIfRequired();
434
- if (!loggedIn) {
435
- // didn't login, let's just quit
436
- console.log("Did not login, quitting...");
437
- return;
438
- }
439
- if (!config.account_id) {
440
- config.account_id = await getAccountId();
441
- if (!config.account_id) {
442
- console.error("No account id found, quitting...");
443
- return;
444
- }
445
- }
446
- }
447
-
448
- // -- snip, end --
449
-
450
- const envRootObj = args.env ? config.env[args.env] || {} : config;
451
-
452
- // TODO: this error shouldn't actually happen,
453
- // but we haven't fixed it internally yet
454
- if ("durable_objects" in envRootObj) {
455
- if (!(args.name || config.name)) {
456
- console.warn(
457
- 'A worker with durable objects need to be named, or it may not work as expected. Add a "name" into wrangler.toml, or pass it in the command line with --name.'
458
- );
459
- }
460
- // TODO: if not already published, publish a draft worker
461
- }
462
-
463
- render(
464
- <Dev
465
- name={args.name || config.name}
466
- entry={filename}
467
- format={format}
468
- initialMode={args.local ? "local" : "remote"}
469
- jsxFactory={args["jsx-factory"] || envRootObj?.jsx_factory}
470
- jsxFragment={args["jsx-fragment"] || envRootObj?.jsx_fragment}
471
- accountId={config.account_id}
472
- site={args.site || config.site?.bucket}
473
- port={args.port || config.dev?.port}
474
- public={args.public}
475
- compatibilityDate={config.compatibility_date}
476
- compatibilityFlags={config.compatibility_flags}
477
- usageModel={config.usage_model}
478
- variables={{
479
- ...(envRootObj?.vars || {}),
480
- ...(envRootObj?.kv_namespaces || []).reduce(
481
- (obj, { binding, preview_id }) => {
482
- if (!preview_id) {
483
- // TODO: This error has to be a _lot_ better, ideally just asking
484
- // to create a preview namespace for the user automatically
485
- throw new Error(
486
- "kv namespaces need a preview id during dev mode"
487
- );
488
- }
489
- return { ...obj, [binding]: { namespaceId: preview_id } };
490
- },
491
- {}
492
- ),
493
- ...(envRootObj?.durable_objects?.bindings || []).reduce(
494
- (obj, { name, class_name, script_name }) => {
495
- return { ...obj, [name]: { class_name, script_name } };
496
- },
497
- {}
498
- ),
499
- }}
500
- />
501
- );
502
- }
503
- );
504
-
505
- // publish
506
- yargs.command(
507
- "publish [script]",
508
- "🆙 Publish your Worker to Cloudflare.",
509
- (yargs) => {
510
- return yargs
511
- .option("env", {
512
- type: "string",
513
- describe: "Perform on a specific environment",
514
- })
515
- .positional("script", {
516
- describe: "script to upload",
517
- type: "string",
518
- })
519
- .option("name", {
520
- describe: "name to use when uploading",
521
- type: "string",
522
- })
523
- .option("public", {
524
- describe: "Static assets to be served",
525
- type: "string",
526
- })
527
- .option("site", {
528
- describe: "Root folder of static assets for Workers Sites",
529
- type: "string",
530
- })
531
- .option("triggers", {
532
- describe: "cron schedules to attach",
533
- alias: ["schedule", "schedules"],
534
- type: "array",
535
- })
536
- .option("routes", {
537
- describe: "routes to upload",
538
- alias: "route",
539
- type: "array",
540
- })
541
- .option("services", {
542
- describe: "experimental support for services",
543
- type: "boolean",
544
- default: "false",
545
- hidden: true,
546
- })
547
- .option("jsx-factory", {
548
- describe: "The function that is called for each JSX element",
549
- type: "string",
550
- })
551
- .option("jsx-fragment", {
552
- describe: "The function that is called for each JSX fragment",
553
- type: "string",
554
- });
555
- },
556
- async (args) => {
557
- if (args.local) {
558
- console.error("🚫 Local publishing is not yet supported");
559
- return;
560
- }
561
- const config = args.config as Config;
562
-
563
- // -- snip, extract --
564
- if (!args.local) {
565
- const loggedIn = await loginOrRefreshIfRequired();
566
- if (!loggedIn) {
567
- // didn't login, let's just quit
568
- console.log("Did not login, quitting...");
569
- return;
570
- }
571
- if (!config.account_id) {
572
- config.account_id = await getAccountId();
573
- if (!config.account_id) {
574
- console.error("No account id found, quitting...");
575
- return;
576
- }
577
- }
578
- }
579
-
580
- // -- snip, end --
581
-
582
- await publish({
583
- config: args.config as Config,
584
- name: args.name,
585
- script: args.script,
586
- env: args.env,
587
- triggers: args.triggers,
588
- jsxFactory: args["jsx-factory"],
589
- jsxFragment: args["jsx-fragment"],
590
- routes: args.routes,
591
- public: args.public,
592
- site: args.site,
593
- });
594
- }
595
- );
596
-
597
- // tail
598
- yargs.command(
599
- "tail [name]",
600
- "🦚 Starts a log tailing session for a deployed Worker.",
601
- (yargs) => {
602
- return (
603
- yargs
604
- .positional("name", {
605
- describe: "name of the worker",
606
- type: "string",
607
- })
608
- // TODO: auto-detect if this should be json or pretty based on atty
609
- .option("format", {
610
- default: "json",
611
- choices: ["json", "pretty"],
612
- describe: "The format of log entries",
613
- })
614
- .option("status", {
615
- choices: ["ok", "error", "canceled"],
616
- describe: "Filter by invocation status",
617
- })
618
- .option("header", {
619
- type: "string",
620
- describe: "Filter by HTTP header",
621
- })
622
- .option("method", {
623
- type: "string",
624
- describe: "Filter by HTTP method",
625
- })
626
- .option("sampling-rate", {
627
- type: "number",
628
- describe: "Adds a percentage of requests to log sampling rate",
629
- })
630
- .option("search", {
631
- type: "string",
632
- describe: "Filter by a text match in console.log messages",
633
- })
634
- .option("env", {
635
- type: "string",
636
- describe: "Perform on a specific environment",
637
- })
638
- );
639
- // TODO: filter by client ip, which can be 'self' or an ip address
640
- },
641
- async (args) => {
642
- const config = args.config as Config;
643
-
644
- if (!(args.name || config.name)) {
645
- console.error("Missing script name");
646
- return;
647
- }
648
- const scriptName = `${args.name || config.name}${
649
- args.env ? `-${args.env}` : ""
650
- }`;
651
-
652
- // -- snip, extract --
653
-
654
- if (!args.local) {
655
- const loggedIn = await loginOrRefreshIfRequired();
656
- if (!loggedIn) {
657
- // didn't login, let's just quit
658
- console.log("Did not login, quitting...");
659
- return;
660
- }
661
- if (!config.account_id) {
662
- config.account_id = await getAccountId();
663
- if (!config.account_id) {
664
- console.error("No account id found, quitting...");
665
- return;
666
- }
667
- }
668
- }
669
-
670
- // -- snip, end --
671
-
672
- const accountId = config.account_id;
673
-
674
- const filters = {
675
- status: args.status as "ok" | "error" | "canceled",
676
- header: args.header,
677
- method: args.method,
678
- "sampling-rate": args["sampling-rate"],
679
- search: args.search,
680
- };
681
-
682
- const { tail, expiration, /* sendHeartbeat, */ deleteTail } =
683
- await createTail(accountId, scriptName, filters);
684
-
685
- console.log(
686
- `successfully created tail, expires at ${expiration.toLocaleString()}`
687
- );
688
-
689
- onExit(() => {
690
- tail.terminate();
691
- deleteTail();
692
- });
693
-
694
- tail.on("message", (data) => {
695
- console.log(JSON.stringify(JSON.parse(data.toString()), null, " "));
696
- });
697
-
698
- while (tail.readyState !== tail.OPEN) {
699
- switch (tail.readyState) {
700
- case tail.CONNECTING:
701
- await setTimeout(1000);
702
- break;
703
- case tail.CLOSING:
704
- await setTimeout(1000);
705
- break;
706
- case tail.CLOSED:
707
- process.exit(1);
708
- }
709
- }
710
-
711
- console.log(`Connected to ${scriptName}, waiting for logs...`);
712
- }
713
- );
714
-
715
- // preview
716
- yargs.command(
717
- "preview [method] [body]",
718
- false,
719
- (yargs) => {
720
- return yargs
721
- .positional("method", {
722
- describe: "Type of request to preview your worker",
723
- choices: ["GET", "POST"],
724
- default: ["GET"],
725
- })
726
- .positional("body", {
727
- type: "string",
728
- describe: "Body string to post to your preview worker request.",
729
- default: "Null",
730
- })
731
- .option("env", {
732
- type: "string",
733
- describe: "Perform on a specific environment",
734
- })
735
- .option("watch", {
736
- default: true,
737
- describe: "Enable live preview",
738
- type: "boolean",
739
- });
740
- },
741
- () => {
742
- // "🔬 [DEPRECATED] Preview your code temporarily on cloudflareworkers.com"
743
- console.error(
744
- "`wrangler preview` has been deprecated, please refer to TODO://some/path for alternatives"
745
- );
746
- }
747
- );
748
-
749
- // route
750
- yargs.command(
751
- "route",
752
- false, // I think we want to hide this command
753
- // "➡️ List or delete worker routes",
754
- (yargs) => {
755
- return yargs
756
- .command(
757
- "list",
758
- "List a route associated with a zone",
759
- (yargs) => {
760
- return yargs
761
- .option("env", {
762
- type: "string",
763
- describe: "Perform on a specific environment",
764
- })
765
- .option("zone", {
766
- type: "string",
767
- describe: "zone id",
768
- })
769
- .positional("zone", {
770
- describe: "zone id",
771
- type: "string",
772
- });
773
- },
774
- async (args) => {
775
- console.log(":route list", args);
776
- // TODO: use environment (current wrangler doesn't do so?)
777
- const zone = args.zone || (args.config as Config).zone_id;
778
- if (!zone) {
779
- console.error("missing zone id");
780
- return;
781
- }
782
-
783
- console.log(await cfetch(`/zones/${zone}/workers/routes`));
784
- }
785
- )
786
- .command(
787
- "delete <id>",
788
- "Delete a route associated with a zone",
789
- (yargs) => {
790
- return yargs
791
- .positional("id", {
792
- describe: "The hash of the route ID to delete.",
793
- type: "string",
794
- })
795
- .option("zone", {
796
- type: "string",
797
- describe: "zone id",
798
- })
799
- .option("env", {
800
- type: "string",
801
- describe: "Perform on a specific environment",
802
- });
803
- },
804
- async (args) => {
805
- console.log(":route delete", args);
806
- // TODO: use environment (current wrangler doesn't do so?)
807
- const zone = args.zone || (args.config as Config).zone_id;
808
- if (!zone) {
809
- throw new Error("missing zone id");
810
- }
811
-
812
- console.log(
813
- await cfetch(`/zones/${zone}/workers/routes/${args.id}`, {
814
- method: "DELETE",
815
- })
816
- );
817
- }
818
- );
819
- }
820
- );
821
-
822
- // subdomain
823
- yargs.command(
824
- "subdomain [name]",
825
- false,
826
- // "👷 Create or change your workers.dev subdomain.",
827
- (yargs) => {
828
- return yargs.positional("name", { type: "string" });
829
- },
830
- () => {
831
- console.error(
832
- "`wrangler subdomain` has been deprecated, please refer to TODO://some/path for alternatives"
833
- );
834
- }
835
- );
836
-
837
- // secret
838
- yargs.command(
839
- "secret",
840
- "🤫 Generate a secret that can be referenced in the worker script",
841
- (yargs) => {
842
- return yargs
843
- .command(
844
- "put <key>",
845
- "Create or update a secret variable for a script",
846
- (yargs) => {
847
- return yargs
848
- .positional("key", {
849
- describe: "The variable name to be accessible in the script.",
850
- type: "string",
851
- })
852
- .option("name", {
853
- describe: "name of the script",
854
- type: "string",
855
- })
856
- .option("env", {
857
- type: "string",
858
- describe:
859
- "Binds the secret to the script of the specific environment.",
860
- });
861
- },
862
- async (args) => {
863
- if (args.local) {
864
- console.error("--local not implemented for this command yet");
865
- return;
866
- }
867
- const config = args.config as Config;
868
-
869
- // TODO: use environment (how does current wrangler do it?)
870
- const scriptName = args.name || config.name;
871
- if (!scriptName) {
872
- console.error("Missing script name");
873
- return;
874
- }
875
-
876
- // -- snip, extract --
877
-
878
- if (!args.local) {
879
- const loggedIn = await loginOrRefreshIfRequired();
880
- if (!loggedIn) {
881
- // didn't login, let's just quit
882
- console.log("Did not login, quitting...");
883
- return;
884
- }
885
- if (!config.account_id) {
886
- config.account_id = await getAccountId();
887
- if (!config.account_id) {
888
- console.error("No account id found, quitting...");
889
- return;
890
- }
891
- }
892
- }
893
-
894
- // -- snip, end --
895
-
896
- const secretValue = await prompt(
897
- "Enter a secret value:",
898
- "password"
899
- );
900
- async function submitSecret() {
901
- return await cfetch(
902
- `/accounts/${config.account_id}/workers/scripts/${scriptName}/secrets/`,
903
- {
904
- method: "PUT",
905
- headers: { "Content-Type": "application/json" },
906
- body: JSON.stringify({
907
- name: args.key,
908
- text: secretValue,
909
- type: "secret_text",
910
- }),
911
- }
912
- );
913
- }
914
-
915
- try {
916
- console.log(await submitSecret());
917
- } catch (e) {
918
- if (e.code === 10007) {
919
- // upload a draft worker
920
- await cfetch(
921
- `/accounts/${config.account_id}/workers/scripts/${scriptName}`,
922
- {
923
- method: "PUT",
924
- // @ts-expect-error TODO: fix this error!
925
- body: toFormData({
926
- main: {
927
- name: scriptName,
928
- content: `export default { fetch() {} }`,
929
- type: "esm",
930
- },
931
- variables: {},
932
- modules: [],
933
- }),
934
- }
935
- );
936
-
937
- // and then try again
938
- console.log(await submitSecret());
939
- // TODO: delete the draft worker if this failed too?
940
- }
941
- }
942
- }
943
- )
944
- .command(
945
- "delete <key>",
946
- "Delete a secret variable from a script",
947
- (yargs) => {
948
- return yargs
949
- .positional("key", {
950
- describe: "The variable name to be accessible in the script.",
951
- type: "string",
952
- })
953
- .option("name", {
954
- describe: "name of the script",
955
- type: "string",
956
- })
957
- .option("env", {
958
- type: "string",
959
- describe:
960
- "Binds the secret to the script of the specific environment.",
961
- });
962
- },
963
- async (args) => {
964
- if (args.local) {
965
- console.error("--local not implemented for this command yet");
966
- return;
967
- }
968
- const config = args.config as Config;
969
-
970
- // TODO: use environment (how does current wrangler do it?)
971
- const scriptName = args.name || config.name;
972
- if (!scriptName) {
973
- throw new Error("Missing script name");
974
- }
975
-
976
- // -- snip, extract --
977
-
978
- if (!args.local) {
979
- const loggedIn = await loginOrRefreshIfRequired();
980
- if (!loggedIn) {
981
- // didn't login, let's just quit
982
- console.log("Did not login, quitting...");
983
- return;
984
- }
985
- if (!config.account_id) {
986
- config.account_id = await getAccountId();
987
- if (!config.account_id) {
988
- console.error("No account id found, quitting...");
989
- return;
990
- }
991
- }
992
- }
993
-
994
- // -- snip, end --
995
-
996
- if (await confirm("Are you sure you want to delete this secret?")) {
997
- console.log(
998
- `Deleting the secret ${args.key} on script ${scriptName}.`
999
- );
1000
-
1001
- console.log(
1002
- await cfetch(
1003
- `/accounts/${config.account_id}/workers/scripts/${scriptName}/secrets/${args.key}`,
1004
- { method: "DELETE" }
1005
- )
1006
- );
1007
- }
1008
- }
1009
- )
1010
- .command(
1011
- "list",
1012
- "List all secrets for a script",
1013
- (yargs) => {
1014
- return yargs
1015
- .option("name", {
1016
- describe: "name of the script",
1017
- type: "string",
1018
- })
1019
- .option("env", {
1020
- type: "string",
1021
- describe:
1022
- "Binds the secret to the script of the specific environment.",
1023
- });
1024
- },
1025
- async (args) => {
1026
- if (args.local) {
1027
- console.error("--local not implemented for this command yet");
1028
- return;
1029
- }
1030
- const config = args.config as Config;
1031
-
1032
- // TODO: use environment (how does current wrangler do it?)
1033
- const scriptName = args.name || config.name;
1034
- if (!scriptName) {
1035
- console.error("Missing script name");
1036
- return;
1037
- }
1038
-
1039
- // -- snip, extract --
1040
-
1041
- if (!args.local) {
1042
- const loggedIn = await loginOrRefreshIfRequired();
1043
- if (!loggedIn) {
1044
- // didn't login, let's just quit
1045
- console.log("Did not login, quitting...");
1046
- return;
1047
- }
1048
- if (!config.account_id) {
1049
- config.account_id = await getAccountId();
1050
- if (!config.account_id) {
1051
- console.error("No account id found, quitting...");
1052
- return;
1053
- }
1054
- }
1055
- }
1056
-
1057
- // -- snip, end --
1058
-
1059
- console.log(
1060
- await cfetch(
1061
- `/accounts/${config.account_id}/workers/scripts/${scriptName}/secrets`
1062
- )
1063
- );
1064
- }
1065
- );
1066
- }
1067
- );
1068
-
1069
- // kv
1070
- // :namespace
1071
- yargs.command(
1072
- "kv:namespace",
1073
- "🗂️ Interact with your Workers KV Namespaces",
1074
- (yargs) => {
1075
- return yargs
1076
- .command(
1077
- "create <namespace>",
1078
- "Create a new namespace",
1079
- (yargs) => {
1080
- return yargs
1081
- .positional("namespace", {
1082
- describe: "The name of the new namespace",
1083
- type: "string",
1084
- })
1085
- .option("env", {
1086
- type: "string",
1087
- describe: "Perform on a specific environment",
1088
- })
1089
- .option("preview", {
1090
- type: "boolean",
1091
- describe: "Interact with a preview namespace",
1092
- });
1093
- },
1094
- async (args) => {
1095
- if (args._.length !== 2) {
1096
- throw new Error(
1097
- `Did you forget to add quotes around "${
1098
- args.namespace
1099
- } ${args._.slice(2).join(" ")}"?`
1100
- );
1101
- }
1102
- const config = args.config as Config;
1103
- if (!config.name) {
1104
- console.warn(
1105
- "No configured name present, using `worker` as a prefix for the title"
1106
- );
1107
- }
1108
-
1109
- const title = `${config.name || "worker"}${
1110
- args.env ? `-${args.env}` : ""
1111
- }-${args.namespace}${args.preview ? "_preview" : ""}`;
1112
-
1113
- if (/[\W]+/.test(args.namespace)) {
1114
- throw new Error("invalid binding name, needs to be js friendly");
1115
- }
1116
-
1117
- if (args.local) {
1118
- const { Miniflare } = await import("miniflare");
1119
- const mf = new Miniflare({
1120
- kvPersist: (args.kvPersist as string) || true,
1121
- // TODO: these options shouldn't be required
1122
- script: ` `, // has to be a string with at least one char
1123
- });
1124
- mf.getKVNamespace(title); // this should "create" the namespace
1125
- console.log(`✨ Success! Created KV namespace ${title}`);
1126
- return;
1127
- }
1128
-
1129
- // -- snip, extract --
1130
-
1131
- if (!args.local) {
1132
- const loggedIn = await loginOrRefreshIfRequired();
1133
- if (!loggedIn) {
1134
- // didn't login, let's just quit
1135
- console.log("Did not login, quitting...");
1136
- return;
1137
- }
1138
- if (!config.account_id) {
1139
- config.account_id = await getAccountId();
1140
- if (!config.account_id) {
1141
- console.error("No account id found, quitting...");
1142
- return;
1143
- }
1144
- }
1145
- }
1146
-
1147
- // -- snip, end --
1148
-
1149
- // TODO: generate a binding name stripping non alphanumeric chars
1150
-
1151
- console.log(`🌀 Creating namespace with title "${title}"`);
1152
-
1153
- const response = await cfetch<{ id: string }>(
1154
- `/accounts/${config.account_id}/storage/kv/namespaces`,
1155
- {
1156
- method: "POST",
1157
- headers: {
1158
- "Content-Type": "application/json",
1159
- },
1160
- body: JSON.stringify({
1161
- title,
1162
- }),
1163
- }
1164
- );
1165
-
1166
- console.log("✨ Success!");
1167
- console.log(
1168
- `Add the following to your configuration file in your kv_namespaces array${
1169
- args.env ? ` under [env.${args.env}]` : ""
1170
- }:`
1171
- );
1172
- console.log(
1173
- `{ binding = "${args.namespace}", ${
1174
- args.preview ? "preview_" : ""
1175
- }id = "${response.id}" }`
1176
- );
1177
- }
1178
- )
1179
- .command(
1180
- "list",
1181
- "Outputs a list of all KV namespaces associated with your account id.",
1182
- {},
1183
- async (args) => {
1184
- if (args.local) {
1185
- console.error(`local mode is not yet supported for this command`);
1186
- return;
1187
- }
1188
-
1189
- const config = args.config as Config;
1190
-
1191
- // -- snip, extract --
1192
-
1193
- if (!args.local) {
1194
- const loggedIn = await loginOrRefreshIfRequired();
1195
- if (!loggedIn) {
1196
- // didn't login, let's just quit
1197
- console.log("Did not login, quitting...");
1198
- return;
1199
- }
1200
- if (!config.account_id) {
1201
- config.account_id = await getAccountId();
1202
- if (!config.account_id) {
1203
- console.error("No account id found, quitting...");
1204
- return;
1205
- }
1206
- }
1207
- }
1208
-
1209
- // -- snip, end --
1210
-
1211
- // TODO: we should show bindings if they exist for given ids
1212
-
1213
- console.log(
1214
- JSON.stringify(
1215
- await listNamespaces(config.account_id),
1216
- null,
1217
- " "
1218
- )
1219
- );
1220
- }
1221
- )
1222
- .command(
1223
- "delete",
1224
- "Deletes a given namespace.",
1225
- (yargs) => {
1226
- return yargs
1227
- .option("binding", {
1228
- type: "string",
1229
- describe: "The name of the namespace to delete",
1230
- })
1231
- .option("namespace-id", {
1232
- type: "string",
1233
- describe: "The id of the namespace to delete",
1234
- })
1235
- .check(demandOneOfOption("binding", "namespace-id"))
1236
- .option("env", {
1237
- type: "string",
1238
- describe: "Perform on a specific environment",
1239
- })
1240
- .option("preview", {
1241
- type: "boolean",
1242
- describe: "Interact with a preview namespace",
1243
- });
1244
- },
1245
- async (args) => {
1246
- if (args.local) {
1247
- console.error(`local mode is not yet supported for this command`);
1248
- return;
1249
- }
1250
- const config = args.config as Config;
1251
-
1252
- const id =
1253
- args["namespace-id"] ||
1254
- (args.env
1255
- ? config.env[args.env] || {}
1256
- : config
1257
- ).kv_namespaces.find(
1258
- (namespace) => namespace.binding === args.binding
1259
- )[args.preview ? "preview_id" : "id"];
1260
- if (!id) {
1261
- throw new Error("Are you sure? id not found");
1262
- }
1263
-
1264
- // -- snip, extract --
1265
-
1266
- if (!args.local) {
1267
- const loggedIn = await loginOrRefreshIfRequired();
1268
- if (!loggedIn) {
1269
- // didn't login, let's just quit
1270
- console.log("Did not login, quitting...");
1271
- return;
1272
- }
1273
-
1274
- if (!config.account_id) {
1275
- config.account_id = await getAccountId();
1276
- if (!config.account_id) {
1277
- console.error("No account id found, quitting...");
1278
- return;
1279
- }
1280
- }
1281
- }
1282
-
1283
- // -- snip, end --
1284
-
1285
- await cfetch<{ id: string }>(
1286
- `/accounts/${config.account_id}/storage/kv/namespaces/${id}`,
1287
- { method: "DELETE" }
1288
- );
1289
-
1290
- // TODO: recommend they remove it from wrangler.toml
1291
-
1292
- // test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
1293
- // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
1294
- // n
1295
- // 💁 Not deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
1296
- // ➜ test-mf wrangler kv:namespace delete --namespace-id 2a7d3d8b23fc4159b5afa489d6cfd388
1297
- // Are you sure you want to delete namespace 2a7d3d8b23fc4159b5afa489d6cfd388? [y/n]
1298
- // y
1299
- // 🌀 Deleting namespace 2a7d3d8b23fc4159b5afa489d6cfd388
1300
- // ✨ Success
1301
- // ⚠️ Make sure to remove this "kv-namespace" entry from your configuration file!
1302
- // ➜ test-mf
1303
-
1304
- // TODO: do it automatically
1305
-
1306
- // TODO: delete the preview namespace as well?
1307
- }
1308
- );
1309
- }
1310
- );
1311
-
1312
- // :key
1313
- yargs.command(
1314
- "kv:key",
1315
- "🔑 Individually manage Workers KV key-value pairs",
1316
- (yargs) => {
1317
- return yargs
1318
- .command(
1319
- "put <key> [value]",
1320
- "Writes a single key/value pair to the given namespace.",
1321
- (yargs) => {
1322
- return yargs
1323
- .positional("key", {
1324
- type: "string",
1325
- describe: "The key to write to.",
1326
- })
1327
- .positional("value", {
1328
- type: "string",
1329
- describe: "The value to write.",
1330
- })
1331
- .option("binding", {
1332
- type: "string",
1333
- describe: "The binding of the namespace to write to.",
1334
- })
1335
- .option("namespace-id", {
1336
- type: "string",
1337
- describe: "The id of the namespace to write to.",
1338
- })
1339
- .check(demandOneOfOption("binding", "namespace-id"))
1340
- .option("env", {
1341
- type: "string",
1342
- describe: "Perform on a specific environment",
1343
- })
1344
- .option("preview", {
1345
- type: "boolean",
1346
- describe: "Interact with a preview namespace",
1347
- })
1348
- .option("ttl", {
1349
- type: "number",
1350
- describe: "Time for which the entries should be visible.",
1351
- })
1352
- .option("expiration", {
1353
- type: "number",
1354
- describe:
1355
- "Time since the UNIX epoch after which the entry expires",
1356
- })
1357
- .option("path", {
1358
- type: "string",
1359
- describe: "Read value from the file at a given path.",
1360
- })
1361
- .check(demandOneOfOption("value", "path"));
1362
- },
1363
- async ({ key, ttl, expiration, ...args }) => {
1364
- const namespaceId = getNamespaceId(args);
1365
- const value = args.path
1366
- ? await readFile(args.path, "utf-8")
1367
- : args.value;
1368
- const config = args.config as Config;
1369
-
1370
- if (args.path) {
1371
- console.log(
1372
- `writing the contents of ${args.path} to the key "${key}" on namespace ${namespaceId}`
1373
- );
1374
- } else {
1375
- console.log(
1376
- `writing the value "${value}" to key "${key}" on namespace ${namespaceId}`
1377
- );
1378
- }
1379
-
1380
- if (args.local) {
1381
- const { Miniflare } = await import("miniflare");
1382
- const mf = new Miniflare({
1383
- kvPersist: (args.kvPersist as string) || true,
1384
- // TODO: these options shouldn't be required
1385
- script: ` `, // has to be a string with at least one char
1386
- });
1387
- const ns = await mf.getKVNamespace(namespaceId);
1388
- await ns.put(key, value, { expiration, expirationTtl: ttl });
1389
- return;
1390
- }
1391
- // -- snip, extract --
1392
-
1393
- if (!args.local) {
1394
- const loggedIn = await loginOrRefreshIfRequired();
1395
- if (!loggedIn) {
1396
- // didn't login, let's just quit
1397
- console.log("Did not login, quitting...");
1398
- return;
1399
- }
1400
-
1401
- if (!config.account_id) {
1402
- config.account_id = await getAccountId();
1403
- if (!config.account_id) {
1404
- console.error("No account id found, quitting...");
1405
- return;
1406
- }
1407
- }
1408
- }
1409
-
1410
- // -- snip, end --
1411
-
1412
- await putKeyValue(config.account_id, namespaceId, key, value, {
1413
- expiration,
1414
- expiration_ttl: ttl,
1415
- });
1416
- }
1417
- )
1418
- .command(
1419
- "list",
1420
- "Outputs a list of all keys in a given namespace.",
1421
- (yargs) => {
1422
- return yargs
1423
- .option("binding", {
1424
- type: "string",
1425
- describe: "The name of the namespace to list",
1426
- })
1427
- .option("namespace-id", {
1428
- type: "string",
1429
- describe: "The id of the namespace to list",
1430
- })
1431
- .check(demandOneOfOption("binding", "namespace-id"))
1432
- .option("env", {
1433
- type: "string",
1434
- describe: "Perform on a specific environment",
1435
- })
1436
- .option("prefix", {
1437
- type: "string",
1438
- describe: "A prefix to filter listed keys",
1439
- });
1440
- },
1441
- async ({ prefix, ...args }) => {
1442
- // TODO: support for limit+cursor (pagination)
1443
-
1444
- const namespaceId = getNamespaceId(args);
1445
- const config = args.config as Config;
1446
-
1447
- if (args.local) {
1448
- const { Miniflare } = await import("miniflare");
1449
- const mf = new Miniflare({
1450
- kvPersist: (args.kvPersist as string) || true,
1451
- // TODO: these options shouldn't be required
1452
- script: ` `, // has to be a string with at least one char
1453
- });
1454
- const ns = await mf.getKVNamespace(namespaceId);
1455
- const listResponse = await ns.list({ prefix });
1456
- console.log(JSON.stringify(listResponse.keys, null, " ")); // TODO: paginate, collate
1457
- return;
1458
- }
1459
-
1460
- // -- snip, extract --
1461
-
1462
- if (!args.local) {
1463
- const loggedIn = await loginOrRefreshIfRequired();
1464
- if (!loggedIn) {
1465
- // didn't login, let's just quit
1466
- console.log("Did not login, quitting...");
1467
- return;
1468
- }
1469
-
1470
- if (!config.account_id) {
1471
- config.account_id = await getAccountId();
1472
- if (!config.account_id) {
1473
- console.error("No account id found, quitting...");
1474
- return;
1475
- }
1476
- }
1477
- }
1478
-
1479
- // -- snip, end --
1480
-
1481
- console.log(
1482
- await listNamespaceKeys(config.account_id, namespaceId, prefix)
1483
- );
1484
- }
1485
- )
1486
- .command(
1487
- "get <key>",
1488
- "Reads a single value by key from the given namespace.",
1489
- (yargs) => {
1490
- return yargs
1491
- .positional("key", {
1492
- describe: "The key value to get.",
1493
- type: "string",
1494
- })
1495
- .option("binding", {
1496
- type: "string",
1497
- describe: "The name of the namespace to get from",
1498
- })
1499
- .option("namespace-id", {
1500
- type: "string",
1501
- describe: "The id of the namespace to get from",
1502
- })
1503
- .check(demandOneOfOption("binding", "namespace-id"))
1504
- .option("env", {
1505
- type: "string",
1506
- describe: "Perform on a specific environment",
1507
- })
1508
- .option("preview", {
1509
- type: "boolean",
1510
- describe: "Interact with a preview namespace",
1511
- });
1512
- },
1513
- async ({ key, ...args }) => {
1514
- const namespaceId = getNamespaceId(args);
1515
- const config = args.config as Config;
1516
-
1517
- if (args.local) {
1518
- const { Miniflare } = await import("miniflare");
1519
- const mf = new Miniflare({
1520
- kvPersist: (args.kvPersist as string) || true,
1521
- // TODO: these options shouldn't be required
1522
- script: ` `, // has to be a string with at least one char
1523
- });
1524
- const ns = await mf.getKVNamespace(namespaceId);
1525
- console.log(await ns.get(key));
1526
- return;
1527
- }
1528
-
1529
- // -- snip, extract --
1530
-
1531
- if (!args.local) {
1532
- const loggedIn = await loginOrRefreshIfRequired();
1533
- if (!loggedIn) {
1534
- // didn't login, let's just quit
1535
- console.log("Did not login, quitting...");
1536
- return;
1537
- }
1538
-
1539
- if (!config.account_id) {
1540
- config.account_id = await getAccountId();
1541
- if (!config.account_id) {
1542
- console.error("No account id found, quitting...");
1543
- return;
1544
- }
1545
- }
1546
- }
1547
-
1548
- // -- snip, end --
1549
-
1550
- // annoyingly, the API for this one doesn't return the
1551
- // data in the 'standard' format. goddamit.
1552
- // That's why we have the fallthrough response in cfetch.
1553
- // Oh well.
1554
- console.log(
1555
- await cfetch(
1556
- `/accounts/${config.account_id}/storage/kv/namespaces/${namespaceId}/values/${key}`
1557
- )
1558
- );
1559
- }
1560
- )
1561
- .command(
1562
- "delete <key>",
1563
- "Removes a single key value pair from the given namespace.",
1564
- (yargs) => {
1565
- return yargs
1566
- .positional("key", {
1567
- describe: "The key value to delete",
1568
- type: "string",
1569
- })
1570
- .option("binding", {
1571
- type: "string",
1572
- describe: "The name of the namespace to delete from",
1573
- })
1574
- .option("namespace-id", {
1575
- type: "string",
1576
- describe: "The id of the namespace to delete from",
1577
- })
1578
- .check(demandOneOfOption("binding", "namespace-id"))
1579
- .option("env", {
1580
- type: "string",
1581
- describe: "Perform on a specific environment",
1582
- })
1583
- .option("preview", {
1584
- type: "boolean",
1585
- describe: "Interact with a preview namespace",
1586
- });
1587
- },
1588
- async ({ key, ...args }) => {
1589
- const namespaceId = getNamespaceId(args);
1590
-
1591
- console.log(
1592
- `deleting the key "${key}" on namespace ${namespaceId}`
1593
- );
1594
-
1595
- if (args.local) {
1596
- const { Miniflare } = await import("miniflare");
1597
- const mf = new Miniflare({
1598
- kvPersist: (args.kvPersist as string) || true,
1599
- // TODO: these options shouldn't be required
1600
- script: ` `, // has to be a string with at least one char
1601
- });
1602
- const ns = await mf.getKVNamespace(namespaceId);
1603
- console.log(await ns.delete(key));
1604
- return;
1605
- }
1606
-
1607
- const config = args.config as Config;
1608
-
1609
- // -- snip, extract --
1610
-
1611
- if (!args.local) {
1612
- const loggedIn = await loginOrRefreshIfRequired();
1613
- if (!loggedIn) {
1614
- // didn't login, let's just quit
1615
- console.log("Did not login, quitting...");
1616
- return;
1617
- }
1618
-
1619
- if (!config.account_id) {
1620
- config.account_id = await getAccountId();
1621
- if (!config.account_id) {
1622
- console.error("No account id found, quitting...");
1623
- return;
1624
- }
1625
- }
1626
- }
1627
-
1628
- // -- snip, end --
1629
-
1630
- await cfetch(
1631
- `/accounts/${config.account_id}/storage/kv/namespaces/${namespaceId}/values/${key}`,
1632
- { method: "DELETE" }
1633
- );
1634
- }
1635
- );
1636
- }
1637
- );
1638
-
1639
- // :bulk
1640
- yargs.command(
1641
- "kv:bulk",
1642
- "💪 Interact with multiple Workers KV key-value pairs at once",
1643
- (yargs) => {
1644
- return yargs
1645
- .command(
1646
- "put <filename>",
1647
- "Upload multiple key-value pairs to a namespace",
1648
- (yargs) => {
1649
- return yargs
1650
- .positional("filename", {
1651
- describe: `The JSON file of key-value pairs to upload, in form [{"key":..., "value":...}"...]`,
1652
- type: "string",
1653
- })
1654
- .option("binding", {
1655
- type: "string",
1656
- describe: "The name of the namespace to put to",
1657
- })
1658
- .option("namespace-id", {
1659
- type: "string",
1660
- describe: "The id of the namespace to put to",
1661
- })
1662
- .check(demandOneOfOption("binding", "namespace-id"))
1663
- .option("env", {
1664
- type: "string",
1665
- describe: "Perform on a specific environment",
1666
- })
1667
- .option("preview", {
1668
- type: "boolean",
1669
- describe: "Interact with a preview namespace",
1670
- });
1671
- },
1672
- async ({ filename, ...args }) => {
1673
- // The simplest implementation I could think of.
1674
- // This could be made more efficient with a streaming parser/uploader
1675
- // but we'll do that in the future if needed.
1676
-
1677
- const namespaceId = getNamespaceId(args);
1678
- const config = args.config as Config;
1679
- const content = await readFile(filename, "utf-8");
1680
- let parsedContent;
1681
- try {
1682
- parsedContent = JSON.parse(content);
1683
- } catch (err) {
1684
- console.error(`could not parse json from ${filename}`);
1685
- throw err;
1686
- }
1687
-
1688
- if (args.local) {
1689
- const { Miniflare } = await import("miniflare");
1690
- const mf = new Miniflare({
1691
- kvPersist: (args.kvPersist as string) || true,
1692
- // TODO: these options shouldn't be required
1693
- script: ` `, // has to be a string with at least one char
1694
- });
1695
- const ns = await mf.getKVNamespace(namespaceId);
1696
- for (const {
1697
- key,
1698
- value,
1699
- expiration,
1700
- expiration_ttl,
1701
- } of parsedContent) {
1702
- await ns.put(key, value, {
1703
- expiration,
1704
- expirationTtl: expiration_ttl,
1705
- });
1706
- }
1707
-
1708
- return;
1709
- }
1710
-
1711
- // -- snip, extract --
1712
-
1713
- if (!args.local) {
1714
- const loggedIn = await loginOrRefreshIfRequired();
1715
- if (!loggedIn) {
1716
- // didn't login, let's just quit
1717
- console.log("Did not login, quitting...");
1718
- return;
1719
- }
1720
-
1721
- if (!config.account_id) {
1722
- config.account_id = await getAccountId();
1723
- if (!config.account_id) {
1724
- console.error("No account id found, quitting...");
1725
- return;
1726
- }
1727
- }
1728
- }
1729
-
1730
- // -- snip, end --
1731
-
1732
- console.log(
1733
- await putBulkKeyValue(config.account_id, namespaceId, content)
1734
- );
1735
- }
1736
- )
1737
- .command(
1738
- "delete <filename>",
1739
- "Upload multiple key-value pairs to a namespace",
1740
- (yargs) => {
1741
- return yargs
1742
- .positional("filename", {
1743
- describe: `The JSON file of key-value pairs to upload, in form ["key1", "key2", ...]`,
1744
- type: "string",
1745
- })
1746
- .option("binding", {
1747
- type: "string",
1748
- describe: "The name of the namespace to delete from",
1749
- })
1750
- .option("namespace-id", {
1751
- type: "string",
1752
- describe: "The id of the namespace to delete from",
1753
- })
1754
- .check(demandOneOfOption("binding", "namespace-id"))
1755
- .option("env", {
1756
- type: "string",
1757
- describe: "Perform on a specific environment",
1758
- })
1759
- .option("preview", {
1760
- type: "boolean",
1761
- describe: "Interact with a preview namespace",
1762
- });
1763
- },
1764
- async ({ filename, ...args }) => {
1765
- const namespaceId = getNamespaceId(args);
1766
- const config = args.config as Config;
1767
- const content = await readFile(filename, "utf-8");
1768
- let parsedContent;
1769
- try {
1770
- parsedContent = JSON.parse(content);
1771
- } catch (err) {
1772
- console.error(`could not parse json from ${filename}`);
1773
- throw err;
1774
- }
1775
-
1776
- if (args.local) {
1777
- const { Miniflare } = await import("miniflare");
1778
- const mf = new Miniflare({
1779
- kvPersist: (args.kvPersist as string) || true,
1780
- // TODO: these options shouldn't be required
1781
- script: ` `, // has to be a string with at least one char
1782
- });
1783
- const ns = await mf.getKVNamespace(namespaceId);
1784
- for (const key of parsedContent) {
1785
- await ns.delete(key);
1786
- }
1787
-
1788
- return;
1789
- }
1790
-
1791
- // -- snip, extract --
1792
-
1793
- if (!args.local) {
1794
- const loggedIn = await loginOrRefreshIfRequired();
1795
- if (!loggedIn) {
1796
- // didn't login, let's just quit
1797
- console.log("Did not login, quitting...");
1798
- return;
1799
- }
1800
-
1801
- if (!config.account_id) {
1802
- config.account_id = await getAccountId();
1803
- if (!config.account_id) {
1804
- console.error("No account id found, quitting...");
1805
- return;
1806
- }
1807
- }
1808
- }
1809
-
1810
- // -- snip, end --
1811
-
1812
- console.log(
1813
- await deleteBulkKeyValue(config.account_id, namespaceId, content)
1814
- );
1815
- }
1816
- );
1817
- }
1818
- );
1819
-
1820
- yargs.command("pages", "⚡️ Configure Cloudflare Pages", pages);
1821
-
1822
- yargs
1823
- .option("config", {
1824
- alias: "c",
1825
- describe: "Path to .toml configuration file",
1826
- type: "string",
1827
- async coerce(arg) {
1828
- return await readConfig(arg);
1829
- },
1830
- })
1831
- .option("local", {
1832
- alias: "l",
1833
- describe: "Run on my machine",
1834
- type: "boolean",
1835
- default: false, // I bet this will a point of contention. We'll revisit it.
1836
- });
1837
-
1838
- yargs.group(["config", "help", "version"], "Flags:");
1839
- yargs.help().alias("h", "help");
1840
- yargs.version(wranglerVersion).alias("v", "version");
1841
- await initialiseUserConfig();
1842
-
1843
- await yargs.parse(
1844
- argv
1845
- /* TODO: we'd like to use the parse callback,
1846
- but there's a serious bug with it
1847
- https://github.com/yargs/yargs/issues/1975#issuecomment-874237906
1848
-
1849
- , (err, argv, output) => {
1850
- if (output) {
1851
- console.log(output);
1852
- }
1853
- console.log({ err });
1854
- console.log("what now");
1855
- }
1856
- */
1857
- );
1858
- }