import { existsSync } from 'node:fs'; import { resolve, dirname } from 'node:path'; import { unstable_readConfig } from 'wrangler'; import { DEFAULT_EXPORT } from './bindings.js'; function resolveWranglerConfig(root, explicitPath) { const configPath = explicitPath ? resolve(root, explicitPath) : ["wrangler.json", "wrangler.jsonc", "wrangler.toml"].map((name) => resolve(root, name)).find(existsSync); if (!configPath || !existsSync(configPath)) { return void 0; } let raw; try { raw = unstable_readConfig({ config: configPath }, { hideWarnings: true }); } catch { return void 0; } return { config: { main: raw.main, durableObjects: collectClassBindings(raw.durable_objects?.bindings), workflows: collectClassBindings(raw.workflows), workerEntrypoints: collectSelfBoundEntrypoints(raw), sameWorkerBindings: collectSameWorkerBindings(raw) }, configDir: dirname(raw.configPath ?? configPath) }; } function collectSelfBoundEntrypoints(raw) { if (!raw.name) { return []; } const entrypoints = /* @__PURE__ */ new Set(); for (const binding of raw.services ?? []) { if (binding?.service === raw.name && typeof binding.entrypoint === "string") { entrypoints.add(binding.entrypoint); } } return [...entrypoints]; } function collectSameWorkerBindings(raw) { const bindings = []; for (const binding of raw.durable_objects?.bindings ?? []) { if (typeof binding?.name === "string" && typeof binding.class_name === "string" && !binding.script_name) { bindings.push({ bindingName: binding.name, className: binding.class_name }); } } for (const binding of raw.services ?? []) { if (raw.name && binding?.service === raw.name && typeof binding.binding === "string") { bindings.push({ bindingName: binding.binding, className: typeof binding.entrypoint === "string" ? binding.entrypoint : DEFAULT_EXPORT }); } } return bindings; } function collectClassBindings(bindings) { const result = []; const seenClassNames = /* @__PURE__ */ new Set(); for (const binding of bindings ?? []) { if (typeof binding?.class_name !== "string" || binding.script_name || seenClassNames.has(binding.class_name)) { continue; } seenClassNames.add(binding.class_name); result.push({ name: binding.name, className: binding.class_name }); } return result; } export { DEFAULT_EXPORT, resolveWranglerConfig }; //# sourceMappingURL=wranglerConfig.js.map