chore(server): 优化环境变量加载与配置
1. 更新.gitignore,忽略所有.env文件但保留.env.example 2. 新增dotenv和cross-env依赖,完善环境变量加载逻辑 3. 新增dev:local本地开发启动脚本,适配多环境配置加载 4. 调整start启动脚本,统一使用cross-env处理环境变量
This commit is contained in:
1 parent
219091f864
commit
609221f544
5 files changed
+8674
-712
No files matched your search
+2
-2
@@ -17,8 +17,8 @@ lerna-debug.log*
|
|||||||
|
|
||||||
# Environment files
|
# Environment files
|
||||||
.env
|
.env
|
||||||
.env.local
|
.env.*
|
||||||
.env.*.local
|
!.env.example
|
||||||
|
|
||||||
# Editor directories and files
|
# Editor directories and files
|
||||||
.vscode/*
|
.vscode/*
|
||||||
|
|||||||
Generated
+7921
File diff suppressed because it is too large.
Load diff
+4
-1
@@ -5,7 +5,8 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "midway-bin dev --ts",
|
"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",
|
"test": "midway-bin test --ts",
|
||||||
"lint": "eslint src/**/*.ts",
|
"lint": "eslint src/**/*.ts",
|
||||||
"build": "midway-bin build -c"
|
"build": "midway-bin build -c"
|
||||||
@@ -23,6 +24,7 @@
|
|||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"class-validator": "^0.14.1",
|
"class-validator": "^0.14.1",
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
|
"dotenv": "^17.4.2",
|
||||||
"ioredis": "^5.4.1",
|
"ioredis": "^5.4.1",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"pg": "^8.11.0",
|
"pg": "^8.11.0",
|
||||||
@@ -41,6 +43,7 @@
|
|||||||
"@types/jsonwebtoken": "^9.0.7",
|
"@types/jsonwebtoken": "^9.0.7",
|
||||||
"@types/node": "^22.10.0",
|
"@types/node": "^22.10.0",
|
||||||
"@types/uuid": "^10.0.0",
|
"@types/uuid": "^10.0.0",
|
||||||
|
"cross-env": "^10.1.0",
|
||||||
"jest": "^29.7.0",
|
"jest": "^29.7.0",
|
||||||
"ts-jest": "^29.2.5",
|
"ts-jest": "^29.2.5",
|
||||||
"typescript": "^5.7.0"
|
"typescript": "^5.7.0"
|
||||||
|
|||||||
@@ -1,5 +1,19 @@
|
|||||||
import { MidwayConfig } from "@midwayjs/core";
|
import { MidwayConfig } from "@midwayjs/core";
|
||||||
|
import { existsSync } from "fs";
|
||||||
import { join } from "path";
|
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 {
|
export default {
|
||||||
keys: "smart-grid-iot-secret-key",
|
keys: "smart-grid-iot-secret-key",
|
||||||
|
|||||||
+733
-709
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user