import { readFileSync } from "node:fs"; import { resolve } from "node:path"; /** * Inline .env/.env.local loader — must be the FIRST import in any * migration script that reads process.env at module-init time. * tsx's ESM hoisting means all top-level imports run before the * body, so we load env inside the imported module's TOP-LEVEL * SCOPE (here) instead of in the consumer's body. */ function loadEnv(): void { for (const name of [".env", ".env.local"]) { try { const raw = readFileSync(resolve(process.cwd(), name), "utf8"); for (const line of raw.split("\n")) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith("#")) continue; const m = trimmed.match(/^([A-Z0-9_]+)\s*=\s*(.+)$/i); if (m && !process.env[m[1]]) { process.env[m[1]] = m[2].replace(/^["']|["']$/g, "").trim(); } } } catch { // missing file — rely on shell env } } } loadEnv(); export { loadEnv };