feat(GOV-009): 建立 NestJS 三进程基线
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
import { parse } from 'dotenv';
|
||||
|
||||
import {
|
||||
loadApiConfig,
|
||||
loadSchedulerConfig,
|
||||
loadWorkerConfig,
|
||||
type ApiRuntimeConfig,
|
||||
type SchedulerRuntimeConfig,
|
||||
type WorkerRuntimeConfig,
|
||||
} from './runtime-config';
|
||||
|
||||
type ProcessName = 'api' | 'scheduler' | 'worker';
|
||||
|
||||
function findRepositoryRoot(startDirectory: string): string {
|
||||
let current = path.resolve(startDirectory);
|
||||
|
||||
while (true) {
|
||||
if (existsSync(path.join(current, 'pnpm-workspace.yaml'))) return current;
|
||||
const parent = path.dirname(current);
|
||||
if (parent === current) throw new Error('无法定位项目根目录');
|
||||
current = parent;
|
||||
}
|
||||
}
|
||||
|
||||
function readDevelopmentEnv(processName: ProcessName): NodeJS.ProcessEnv {
|
||||
const repositoryRoot = findRepositoryRoot(__dirname);
|
||||
const envFile = path.join(
|
||||
repositoryRoot,
|
||||
'deploy',
|
||||
'env',
|
||||
'development',
|
||||
`${processName}.env.local`,
|
||||
);
|
||||
|
||||
if (!existsSync(envFile)) {
|
||||
throw new Error(`缺少本地开发配置:deploy/env/development/${processName}.env.local`);
|
||||
}
|
||||
|
||||
return { ...process.env, ...parse(readFileSync(envFile)) };
|
||||
}
|
||||
|
||||
function resolveProcessEnvironment(processName: ProcessName): NodeJS.ProcessEnv {
|
||||
// 生产环境只信任 systemd/Compose 注入的 process.env,绝不读取仓库内开发配置。
|
||||
return process.env.APP_ENV === 'production' ? process.env : readDevelopmentEnv(processName);
|
||||
}
|
||||
|
||||
export function loadApiProcessConfig(): ApiRuntimeConfig {
|
||||
return loadApiConfig(resolveProcessEnvironment('api'));
|
||||
}
|
||||
|
||||
export function loadSchedulerProcessConfig(): SchedulerRuntimeConfig {
|
||||
return loadSchedulerConfig(resolveProcessEnvironment('scheduler'));
|
||||
}
|
||||
|
||||
export function loadWorkerProcessConfig(): WorkerRuntimeConfig {
|
||||
return loadWorkerConfig(resolveProcessEnvironment('worker'));
|
||||
}
|
||||
Reference in New Issue
Block a user