devflare 1.0.0-next.39 → 1.0.0-next.40

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 (277) hide show
  1. package/dist/_chunks/account-BtWcv3X9.js +462 -0
  2. package/dist/_chunks/account-resources-CsmVowJp.js +159 -0
  3. package/dist/_chunks/ai-CBEuLdoP.js +322 -0
  4. package/dist/_chunks/api-TzdliH-6.js +568 -0
  5. package/dist/_chunks/build-3T6O4uIg.js +24 -0
  6. package/dist/_chunks/build-artifacts-8TqBSQkG.js +483 -0
  7. package/dist/_chunks/cli-BTF1X4SG.js +1128 -0
  8. package/dist/_chunks/cloudflare-BaloqI3H.js +157 -0
  9. package/dist/_chunks/colors-DiK6wPJ5.js +13 -0
  10. package/dist/_chunks/command-utils-BsfkmTqh.js +45 -0
  11. package/dist/_chunks/compiler-uoyAQ8zW.js +424 -0
  12. package/dist/_chunks/config-D5u-bEkn.js +78 -0
  13. package/dist/_chunks/config-path-DCPto4XB.js +42 -0
  14. package/dist/_chunks/context-CX50Y2Kb.js +2250 -0
  15. package/dist/_chunks/decorators-QmV57ixr.js +19 -0
  16. package/dist/_chunks/dependencies-C2oxFjU_.js +57 -0
  17. package/dist/_chunks/deploy-DMTr6JeR.js +915 -0
  18. package/dist/_chunks/dev-CvRHmRFZ.js +3456 -0
  19. package/dist/_chunks/doctor-DIG8DoFU.js +219 -0
  20. package/dist/_chunks/durable-object-DMtH0XYX.js +234 -0
  21. package/dist/_chunks/env-S0_nMVz1.js +125 -0
  22. package/dist/_chunks/family-DXC5SdFK.js +143 -0
  23. package/dist/_chunks/generated-artifacts-dMJY4KAt.js +32 -0
  24. package/dist/_chunks/glob-CmQOvunB.js +56 -0
  25. package/dist/_chunks/init-3moXTgpb.js +165 -0
  26. package/dist/_chunks/loader-DLXO60RO.js +1153 -0
  27. package/dist/_chunks/local-dev-vars-CTSa-wvF.js +81 -0
  28. package/dist/_chunks/local-hyperdrive-CJ90j46O.js +119 -0
  29. package/dist/_chunks/local-media-bindings-DsAW4AMF.js +335 -0
  30. package/dist/_chunks/local-secrets-DMmEOygL.js +123 -0
  31. package/dist/_chunks/local-workflow-entrypoints-Wip3o1u8.js +90 -0
  32. package/dist/_chunks/login-BmnDzDlH.js +51 -0
  33. package/dist/_chunks/package-metadata-DAuCkyvK.js +39 -0
  34. package/dist/_chunks/preferences-BKp_7XJx.js +241 -0
  35. package/dist/_chunks/preview-BhqDmq8I.js +456 -0
  36. package/dist/_chunks/preview-registry-ClAbLTHH.js +1186 -0
  37. package/dist/_chunks/preview-resources-DdXj8LMM.js +358 -0
  38. package/dist/_chunks/previews-Dlr13x5M.js +1134 -0
  39. package/dist/_chunks/productions-D3AnOHac.js +411 -0
  40. package/dist/_chunks/ref-CYJNAAe_.js +178 -0
  41. package/dist/_chunks/remote-CZnP-HLJ.js +79 -0
  42. package/dist/_chunks/remote-config-D39BY9ji.js +132 -0
  43. package/dist/_chunks/resolve-package-Y84HB3Nt.js +133 -0
  44. package/dist/_chunks/rolldown-runtime-CvQ6eiug.js +14 -0
  45. package/dist/_chunks/routes-C3FqES2W.js +796 -0
  46. package/dist/_chunks/runtime-FvXvfuZ2.js +550 -0
  47. package/dist/_chunks/schema-normalization-DWVleVxo.js +477 -0
  48. package/dist/_chunks/secrets-70eN0Q6C.js +95 -0
  49. package/dist/_chunks/token-kguIDmej.js +333 -0
  50. package/dist/_chunks/tokens-iVcgNXi-.js +221 -0
  51. package/dist/_chunks/types-OrLbzv4I.js +476 -0
  52. package/dist/_chunks/ui-BUoZApvE.js +103 -0
  53. package/dist/_chunks/usage-BBTGzjon.js +272 -0
  54. package/dist/_chunks/vite-CsI0E-uV.js +1235 -0
  55. package/dist/_chunks/vite-utils-OVLMV605.js +183 -0
  56. package/dist/_chunks/worker-CS1jgNFE.js +410 -0
  57. package/dist/_chunks/worker-bundler-dTTnYU3n.js +458 -0
  58. package/dist/_chunks/worker-entrypoint-CQW77lG8.js +247 -0
  59. package/dist/_chunks/workerName-CFJsLZA-.js +23 -0
  60. package/dist/bridge/index.d.ts +6 -6
  61. package/dist/bridge/miniflare.d.ts +2 -2
  62. package/dist/bridge/proxy.d.ts +1 -1
  63. package/dist/bridge/v2/body-streams.d.ts +1 -1
  64. package/dist/bridge/v2/codec.d.ts +4 -4
  65. package/dist/bridge/v2/index.d.ts +10 -10
  66. package/dist/bridge/v2/serialization.d.ts +1 -1
  67. package/dist/bridge/v2/value-codec.d.ts +1 -1
  68. package/dist/bridge/v2/value-serialization.d.ts.map +1 -1
  69. package/dist/bridge/v2/ws-relay.d.ts +3 -3
  70. package/dist/browser-shim/index.d.ts +2 -2
  71. package/dist/browser.d.ts +70 -70
  72. package/dist/browser.js +74 -2462
  73. package/dist/bundler/do-bundler.d.ts +1 -1
  74. package/dist/bundler/index.d.ts +3 -3
  75. package/dist/bundler/rolldown-shared.d.ts +1 -1
  76. package/dist/bundler/worker-bundler.d.ts +1 -1
  77. package/dist/cli/build-manifest.d.ts +1 -1
  78. package/dist/cli/command-utils.d.ts +1 -1
  79. package/dist/cli/commands/account.d.ts +1 -1
  80. package/dist/cli/commands/ai.d.ts +1 -1
  81. package/dist/cli/commands/build-artifacts.d.ts +5 -5
  82. package/dist/cli/commands/build.d.ts +1 -1
  83. package/dist/cli/commands/config.d.ts +1 -1
  84. package/dist/cli/commands/deploy/prepare.d.ts +2 -2
  85. package/dist/cli/commands/deploy/runtime.d.ts +1 -1
  86. package/dist/cli/commands/deploy/verification.d.ts +1 -1
  87. package/dist/cli/commands/deploy.d.ts +1 -1
  88. package/dist/cli/commands/dev.d.ts +1 -1
  89. package/dist/cli/commands/doctor.d.ts +1 -1
  90. package/dist/cli/commands/init.d.ts +1 -1
  91. package/dist/cli/commands/login.d.ts +1 -1
  92. package/dist/cli/commands/previews-support/cleanup.d.ts +1 -1
  93. package/dist/cli/commands/previews-support/family.d.ts +3 -3
  94. package/dist/cli/commands/previews-support/render.d.ts +3 -3
  95. package/dist/cli/commands/previews-support/theme.d.ts +2 -2
  96. package/dist/cli/commands/previews-support/types.d.ts +2 -2
  97. package/dist/cli/commands/previews.d.ts +1 -1
  98. package/dist/cli/commands/productions.d.ts +1 -1
  99. package/dist/cli/commands/remote.d.ts +1 -1
  100. package/dist/cli/commands/secrets.d.ts +1 -1
  101. package/dist/cli/commands/token.d.ts +1 -1
  102. package/dist/cli/commands/type-generation/discovery.d.ts +2 -2
  103. package/dist/cli/commands/type-generation/generator.d.ts +3 -3
  104. package/dist/cli/commands/type-generation/models.d.ts +1 -1
  105. package/dist/cli/commands/types.d.ts +1 -1
  106. package/dist/cli/commands/worker.d.ts +1 -1
  107. package/dist/cli/deploy-strategy.d.ts +1 -1
  108. package/dist/cli/deploy-target.d.ts +1 -1
  109. package/dist/cli/help-pages/pages/account.d.ts +1 -1
  110. package/dist/cli/help-pages/pages/core.d.ts +1 -1
  111. package/dist/cli/help-pages/pages/index.d.ts +1 -1
  112. package/dist/cli/help-pages/pages/misc.d.ts +1 -1
  113. package/dist/cli/help-pages/pages/previews.d.ts +1 -1
  114. package/dist/cli/help-pages/pages/productions.d.ts +1 -1
  115. package/dist/cli/help-pages/render.d.ts +2 -2
  116. package/dist/cli/help-pages/shared.d.ts +1 -1
  117. package/dist/cli/help.d.ts +2 -2
  118. package/dist/cli/index.js +2 -1383
  119. package/dist/cli/preview-bindings.d.ts +3 -3
  120. package/dist/cli/preview.d.ts +1 -1
  121. package/dist/cli/wrangler-auth.d.ts +1 -1
  122. package/dist/cloudflare/account-core.d.ts +2 -2
  123. package/dist/cloudflare/account-resources.d.ts +2 -2
  124. package/dist/cloudflare/account-status.d.ts +1 -1
  125. package/dist/cloudflare/account-workers.d.ts +2 -2
  126. package/dist/cloudflare/account.d.ts +7 -7
  127. package/dist/cloudflare/auth.d.ts +1 -1
  128. package/dist/cloudflare/index.d.ts +13 -13
  129. package/dist/cloudflare/index.js +4 -59
  130. package/dist/cloudflare/kv-namespace.d.ts +1 -1
  131. package/dist/cloudflare/preview-registry-cache.d.ts +1 -1
  132. package/dist/cloudflare/preview-registry-inference.d.ts +2 -2
  133. package/dist/cloudflare/preview-registry-records.d.ts +3 -3
  134. package/dist/cloudflare/preview-registry-shape.d.ts +2 -2
  135. package/dist/cloudflare/preview-registry-store.d.ts +3 -3
  136. package/dist/cloudflare/preview-registry-transport.d.ts +2 -2
  137. package/dist/cloudflare/preview-registry-types.d.ts +2 -2
  138. package/dist/cloudflare/preview-registry.d.ts +5 -5
  139. package/dist/cloudflare/tokens.d.ts +2 -2
  140. package/dist/cloudflare/usage.d.ts +2 -2
  141. package/dist/config/binding-resolution-helpers.d.ts +1 -1
  142. package/dist/config/compiler/bindings.d.ts +2 -2
  143. package/dist/config/compiler/core-helpers.d.ts +2 -2
  144. package/dist/config/compiler/do-workers.d.ts +2 -2
  145. package/dist/config/compiler/paths.d.ts +1 -1
  146. package/dist/config/compiler.d.ts +6 -6
  147. package/dist/config/define.d.ts +2 -2
  148. package/dist/config/deploy-resources.d.ts +5 -5
  149. package/dist/config/env-vars.d.ts +1 -1
  150. package/dist/config/framework-providers.d.ts +1 -1
  151. package/dist/config/index.d.ts +12 -12
  152. package/dist/config/loader.d.ts +2 -2
  153. package/dist/config/local-dev-vars.d.ts +1 -1
  154. package/dist/config/preview-resources.d.ts +4 -4
  155. package/dist/config/preview.d.ts +1 -1
  156. package/dist/config/ref.d.ts +2 -2
  157. package/dist/config/resolve-phased.d.ts +4 -4
  158. package/dist/config/resolve.d.ts +1 -1
  159. package/dist/config/resource-resolution.d.ts +6 -6
  160. package/dist/config/schema-env.d.ts +20 -20
  161. package/dist/config/schema-normalization.d.ts +1 -1
  162. package/dist/config/schema-types-bindings.d.ts +4 -4
  163. package/dist/config/schema-types-build.d.ts +1 -1
  164. package/dist/config/schema-types.d.ts +7 -7
  165. package/dist/config/schema.d.ts +46 -46
  166. package/dist/config/service-bindings-validation.d.ts +1 -1
  167. package/dist/config-entry.d.ts +6 -6
  168. package/dist/config-entry.js +3 -8
  169. package/dist/decorators/index.d.ts +2 -2
  170. package/dist/decorators/index.js +2 -9
  171. package/dist/dev-server/d1-migrations.d.ts +1 -1
  172. package/dist/dev-server/dev-server-state.d.ts +6 -6
  173. package/dist/dev-server/gateway-script.d.ts +1 -1
  174. package/dist/dev-server/index.d.ts +1 -1
  175. package/dist/dev-server/miniflare-bindings.d.ts +1 -1
  176. package/dist/dev-server/miniflare-dev-config.d.ts +5 -5
  177. package/dist/dev-server/miniflare-worker-config.d.ts +3 -3
  178. package/dist/dev-server/server-startup-helpers.d.ts +6 -6
  179. package/dist/dev-server/worker-surface-paths.d.ts +2 -2
  180. package/dist/index.d.ts +8 -8
  181. package/dist/index.js +9 -22
  182. package/dist/runtime/context-events.d.ts +1 -1
  183. package/dist/runtime/context.d.ts +3 -3
  184. package/dist/runtime/exports.d.ts +2 -2
  185. package/dist/runtime/index.d.ts +10 -10
  186. package/dist/runtime/index.js +5 -57
  187. package/dist/runtime/middleware.d.ts +1 -1
  188. package/dist/runtime/router/index.d.ts +3 -3
  189. package/dist/runtime/validation.d.ts +1 -1
  190. package/dist/secrets/local-secrets.d.ts +1 -1
  191. package/dist/shims/local-media-bindings.d.ts +1 -1
  192. package/dist/sveltekit/index.d.ts +1 -1
  193. package/dist/sveltekit/index.js +327 -9
  194. package/dist/sveltekit/local-bindings.d.ts +1 -1
  195. package/dist/sveltekit/platform.d.ts +1 -1
  196. package/dist/test/binding-hints.d.ts +2 -2
  197. package/dist/test/cf.d.ts +19 -19
  198. package/dist/test/index.d.ts +18 -18
  199. package/dist/test/index.js +4495 -46
  200. package/dist/test/offline-bindings.d.ts +3 -3
  201. package/dist/test/resolve-service-bindings.d.ts +1 -1
  202. package/dist/test/simple-context-bindings.d.ts +1 -1
  203. package/dist/test/simple-context-durable-objects.d.ts +1 -1
  204. package/dist/test/simple-context-env.d.ts +2 -2
  205. package/dist/test/simple-context-handlers.d.ts +2 -2
  206. package/dist/test/simple-context-lifecycle.d.ts +2 -2
  207. package/dist/test/simple-context-mfconfig.d.ts +1 -1
  208. package/dist/test/simple-context-multi-worker.d.ts +2 -2
  209. package/dist/test/simple-context-runtime.d.ts +1 -1
  210. package/dist/test/simple-context-startup.d.ts +1 -1
  211. package/dist/test/simple-context.d.ts +1 -1
  212. package/dist/test/utilities/env.d.ts +3 -3
  213. package/dist/test/utilities.d.ts +10 -10
  214. package/dist/test/worker.d.ts +1 -1
  215. package/dist/transform/index.d.ts +2 -2
  216. package/dist/utils/send-email.js +163 -19
  217. package/dist/vite/config-file.d.ts +2 -2
  218. package/dist/vite/index.d.ts +3 -3
  219. package/dist/vite/index.js +2 -31
  220. package/dist/vite/plugin-config-hook.d.ts +1 -1
  221. package/dist/vite/plugin-context.d.ts +3 -3
  222. package/dist/vite/plugin-durable-objects.d.ts +2 -2
  223. package/dist/vite/plugin-programmatic.d.ts +1 -1
  224. package/dist/vite/plugin-service-bindings.d.ts +2 -2
  225. package/dist/vite/plugin.d.ts +3 -3
  226. package/dist/worker-entry/composed-worker.d.ts +1 -1
  227. package/dist/worker-entry/routes.d.ts +2 -2
  228. package/dist/worker-entry/surface-paths.d.ts +1 -1
  229. package/dist/workflows/local-workflow-entrypoints.d.ts +1 -1
  230. package/package.json +5 -3
  231. package/dist/account-4tgh03a8.js +0 -484
  232. package/dist/ai-r3zqmxan.js +0 -261
  233. package/dist/api-6z01z734.js +0 -33
  234. package/dist/build-kw625v84.js +0 -51
  235. package/dist/config-ghk2bxj3.js +0 -102
  236. package/dist/deploy-9kee65n9.js +0 -1062
  237. package/dist/dev-x8zr249a.js +0 -3569
  238. package/dist/doctor-0ng5qrr1.js +0 -258
  239. package/dist/durable-object-0ybmg08a.js +0 -13
  240. package/dist/index-04wh9bxx.js +0 -514
  241. package/dist/index-0cc05hzv.js +0 -74
  242. package/dist/index-2yv7w548.js +0 -185
  243. package/dist/index-37x76zdn.js +0 -4
  244. package/dist/index-3cpzsr47.js +0 -125
  245. package/dist/index-3t6rypgc.js +0 -13
  246. package/dist/index-4mmpccrb.js +0 -309
  247. package/dist/index-5dkjffqz.js +0 -25
  248. package/dist/index-5hy7jmv3.js +0 -890
  249. package/dist/index-627srx16.js +0 -45
  250. package/dist/index-7m557715.js +0 -283
  251. package/dist/index-a8pnvg3r.js +0 -52
  252. package/dist/index-aabgympv.js +0 -39
  253. package/dist/index-atwc3csp.js +0 -256
  254. package/dist/index-cv4cvrxe.js +0 -2046
  255. package/dist/index-h7r11y4a.js +0 -539
  256. package/dist/index-hvtd936d.js +0 -1229
  257. package/dist/index-kcp7y951.js +0 -136
  258. package/dist/index-maw0jjnn.js +0 -147
  259. package/dist/index-mek9msfv.js +0 -705
  260. package/dist/index-n4xkaymm.js +0 -236
  261. package/dist/index-nkk59p3y.js +0 -579
  262. package/dist/index-phcrvs6d.js +0 -342
  263. package/dist/index-se4kw8tm.js +0 -460
  264. package/dist/index-x8s9rwfh.js +0 -2020
  265. package/dist/index-y7w3x9p1.js +0 -18
  266. package/dist/index-z9gy8w6b.js +0 -57
  267. package/dist/index-zdd0d9qa.js +0 -199
  268. package/dist/init-tb8jqgvr.js +0 -180
  269. package/dist/login-4h1sfsed.js +0 -75
  270. package/dist/previews-qr7c5j8h.js +0 -1336
  271. package/dist/productions-xpmd6bw8.js +0 -505
  272. package/dist/remote-kqcvhnfx.js +0 -192
  273. package/dist/secrets-6wbdsdek.js +0 -89
  274. package/dist/token-yp1qa70y.js +0 -419
  275. package/dist/types-15yv4cj3.js +0 -701
  276. package/dist/worker-1yg72jxg.js +0 -510
  277. package/dist/worker-entrypoint-y8s9kdcx.js +0 -15
