flnxi-Wu/lib/configLoader.ts
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

42 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
// 静默失败,避免影响运行
}
}
export default { loadPortableEnvIfPresent }