feat: enable dev frontend hot reload

This commit is contained in:
Cheng Zhou
2026-06-04 16:57:24 +08:00
parent e78bb04c4b
commit f6e5a4c744
3 changed files with 157 additions and 9 deletions
+20 -9
View File
@@ -71,6 +71,14 @@ run() {
"$@"
}
compose_cmd() {
if [[ "$TARGET_ENV" == "dev" ]]; then
docker compose -f "$ROOT_DIR/docker-compose.dev.yaml" "$@"
else
docker compose "$@"
fi
}
# 运行长命令,默认折叠输出。失败或 --verbose 时回放完整日志。
# 用法: run_quiet <提示语> -- <命令...>
run_quiet() {
@@ -144,6 +152,7 @@ ${C_BOLD}用法:${C_RESET}
${C_BOLD}环境:${C_RESET}
${C_CYAN}dev${C_RESET} 本地开发环境(使用默认密钥,不生成 RSA 密钥对)
前端通过 Vite dev server 热加载,入口仍为 http://127.0.0.1:8888
${C_CYAN}main${C_RESET} 内网测试 / 预发布环境
${C_CYAN}release${C_RESET} 生产环境(强制 HTTPS,自动生成密钥对)
@@ -451,22 +460,22 @@ prepare_env_file() {
run_compose_config() {
step "校验 Docker Compose 配置"
run_quiet "Docker Compose 配置语法校验" -- docker compose config
run_quiet "Docker Compose 配置语法校验" -- compose_cmd config
}
run_backend_init() {
[[ "$TARGET_ENV" != "dev" ]] || return 0
step "初始化数据库与管理员账号"
run_quiet "运行 backend-init 容器" -- docker compose run --rm backend-init
run_quiet "运行 backend-init 容器" -- compose_cmd run --rm backend-init
}
run_build_and_start() {
if [[ "$SKIP_BUILD" -eq 1 ]]; then
step "启动服务(跳过镜像构建)"
run_quiet "启动容器" -- docker compose up -d
run_quiet "启动容器" -- compose_cmd up -d
else
step "构建镜像并启动服务"
run_quiet "构建镜像并启动容器(首次较慢)" -- docker compose up -d --build
run_quiet "构建镜像并启动容器(首次较慢)" -- compose_cmd up -d --build
fi
}
@@ -476,7 +485,7 @@ run_migrations() {
return
fi
step "执行数据库迁移"
run_quiet "Alembic upgrade head" -- docker compose run --rm backend python -m alembic upgrade head
run_quiet "Alembic upgrade head" -- compose_cmd run --rm backend python -m alembic upgrade head
}
# ── 健康检查 ─────────────────────────────────
@@ -484,13 +493,15 @@ run_migrations() {
check_container_status() {
step "检查容器运行状态"
local running
running="$(docker compose ps --services --filter status=running 2>/dev/null || true)"
for svc in db backend nginx; do
running="$(compose_cmd ps --services --filter status=running 2>/dev/null || true)"
local services=(db backend nginx)
[[ "$TARGET_ENV" != "dev" ]] || services+=(frontend-dev)
for svc in "${services[@]}"; do
if printf '%s\n' "$running" | grep -qx "$svc"; then
printf ' %s✔%s %-8s 运行中\n' "${C_GREEN}${C_BOLD}" "${C_RESET}" "$svc"
else
printf ' %s✖%s %-8s %s未运行%s\n' "${C_RED}${C_BOLD}" "${C_RESET}" "$svc" "${C_RED}" "${C_RESET}"
fail "$svc 容器未正常运行,请执行 'docker compose ps' 排查"
fail "$svc 容器未正常运行,请执行 'bash scripts/install-ctms.sh $TARGET_ENV --skip-build --skip-migrate --verbose' 或对应 compose ps 命令排查"
fi
done
}
@@ -524,7 +535,7 @@ if expect_rsa:
)
PY
)
run_quiet "执行后端配置自检" -- docker compose exec -T \
run_quiet "执行后端配置自检" -- compose_cmd exec -T \
-e EXPECTED_ENV="$expected_env" \
-e EXPECTED_KEY_ID="$expected_key_id" \
-e EXPECT_RSA="$expect_rsa" \