20 lines
749 B
JavaScript
20 lines
749 B
JavaScript
import { existsSync, readFileSync } from 'node:fs';
|
||
import { dirname, resolve } from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
|
||
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
||
|
||
/** 本地 dev:与 apps/api/.env 的 PORT 对齐,供 player/admin Vite 代理使用 */
|
||
export function resolveApiDevTarget() {
|
||
if (process.env.VITE_DEV_API_TARGET) return process.env.VITE_DEV_API_TARGET;
|
||
if (process.env.API_DEV_TARGET) return process.env.API_DEV_TARGET;
|
||
|
||
let port = '3000';
|
||
const envPath = resolve(repoRoot, 'apps/api/.env');
|
||
if (existsSync(envPath)) {
|
||
const match = readFileSync(envPath, 'utf8').match(/^PORT=(\d+)\s*$/m);
|
||
if (match) port = match[1];
|
||
}
|
||
return `http://localhost:${port}`;
|
||
}
|