This commit is contained in:
vet
2026-04-21 10:38:07 +07:00
parent cdbcad650e
commit 67ab5f5ba5
3 changed files with 208 additions and 7 deletions

109
08-build-static-frontend.sh Normal file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/env bash
# =============================================================================
# 08-build-static-frontend.sh — 构建静态前端资源dist
#
# 用法:
# ./08-build-static-frontend.sh # 构建 cms + build-cms
# ./08-build-static-frontend.sh cms # 只构建 cms
# ./08-build-static-frontend.sh build-cms # 只构建 build-cms
#
# 产物目录:
# /app/cms/dist
# /app/build-cms/dist
# =============================================================================
set -euo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh"
init_dirs
init_script_log
header "构建静态前端资源"
declare -A FE_DIR=(
[cms]="cms"
[build-cms]="build-cms"
)
declare -A FE_PM=(
[cms]="pnpm"
[build-cms]="pnpm"
)
declare -A FE_BUILD_CMD=(
[cms]="pnpm run build"
[build-cms]="pnpm run build"
)
FE_PROJECTS=(cms build-cms)
TARGET="${1:-all}"
_valid_target() {
local target="$1"
for p in "${FE_PROJECTS[@]}"; do
[[ "$p" == "$target" ]] && return 0
done
return 1
}
_build_fe() {
local name="$1"
local dir="$ROOT_DIR/${FE_DIR[$name]}"
local pm="${FE_PM[$name]}"
local cmd="${FE_BUILD_CMD[$name]}"
local dist_dir="$dir/dist"
if [[ ! -d "$dir" ]]; then
error "$name 目录不存在: $dir"
return 1
fi
if [[ ! -d "$dir/node_modules" ]]; then
error "$name node_modules 不存在,请先执行: ./deploy-test/06-install-frontend.sh $name"
return 1
fi
if ! command -v "$pm" &>/dev/null; then
error "$name 需要 $pm,未安装"
return 1
fi
info "构建 ${BOLD}$name${NC} ..."
(
cd "$dir"
rm -rf "$dist_dir"
# shellcheck disable=SC2086
$cmd
)
if [[ ! -d "$dist_dir" ]]; then
error "$name 构建完成但 dist 不存在: $dist_dir"
return 1
fi
success "$name 构建完成 → $dist_dir"
}
if [[ "$TARGET" == "all" ]]; then
FAILED=()
for proj in "${FE_PROJECTS[@]}"; do
_build_fe "$proj" || FAILED+=("$proj")
done
if [[ ${#FAILED[@]} -gt 0 ]]; then
error "以下静态前端构建失败: ${FAILED[*]}"
exit 1
fi
else
if ! _valid_target "$TARGET"; then
error "未知项目: $TARGET"
echo "可用: ${FE_PROJECTS[*]}"
exit 1
fi
_build_fe "$TARGET"
fi
echo ""
echo -e "${BOLD}下一步:${NC}"
echo " 1. 以 root 执行: sudo ./deploy-test/00-init-tools.sh nginx"
echo " 2. 域名访问:"
echo " - http://cms-jack.imharry.work/"
echo " - http://build-jack.imharry.work/"