Files
Artveris/scripts/dev.py
T

73 lines
2.4 KiB
Python
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.
# /// script
# requires-python = ">=3.9"
# dependencies = ["pyyaml"]
# ///
"""本地开发一键启动:生成 .env -> 起 MySQL/Redis -> 构建 shared -> 迁移+种子 -> 同时启动 api/admin/h5。
用法:
uv run scripts/dev.py # 完整启动
uv run scripts/dev.py --no-seed # 跳过种子数据
uv run scripts/dev.py --install # 先 pnpm install
"""
from __future__ import annotations
import sys
import time
from _lib import build_env, free_ports, load_config, popen, run, run_retry, wait_port, write_env
def main() -> None:
args = sys.argv[1:]
cfg = load_config()
app = cfg["app"]
env = build_env(cfg)
write_env(env)
# 0. 释放上次残留的本地服务(同时解除对 Prisma 引擎 DLL 的占用,避免 generate EPERM)
free_ports([app["api_port"], app["admin_port"], app["h5_port"]])
time.sleep(1.5)
if "--install" in args:
run("pnpm install")
# 1. 启动数据库与缓存
run("docker compose --env-file .env -f docker/docker-compose.dev.yml up -d")
wait_port(cfg["database"]["host"], int(cfg["database"]["port"]), 90)
wait_port(cfg["redis"]["host"], int(cfg["redis"]["port"]), 30)
time.sleep(3) # 给 MySQL 初始化留余量
# 2. 构建共享包
run("pnpm build:shared")
# 3. 数据库迁移 + 种子(generate 偶发被占用,自动重试)
if not run_retry("pnpm --filter @artveris/api prisma:generate", attempts=4, delay=2):
print("[artveris] 警告:prisma generate 仍失败(客户端可能已存在),继续启动 …")
run("pnpm --filter @artveris/api prisma:deploy")
if "--no-seed" not in args:
run("pnpm --filter @artveris/api seed", check=False)
# 4. 同时启动三端
procs = [
popen("pnpm dev:api"),
popen("pnpm dev:admin"),
popen("pnpm dev:h5"),
]
app = cfg["app"]
print("\n[artveris] 开发服务启动中:")
print(f" 后端 API : http://localhost:{app['api_port']}/api (文档 /api/docs)")
print(f" 管理后台 : http://localhost:{app['admin_port']}")
print(f" C 端 : http://localhost:{app['h5_port']}")
print(" 按 Ctrl+C 退出全部。\n")
try:
for p in procs:
p.wait()
except KeyboardInterrupt:
print("\n[artveris] 正在停止 …")
for p in procs:
p.terminate()
if __name__ == "__main__":
main()