feat: auto npm install in dev mode to sync dependencies (#94)

This commit is contained in:
Zihao Xu
2026-01-24 17:19:44 -08:00
committed by GitHub
parent 8af763531b
commit a2f2010ee5
2 changed files with 104 additions and 0 deletions

View File

@@ -559,6 +559,31 @@ async def game_loop():
print(f"Game loop error: {e}")
get_logger().logger.error(f"Game loop error: {e}", exc_info=True)
def ensure_npm_dependencies(web_dir: str) -> bool:
"""
确保 npm 依赖是最新的。
Args:
web_dir: web 目录路径。
Returns:
True 如果安装成功False 如果失败。
"""
import platform
print("📦 正在检查前端依赖...")
try:
if platform.system() == "Windows":
subprocess.run("npm install", cwd=web_dir, shell=True, check=True)
else:
subprocess.run(["npm", "install"], cwd=web_dir, shell=False, check=True)
print("✅ 前端依赖已就绪")
return True
except subprocess.CalledProcessError as e:
print(f"⚠️ npm install 失败: {e},继续启动...")
return False
@asynccontextmanager
async def lifespan(app: FastAPI):
# 初始化语言设置
@@ -603,6 +628,9 @@ async def lifespan(app: FastAPI):
project_root = os.path.abspath(os.path.join(current_dir, '..', '..'))
web_dir = os.path.join(project_root, 'web')
# 确保 npm 依赖是最新的npm install 会自动跳过已安装的包,通常 <1s
ensure_npm_dependencies(web_dir)
print(f"正在启动前端开发服务 (npm run dev) 于: {web_dir}")
# 跨平台兼容Windows 用 shell=True + 字符串macOS/Linux 用 shell=False + 列表。
try: