import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; const GLOBAL_ENV_PATH = path.join(os.homedir(), '.openclaw', '.env'); /** * Load ~/.openclaw/.env into process.env. * Always reads fresh from disk — no caching. * File values always override process.env. */ export function loadGlobalEnv(): void { applyEnvFile(); } /** * Force re-read ~/.openclaw/.env. * Same as loadGlobalEnv — always reads fresh. */ export function reloadGlobalEnv(): void { applyEnvFile(); } function applyEnvFile(): void { let content: string; try { content = fs.readFileSync(GLOBAL_ENV_PATH, 'utf-8'); } catch { return; // file doesn't exist — that's fine } for (const line of content.split('\n')) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith('#')) continue; const eqIdx = trimmed.indexOf('='); if (eqIdx < 1) continue; const key = trimmed.slice(0, eqIdx).trim(); let value = trimmed.slice(eqIdx + 1).trim(); // strip surrounding quotes if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) { value = value.slice(1, -1); } // Always use file value — real-time read, no caching process.env[key] = value; } }