import { existsSync, readFileSync } from 'node:fs';
import { resolve } from 'node:path';
/**
* Minimal .env loader to avoid requiring a runtime dependency.
* Existing environment variables always take precedence.
*/
export function loadDotEnv(): void {
const envPath = resolve(process.cwd(), '.env');
if (!existsSync(envPath)) {
return;
}
const content = readFileSync(envPath, 'utf8');
const lines = content.split(/\r?\n/);
for (const rawLine of lines) {
const line = rawLine.trim();
if (!line || line.startsWith('#')) {
continue;
}
const separatorIndex = line.indexOf('=');
if (separatorIndex <= 0) {
continue;
}
const key = line.slice(0, separatorIndex).trim();
let value = line.slice(separatorIndex + 1).trim();
// Support quoted values while keeping parsing rules intentionally simple.
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
if (process.env[key] === undefined) {
process.env[key] = value;
}
}
}