import fs from 'fs' import path from 'path' /** * 读取根目录的 config-bianliang.env 并注入到 process.env * - 仅在变量未设置时写入,绝不覆盖真实环境变量或 .env 值 * - 支持以 # 开头的注释,空行会被忽略 * - 行格式 KEY=VALUE,等号右侧原样接收(去除收尾空白) */ export function loadPortableEnvIfPresent(): void { try { const root = process.cwd() const envFile = path.join(root, 'config-bianliang.env') if (!fs.existsSync(envFile)) return const content = fs.readFileSync(envFile, 'utf8') const lines = content.split(/\r?\n/) for (const rawLine of lines) { const line = rawLine.trim() if (!line || line.startsWith('#')) continue const eqIndex = line.indexOf('=') if (eqIndex <= 0) continue const key = line.slice(0, eqIndex).trim() const value = line.slice(eqIndex + 1).trim() if (!key) continue if (process.env[key] === undefined || process.env[key] === '') { process.env[key] = value } } } catch { // 静默失败,避免影响运行 } } const configLoader = { loadPortableEnvIfPresent } export default configLoader