@@ -0,0 +1,90 @@
1
+ import { h as normalizeWorkflowBinding } from "./schema-normalization-DWVleVxo.js";
2
+ import { i as findFiles, r as DEFAULT_WORKFLOW_PATTERN } from "./glob-CmQOvunB.js";
3
+ import { t as bundleWorkerEntry } from "./worker-bundler-dTTnYU3n.js";
4
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
5
+ import { join, relative, resolve } from "pathe";
6
+ //#region src/workflows/local-workflow-entrypoints.ts
7
+ function findExportedClasses(code) {
8
+ const classes = [];
9
+ const classPattern = /export\s+class\s+(\w+)/g;
10
+ let match = classPattern.exec(code);
11
+ while (match !== null) {
12
+ classes.push(match[1]);
13
+ match = classPattern.exec(code);
14
+ }
15
+ return classes;
16
+ }
17
+ function toImportSpecifier(fromDir, filePath) {
18
+ const relativePath = relative(fromDir, filePath).replace(/\\/g, "/");
19
+ return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
20
+ }
21
+ async function discoverWorkflowClasses(config, configDir) {
22
+ const classToFilePath = /* @__PURE__ */ new Map();
23
+ const workflowPatternConfig = config.files?.workflows;
24
+ const workflowPattern = typeof workflowPatternConfig === "string" ? workflowPatternConfig : DEFAULT_WORKFLOW_PATTERN;
25
+ if (workflowPatternConfig === false) return classToFilePath;
26
+ const files = await findFiles(workflowPattern, { cwd: configDir });
27
+ for (const filePath of files) try {
28
+ const code = await readFile(filePath, "utf-8");
29
+ for (const className of findExportedClasses(code)) classToFilePath.set(className, filePath);
30
+ } catch {}
31
+ return classToFilePath;
32
+ }
33
+ async function resolveLocalWorkflowEntrypoints(config, configDir) {
34
+ const workflows = config.bindings?.workflows;
35
+ if (!workflows || Object.keys(workflows).length === 0) return [];
36
+ const classToFilePath = await discoverWorkflowClasses(config, configDir);
37
+ const entrypoints = [];
38
+ for (const [bindingName, binding] of Object.entries(workflows)) {
39
+ const normalized = normalizeWorkflowBinding(binding);
40
+ if (normalized.scriptName) continue;
41
+ const scriptPath = classToFilePath.get(normalized.className);
42
+ if (!scriptPath) throw new Error(`Workflow binding ${bindingName} (className: '${normalized.className}') not found.\nEither set files.workflows to match the workflow source file, or set scriptName when the workflow lives in another worker.`);
43
+ entrypoints.push({
44
+ bindingName,
45
+ className: normalized.className,
46
+ scriptPath
47
+ });
48
+ }
49
+ return entrypoints;
50
+ }
51
+ function buildWorkflowVirtualEntry(entrypoints, entryDir) {
52
+ const imports = entrypoints.map((entrypoint, index) => {
53
+ const importName = `__DevflareWorkflow${index}`;
54
+ const importPath = toImportSpecifier(entryDir, entrypoint.scriptPath);
55
+ return {
56
+ importName,
57
+ className: entrypoint.className,
58
+ line: `import { ${entrypoint.className} as ${importName} } from '${importPath}'`
59
+ };
60
+ });
61
+ const exports = imports.map((entrypoint) => {
62
+ return `export { ${entrypoint.importName} as ${entrypoint.className} }`;
63
+ });
64
+ return [
65
+ ...imports.map((entrypoint) => entrypoint.line),
66
+ "",
67
+ ...exports
68
+ ].join("\n");
69
+ }
70
+ async function bundleWorkflowEntrypointScript(config, configDir, options = {}) {
71
+ const entrypoints = await resolveLocalWorkflowEntrypoints(config, configDir);
72
+ if (entrypoints.length === 0) return "";
73
+ const entryDir = resolve(configDir, ".devflare", "workflow-entrypoints");
74
+ const entryPath = join(entryDir, "__entry.ts");
75
+ const outFile = join(entryDir, "index.js");
76
+ await mkdir(entryDir, { recursive: true });
77
+ await writeFile(entryPath, buildWorkflowVirtualEntry(entrypoints, entryDir));
78
+ await bundleWorkerEntry({
79
+ cwd: configDir,
80
+ inputFile: entryPath,
81
+ outFile,
82
+ rolldownOptions: config.rolldown?.options,
83
+ sourcemap: config.rolldown?.sourcemap,
84
+ minify: config.rolldown?.minify,
85
+ logger: options.logger
86
+ });
87
+ return await readFile(outFile, "utf-8");
88
+ }
89
+ //#endregion
90
+ export { bundleWorkflowEntrypointScript as t };
@@ -0,0 +1,51 @@
1
+ import { t as account } from "./cloudflare-BaloqI3H.js";
2
+ import { d as green, f as logLine, g as yellow, h as whiteDim, o as dim, r as createCliTheme } from "./ui-BUoZApvE.js";
3
+ import { n as getConfiguredAccountId } from "./command-utils-BsfkmTqh.js";
4
+ import { t as getDependencies } from "./dependencies-C2oxFjU_.js";
5
+ //#region src/cli/commands/login.ts
6
+ async function logResolvedAccount(cwd, logger, theme) {
7
+ try {
8
+ const primaryAccount = await account.getPrimaryAccount();
9
+ if (primaryAccount) {
10
+ logLine(logger, `${dim("Primary account:", theme)} ${green(primaryAccount.name, theme)} ${whiteDim(`(${primaryAccount.id})`, theme)}`);
11
+ return;
12
+ }
13
+ } catch {}
14
+ const configuredAccountId = await getConfiguredAccountId(cwd);
15
+ if (!configuredAccountId) {
16
+ logLine(logger, dim("Run `devflare account` to inspect available accounts.", theme));
17
+ return;
18
+ }
19
+ const configuredAccount = await account.getAccountById(configuredAccountId);
20
+ if (configuredAccount) {
21
+ logLine(logger, `${dim("Configured account:", theme)} ${green(configuredAccount.name, theme)} ${whiteDim(`(${configuredAccount.id})`, theme)}`);
22
+ return;
23
+ }
24
+ logLine(logger, `${dim("Configured account ID:", theme)} ${whiteDim(configuredAccountId, theme)}`);
25
+ logLine(logger, dim("Run `devflare account --account <id>` to inspect the configured account.", theme));
26
+ }
27
+ async function runLoginCommand(parsed, logger, options) {
28
+ const force = parsed.options.force === true;
29
+ const cwd = options.cwd ?? process.cwd();
30
+ const theme = createCliTheme(parsed.options);
31
+ if (!force && await account.isAuthenticated()) {
32
+ logger.success("Already authenticated with Cloudflare");
33
+ await logResolvedAccount(cwd, logger, theme);
34
+ logLine(logger, dim("Use `devflare login --force` to open Wrangler login again.", theme));
35
+ return { exitCode: 0 };
36
+ }
37
+ logLine(logger);
38
+ logLine(logger, `${yellow("login", theme)} ${dim("Opening Wrangler login…", theme)}`);
39
+ if ((await (await getDependencies()).exec.exec("bunx", ["wrangler", "login"], {
40
+ cwd,
41
+ stdio: "inherit"
42
+ })).exitCode !== 0) {
43
+ logger.error("Wrangler login failed");
44
+ return { exitCode: 1 };
45
+ }
46
+ logger.success("Authenticated with Cloudflare");
47
+ await logResolvedAccount(cwd, logger, theme);
48
+ return { exitCode: 0 };
49
+ }
50
+ //#endregion
51
+ export { runLoginCommand };
@@ -0,0 +1,39 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { dirname, resolve } from "pathe";
3
+ import { fileURLToPath } from "node:url";
4
+ //#region src/cli/package-metadata.ts
5
+ let packageMetadataPromise = null;
6
+ async function loadPackageMetadata() {
7
+ let currentDir = dirname(fileURLToPath(import.meta.url));
8
+ while (true) {
9
+ const packageJsonPath = resolve(currentDir, "package.json");
10
+ try {
11
+ const packageJson = await readFile(packageJsonPath, "utf8");
12
+ const metadata = JSON.parse(packageJson);
13
+ if (metadata.name === "devflare") return metadata;
14
+ } catch {}
15
+ const parentDir = dirname(currentDir);
16
+ if (parentDir === currentDir) throw new Error("Could not resolve the devflare package.json file");
17
+ currentDir = parentDir;
18
+ }
19
+ }
20
+ async function getPackageMetadata() {
21
+ if (!packageMetadataPromise) packageMetadataPromise = loadPackageMetadata();
22
+ return packageMetadataPromise;
23
+ }
24
+ async function getPackageVersion() {
25
+ return (await getPackageMetadata()).version ?? "0.0.0";
26
+ }
27
+ async function getInitDependencyVersions() {
28
+ const metadata = await getPackageMetadata();
29
+ const dependencies = metadata.dependencies ?? {};
30
+ const devDependencies = metadata.devDependencies ?? {};
31
+ return {
32
+ devflare: `^${metadata.version ?? "0.0.0"}`,
33
+ typescript: devDependencies.typescript ?? "^5.7.0",
34
+ wrangler: dependencies.wrangler ?? devDependencies.wrangler ?? "^4.85.0",
35
+ workersTypes: devDependencies["@cloudflare/workers-types"] ?? "^4.20250109.0"
36
+ };
37
+ }
38
+ //#endregion
39
+ export { getPackageVersion as n, getInitDependencyVersions as t };
@@ -0,0 +1,241 @@
1
+ import { a as apiGetAll, d as kvGet, f as kvPut, i as apiGet, s as apiPost, u as kvDelete } from "./api-TzdliH-6.js";
2
+ import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ //#region src/cloudflare/account-core.ts
6
+ async function getAccounts(options) {
7
+ return (await apiGetAll("/accounts", options)).map((account) => ({
8
+ id: account.id,
9
+ name: account.name,
10
+ type: account.type,
11
+ createdOn: account.created_on ? new Date(account.created_on) : void 0
12
+ }));
13
+ }
14
+ async function getPrimaryAccount(options) {
15
+ return (await getAccounts(options))[0] ?? null;
16
+ }
17
+ async function getAccountById(accountId, options) {
18
+ try {
19
+ const account = await apiGet(`/accounts/${accountId}`, options);
20
+ return {
21
+ id: account.id,
22
+ name: account.name,
23
+ type: account.type,
24
+ createdOn: account.created_on ? new Date(account.created_on) : void 0
25
+ };
26
+ } catch {
27
+ return null;
28
+ }
29
+ }
30
+ //#endregion
31
+ //#region src/cloudflare/kv-namespace.ts
32
+ const DEVFLARE_KV_NAMESPACE_TITLE = "devflare-usage";
33
+ async function getOrCreateNamedKVNamespace(accountId, title = DEVFLARE_KV_NAMESPACE_TITLE, options) {
34
+ const existing = (await apiGetAll(`/accounts/${accountId}/storage/kv/namespaces`, options)).find((namespace) => namespace.title === title);
35
+ if (existing) return existing.id;
36
+ return (await apiPost(`/accounts/${accountId}/storage/kv/namespaces`, { title }, options)).id;
37
+ }
38
+ //#endregion
39
+ //#region src/cloudflare/preferences.ts
40
+ const GLOBAL_ACCOUNT_KEY = "settings:defaultAccountId";
41
+ const LOCAL_CACHE_DIR = ".devflare";
42
+ const LOCAL_CACHE_FILE = "preferences.json";
43
+ /**
44
+ * Write a file atomically by writing to a temp sibling and renaming into place.
45
+ * Prevents corruption if the process is killed mid-write.
46
+ *
47
+ * @internal exported for tests only
48
+ */
49
+ function writeFileAtomic(path, contents) {
50
+ const tmpPath = path + ".tmp-" + process.pid + "-" + Date.now();
51
+ writeFileSync(tmpPath, contents, "utf-8");
52
+ try {
53
+ renameSync(tmpPath, path);
54
+ } catch (error) {
55
+ try {
56
+ unlinkSync(tmpPath);
57
+ } catch {}
58
+ throw error;
59
+ }
60
+ }
61
+ /**
62
+ * Get the path to the local preferences file
63
+ */
64
+ function getLocalPreferencesPath() {
65
+ return join(homedir(), LOCAL_CACHE_DIR, LOCAL_CACHE_FILE);
66
+ }
67
+ /**
68
+ * Read local preferences from disk
69
+ */
70
+ function readLocalPreferences() {
71
+ const path = getLocalPreferencesPath();
72
+ if (!existsSync(path)) return {};
73
+ try {
74
+ const content = readFileSync(path, "utf-8");
75
+ return JSON.parse(content);
76
+ } catch {
77
+ return {};
78
+ }
79
+ }
80
+ /**
81
+ * Write local preferences to disk
82
+ */
83
+ function writeLocalPreferences(prefs) {
84
+ const path = getLocalPreferencesPath();
85
+ const dir = join(homedir(), LOCAL_CACHE_DIR);
86
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
87
+ writeFileAtomic(path, JSON.stringify(prefs, null, " "));
88
+ }
89
+ /**
90
+ * Find the nearest package.json (searching upward from cwd)
91
+ */
92
+ function findPackageJsonPath(startDir) {
93
+ let dir = startDir ?? process.cwd();
94
+ while (dir !== join(dir, "..")) {
95
+ const pkgPath = join(dir, "package.json");
96
+ if (existsSync(pkgPath)) return pkgPath;
97
+ dir = join(dir, "..");
98
+ }
99
+ return null;
100
+ }
101
+ /**
102
+ * Read package.json from a path
103
+ */
104
+ function readPackageJson(path) {
105
+ try {
106
+ const content = readFileSync(path, "utf-8");
107
+ return JSON.parse(content);
108
+ } catch {
109
+ return null;
110
+ }
111
+ }
112
+ /**
113
+ * Write package.json to a path
114
+ */
115
+ function writePackageJson(path, pkg) {
116
+ writeFileAtomic(path, JSON.stringify(pkg, null, " ") + "\n");
117
+ }
118
+ /**
119
+ * Get workspace account ID from nearest package.json
120
+ */
121
+ function getWorkspaceAccountId() {
122
+ const pkgPath = findPackageJsonPath();
123
+ if (!pkgPath) return null;
124
+ return readPackageJson(pkgPath)?.devflare?.accountId ?? null;
125
+ }
126
+ /**
127
+ * Set workspace account ID in nearest package.json
128
+ * Creates package.json if it doesn't exist
129
+ */
130
+ function setWorkspaceAccountId(accountId) {
131
+ let pkgPath = findPackageJsonPath();
132
+ let pkg;
133
+ if (pkgPath) pkg = readPackageJson(pkgPath) ?? {};
134
+ else {
135
+ pkgPath = join(process.cwd(), "package.json");
136
+ pkg = {
137
+ name: "workspace",
138
+ private: true
139
+ };
140
+ }
141
+ if (!pkg.devflare) pkg.devflare = {};
142
+ pkg.devflare.accountId = accountId;
143
+ writePackageJson(pkgPath, pkg);
144
+ return pkgPath;
145
+ }
146
+ /**
147
+ * Find or create the devflare-managed KV namespace
148
+ * (Reuses the same namespace as usage tracking)
149
+ */
150
+ async function getOrCreatePreferencesNamespace(accountId) {
151
+ return getOrCreateNamedKVNamespace(accountId, DEVFLARE_KV_NAMESPACE_TITLE);
152
+ }
153
+ /**
154
+ * Get the global default account ID
155
+ *
156
+ * Priority:
157
+ * 1. Local cache (fast, no network)
158
+ * 2. Cloud KV (if local cache is missing)
159
+ *
160
+ * Returns null if no default is set
161
+ */
162
+ async function getGlobalDefaultAccountId(fallbackAccountId) {
163
+ const local = readLocalPreferences();
164
+ if (local.defaultAccountId) return local.defaultAccountId;
165
+ try {
166
+ const value = await kvGet(fallbackAccountId, await getOrCreatePreferencesNamespace(fallbackAccountId), GLOBAL_ACCOUNT_KEY);
167
+ if (value) {
168
+ writeLocalPreferences({
169
+ ...local,
170
+ defaultAccountId: value,
171
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
172
+ });
173
+ return value;
174
+ }
175
+ } catch (error) {
176
+ console.debug("[devflare preferences] cloud KV sync failed:", error instanceof Error ? error.message : String(error));
177
+ }
178
+ return null;
179
+ }
180
+ /**
181
+ * Set the global default account ID
182
+ * Saves to both local cache and cloud KV
183
+ *
184
+ * @param accountId - The account ID to set as default
185
+ * @param anyAccountId - Any account ID to use for accessing KV (can be the same)
186
+ */
187
+ async function setGlobalDefaultAccountId(accountId, anyAccountId) {
188
+ const kvAccountId = anyAccountId ?? accountId;
189
+ writeLocalPreferences({
190
+ ...readLocalPreferences(),
191
+ defaultAccountId: accountId,
192
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
193
+ });
194
+ try {
195
+ await kvPut(kvAccountId, await getOrCreatePreferencesNamespace(kvAccountId), GLOBAL_ACCOUNT_KEY, accountId);
196
+ } catch (error) {
197
+ console.debug("[devflare preferences] cloud KV sync failed:", error instanceof Error ? error.message : String(error));
198
+ }
199
+ }
200
+ /**
201
+ * Get the effective account ID to use
202
+ *
203
+ * Priority:
204
+ * 1. Workspace (package.json) - highest priority
205
+ * 2. Global default (local cache + cloud KV)
206
+ * 3. Primary account (first account in list)
207
+ *
208
+ * @param primaryAccountId - The primary account ID to use as fallback
209
+ */
210
+ async function getEffectiveAccountId(primaryAccountId) {
211
+ const workspaceId = getWorkspaceAccountId();
212
+ if (workspaceId) return {
213
+ accountId: workspaceId,
214
+ source: "workspace"
215
+ };
216
+ const globalId = await getGlobalDefaultAccountId(primaryAccountId);
217
+ if (globalId) return {
218
+ accountId: globalId,
219
+ source: "global"
220
+ };
221
+ return {
222
+ accountId: primaryAccountId,
223
+ source: "primary"
224
+ };
225
+ }
226
+ /**
227
+ * Clear the global default account ID (both local and cloud)
228
+ */
229
+ async function clearGlobalDefaultAccountId(anyAccountId) {
230
+ const local = readLocalPreferences();
231
+ delete local.defaultAccountId;
232
+ local.lastUpdated = (/* @__PURE__ */ new Date()).toISOString();
233
+ writeLocalPreferences(local);
234
+ try {
235
+ await kvDelete(anyAccountId, await getOrCreatePreferencesNamespace(anyAccountId), GLOBAL_ACCOUNT_KEY);
236
+ } catch (error) {
237
+ console.debug("[devflare preferences] cloud KV sync failed:", error instanceof Error ? error.message : String(error));
238
+ }
239
+ }
240
+ //#endregion
241
+ export { setGlobalDefaultAccountId as a, getOrCreateNamedKVNamespace as c, getPrimaryAccount as d, getWorkspaceAccountId as i, getAccountById as l, getEffectiveAccountId as n, setWorkspaceAccountId as o, getGlobalDefaultAccountId as r, DEVFLARE_KV_NAMESPACE_TITLE as s, clearGlobalDefaultAccountId as t, getAccounts as u };