Files
dukang/server/dukang-api/src/load-env.ts
T
jacy 7304c7a8e1 chore(ops): 增加生产库备份/回滚与按 isTest 清理脚本
只入库通用运维能力,不提交写死手机号的一次性清理脚本与 dump。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-23 22:03:35 +08:00

53 lines
1.8 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 { config } from 'dotenv';
import { existsSync } from 'fs';
import { resolve } from 'path';
/**
* 环境变量加载策略(后加载的文件覆盖先前的同名键):
*
* - local`.env` + `.env.local`
* - staging`.env.staging` + `.env.staging.local`(同机测试栈,APP_ENV=staging
* - production`.env.production` + `.env.production.local`
*
* 判定:优先 `APP_ENV`;未设置时 `NODE_ENV=production` → production,否则 local。
* staging 在 PM2 中仍设 `NODE_ENV=production`(构建产物),靠 `APP_ENV=staging` 区分。
*/
const apiRoot = resolve(__dirname, '..');
const nodeEnv = process.env.NODE_ENV ?? 'development';
function resolveAppEnv(): 'local' | 'staging' | 'production' {
const raw = (process.env.APP_ENV ?? '').trim().toLowerCase();
if (raw === 'staging' || raw === 'stage' || raw === 'test') return 'staging';
if (raw === 'production' || raw === 'prod') return 'production';
if (raw === 'local' || raw === 'development' || raw === 'dev') return 'local';
if (nodeEnv === 'production') return 'production';
return 'local';
}
const appEnv = resolveAppEnv();
const preserveDatabaseUrl =
process.env.FORCE_DATABASE_URL === '1' ? process.env.DATABASE_URL : undefined;
const layers =
appEnv === 'staging'
? [resolve(apiRoot, '.env.staging'), resolve(apiRoot, '.env.staging.local')]
: appEnv === 'production'
? [resolve(apiRoot, '.env.production'), resolve(apiRoot, '.env.production.local')]
: [resolve(apiRoot, '.env'), resolve(apiRoot, '.env.local')];
for (const file of layers) {
if (existsSync(file)) {
config({ path: file, override: true });
}
}
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = nodeEnv;
}
if (!process.env.APP_ENV) {
process.env.APP_ENV = appEnv;
}
if (preserveDatabaseUrl) {
process.env.DATABASE_URL = preserveDatabaseUrl;
}