This commit is contained in:
vet
2026-04-21 12:24:04 +07:00
parent 67ab5f5ba5
commit 6eb89ad2a9
15 changed files with 421 additions and 170 deletions

110
08-build-static-frontend.sh Normal file → Executable file
View File

@@ -1,55 +1,58 @@
#!/usr/bin/env bash
# =============================================================================
# 08-build-static-frontend.sh — 构建静态前端资源dist
# 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
# ./08-build-static-frontend.sh # 构建全部静态前端
# ./08-build-static-frontend.sh <project> # 构建指定项目
#
# 产物目录:
# /app/cms/dist
# /app/build-cms/dist
# 可用项目: pc, cms, build-cms, build-down
# =============================================================================
set -euo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh"
init_dirs
load_env
init_script_log
header "构建静态前端资源"
header "构建静态前端"
declare -A FE_DIR=(
[cms]="cms"
[build-cms]="build-cms"
)
STATIC_FRONTENDS=(pc cms build-cms build-down)
declare -A FE_PM=(
[pc]="yarn"
[cms]="pnpm"
[build-cms]="pnpm"
[build-down]="npm"
)
declare -A FE_BUILD_CMD=(
[pc]="yarn build"
[cms]="pnpm run build"
[build-cms]="pnpm run build"
[build-down]="npm run build"
)
FE_PROJECTS=(cms build-cms)
TARGET="${1:-all}"
declare -A FE_DIST_FILE=(
[pc]="$ROOT_DIR/pc/dist/index.html"
[cms]="$ROOT_DIR/cms/dist/index.html"
[build-cms]="$ROOT_DIR/build-cms/dist/index.html"
[build-down]="$ROOT_DIR/build-down/dist/index.html"
)
_valid_target() {
local target="$1"
for p in "${FE_PROJECTS[@]}"; do
[[ "$p" == "$target" ]] && return 0
done
return 1
}
declare -A FE_URL=(
[pc]="https://${PC_PROXY_DOMAIN:-pc-jack.imharry.work}/"
[cms]="http://cms-jack.imharry.work/"
[build-cms]="http://build-jack.imharry.work/"
[build-down]="http://down-jack.imharry.work/"
)
_build_fe() {
_build_one() {
local name="$1"
local dir="$ROOT_DIR/${FE_DIR[$name]}"
local dir="$ROOT_DIR/$name"
local pm="${FE_PM[$name]}"
local cmd="${FE_BUILD_CMD[$name]}"
local dist_dir="$dir/dist"
local dist_file="${FE_DIST_FILE[$name]}"
local logfile="$LOG_DIR/build-${name}.log"
if [[ ! -d "$dir" ]]; then
error "$name 目录不存在: $dir"
@@ -61,49 +64,70 @@ _build_fe() {
return 1
fi
if ! command -v "$pm" &>/dev/null; then
error "$name 需要 $pm,未安装"
if ! command -v "$pm" >/dev/null 2>&1; then
error "$name 需要 $pm但当前未安装"
return 1
fi
info "构建 ${BOLD}$name${NC} ..."
: > "$logfile"
if [[ "$name" == "pc" ]]; then
pc_ensure_wasm_assets
pc_export_vite_backend_env
pc_print_vite_backend_env
fi
(
cd "$dir"
rm -rf "$dist_dir"
# shellcheck disable=SC2086
$cmd
)
$cmd 2>&1
) | tee "$logfile"
if [[ ! -d "$dist_dir" ]]; then
error "$name 构建完成但 dist 不存在: $dist_dir"
if [[ ! -f "$dist_file" ]]; then
error "$name 构建完成,但未找到产物: $dist_file"
return 1
fi
success "$name 构建完成 $dist_dir"
success "$name 构建完成: $dist_file"
info "访问地址: ${FE_URL[$name]}"
}
TARGET="${1:-all}"
if [[ "$TARGET" == "all" ]]; then
FAILED=()
for proj in "${FE_PROJECTS[@]}"; do
_build_fe "$proj" || FAILED+=("$proj")
for project in "${STATIC_FRONTENDS[@]}"; do
if ! _build_one "$project"; then
FAILED+=("$project")
fi
echo ""
done
if [[ ${#FAILED[@]} -gt 0 ]]; then
error "以下静态前端构建失败: ${FAILED[*]}"
error "以下项目构建失败: ${FAILED[*]}"
exit 1
fi
else
if ! _valid_target "$TARGET"; then
valid=false
for project in "${STATIC_FRONTENDS[@]}"; do
if [[ "$TARGET" == "$project" ]]; then
valid=true
break
fi
done
if ! $valid; then
error "未知项目: $TARGET"
echo "可用: ${FE_PROJECTS[*]}"
echo "可用: ${STATIC_FRONTENDS[*]}"
exit 1
fi
_build_fe "$TARGET"
_build_one "$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/"
success "静态前端构建完成"
echo "下一步:"
echo " sudo ./deploy-test/00-init-tools.sh nginx"
echo " ./deploy-test/09-verify-static-frontends.sh"