chore(server): 优化环境变量加载与配置

1.  更新.gitignore,忽略所有.env文件但保留.env.example
2.  新增dotenv和cross-env依赖,完善环境变量加载逻辑
3.  新增dev:local本地开发启动脚本,适配多环境配置加载
4.  调整start启动脚本,统一使用cross-env处理环境变量
This commit is contained in:
andyesfly committed 2026-09-03 11:36:05 +08:00
1 parent 219091f864
commit 609221f544
5 files changed
+8674 -712

No files matched your search

+2 -2
View File
@@ -17,8 +17,8 @@ lerna-debug.log*
# Environment files
.env
.env.local
.env.*.local
.env.*
!.env.example
# Editor directories and files
.vscode/*
+7921
View File
File diff suppressed because it is too large. Load diff
+4 -1
View File
@@ -5,7 +5,8 @@
"private": true,
"scripts": {
"dev": "midway-bin dev --ts",
"start": "NODE_ENV=production midway-bin bootstrap --ts",
"dev:local": "cross-env MIDWAY_SERVER_ENV=local midway-bin dev --ts",
"start": "cross-env NODE_ENV=production midway-bin bootstrap --ts",
"test": "midway-bin test --ts",
"lint": "eslint src/**/*.ts",
"build": "midway-bin build -c"
@@ -23,6 +24,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"dayjs": "^1.11.13",
"dotenv": "^17.4.2",
"ioredis": "^5.4.1",
"jsonwebtoken": "^9.0.2",
"pg": "^8.11.0",
@@ -41,6 +43,7 @@
"@types/jsonwebtoken": "^9.0.7",
"@types/node": "^22.10.0",
"@types/uuid": "^10.0.0",
"cross-env": "^10.1.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"typescript": "^5.7.0"
+14
View File
@@ -1,5 +1,19 @@
import { MidwayConfig } from "@midwayjs/core";
import { existsSync } from "fs";
import { join } from "path";
import { config as loadEnv } from "dotenv";
// 加载项目根目录的环境变量文件:
// 1) .env 基础配置(默认=生产,不覆盖系统环境变量)
// 2) .env.{环境} 环境专属覆盖,如 .env.local(MIDWAY_SERVER_ENV=local 时生效)
const envDir = join(__dirname, "../../");
loadEnv({ path: join(envDir, ".env") });
const currentEnv = process.env.MIDWAY_SERVER_ENV || process.env.NODE_ENV || "prod";
const envFile = join(envDir, `.env.${currentEnv}`);
if (existsSync(envFile)) {
loadEnv({ path: envFile, override: true });
}
export default {
keys: "smart-grid-iot-secret-key",
+733 -709
View File
File diff suppressed because it is too large. Load diff