Files
thebet365/scripts/dev-api-target.mjs
Mars 43fa8ebef0 fix(dev): 本地 API 默认端口改为 3100 避开 Windows 保留段
Hyper-V 常保留 2960-3059,3000 会 EACCES;ensure-port-free 与 Vite 代理从 apps/api/.env 读取 PORT
2026-06-23 15:11:50 +08:00

29 lines
1022 B
JavaScript
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 { 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 默认端口3000 在部分 WindowsHyper-V 保留 29603059上会 EACCES */
const DEFAULT_DEV_API_PORT = '3100';
/** 与 apps/api/.env 的 PORT 对齐 */
export function resolveApiDevPort() {
if (process.env.PORT) return String(process.env.PORT);
let port = DEFAULT_DEV_API_PORT;
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 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;
return `http://localhost:${resolveApiDevPort()}`;
}