fix: 自动激活pnpm工具链

This commit is contained in:
Nixevol
2026-07-03 02:56:18 +08:00
parent 10f8c50c5d
commit b9e2b78fcd
7 changed files with 38 additions and 3 deletions
+1
View File
@@ -3,6 +3,7 @@
所有环境配置集中在 [`config.yaml`](./config.yaml)(带注释)。脚本会据此自动生成根目录 `.env`,**无需手改 .env**。
脚本现在直接用本机 Python 运行。建议使用项目内虚拟环境,避免污染全局 Python。
Node.js 20+ 自带 corepack;若未检测到 pnpm,脚本会自动激活根 `package.json` 声明的 pnpm 版本。
首次运行前:
+29
View File
@@ -8,6 +8,7 @@ import subprocess
import sys
import time
from pathlib import Path
from shutil import which
import yaml
@@ -58,6 +59,34 @@ def load_config(path: Path | None = None) -> dict:
return yaml.safe_load(f)
def ensure_pnpm() -> None:
"""确保 pnpm 可用;若未安装则通过 Node 自带的 corepack 激活项目声明的版本。"""
if which("pnpm"):
return
if not which("node"):
print("[artveris] 未找到 node,请先安装 Node.js 20+ 后重试。")
sys.exit(1)
if not which("corepack"):
print("[artveris] 未找到 pnpm/corepack,请确认 Node.js 20+ 已正确安装并加入 PATH。")
sys.exit(1)
version = "10.28.0"
try:
with open(ROOT / "package.json", "r", encoding="utf-8") as f:
package_manager = json.load(f).get("packageManager", "")
if package_manager.startswith("pnpm@"):
version = package_manager.removeprefix("pnpm@")
except (OSError, json.JSONDecodeError):
pass
print(f"[artveris] 未检测到 pnpm,正在通过 corepack 激活 pnpm@{version} …")
run("corepack enable")
run(f"corepack prepare pnpm@{version} --activate")
if not which("pnpm"):
print("[artveris] pnpm 激活后仍不可用,请重新打开终端后再试。")
sys.exit(1)
def build_env(cfg: dict) -> dict[str, str]:
"""将 YAML 配置映射为 .env 键值。"""
db = cfg["database"]
+2
View File
@@ -26,6 +26,7 @@ from _lib import (
ask,
build_env,
create_kill_job,
ensure_pnpm,
ensure_mysql_database,
install_console_ctrl_handler,
is_interactive,
@@ -155,6 +156,7 @@ def main() -> None:
kill_project_servers(ports=ports)
time.sleep(1.5)
ensure_pnpm()
if "--install" in args:
run("pnpm install")
+2 -1
View File
@@ -8,7 +8,7 @@
"""
from __future__ import annotations
from _lib import build_env, load_config, run, write_env
from _lib import build_env, ensure_pnpm, load_config, run, write_env
COMPOSE = "docker compose --env-file .env -f docker/docker-compose.full.yml"
@@ -18,6 +18,7 @@ def main() -> None:
write_env(build_env(cfg))
# 安装依赖并构建前端静态(供 Nginx 挂载)
ensure_pnpm()
run("pnpm install")
run("pnpm build:shared")
run("pnpm --filter @artveris/admin build")
+2 -1
View File
@@ -13,7 +13,7 @@ from __future__ import annotations
import sys
from _lib import build_env, load_config, run, write_env
from _lib import build_env, ensure_pnpm, load_config, run, write_env
COMPOSE = "docker compose --env-file .env -f docker/docker-compose.full.yml"
@@ -31,6 +31,7 @@ def main() -> None:
return
# 构建前端静态(供 Nginx 容器挂载)
ensure_pnpm()
run("pnpm install")
run("pnpm build:shared")
run("pnpm --filter @artveris/admin build")