import { readFileSync, existsSync } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; /** * Minimal .env loader (no dependency): loads /.env into process.env when a key is not * already set. Called once at worker/CLI startup so `env()` from @rareindex/shared sees secrets. */ export function loadDotenv(): void { const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..'); for (const file of ['.env', '.env.local']) { const p = path.join(root, file); if (!existsSync(p)) continue; for (const line of readFileSync(p, 'utf8').split('\n')) { const m = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/); if (!m) continue; let v = m[2]!; if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1); if (process.env[m[1]!] === undefined) process.env[m[1]!] = v; } } } export const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');