Files
deploy-test/08-build-static-frontend.sh
2026-04-21 12:24:04 +07:00

134 lines
3.1 KiB
Bash
Executable File
Raw Permalink 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.

#!/usr/bin/env bash
# =============================================================================
# 08-build-static-frontend.sh — 构建静态前端产物dist
#
# 用法:
# ./08-build-static-frontend.sh # 构建全部静态前端
# ./08-build-static-frontend.sh <project> # 构建指定项目
#
# 可用项目: 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 "构建静态前端"
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"
)
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"
)
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_one() {
local name="$1"
local dir="$ROOT_DIR/$name"
local pm="${FE_PM[$name]}"
local cmd="${FE_BUILD_CMD[$name]}"
local dist_file="${FE_DIST_FILE[$name]}"
local logfile="$LOG_DIR/build-${name}.log"
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 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"
# shellcheck disable=SC2086
$cmd 2>&1
) | tee "$logfile"
if [[ ! -f "$dist_file" ]]; then
error "$name 构建完成,但未找到产物: $dist_file"
return 1
fi
success "$name 构建完成: $dist_file"
info "访问地址: ${FE_URL[$name]}"
}
TARGET="${1:-all}"
if [[ "$TARGET" == "all" ]]; then
FAILED=()
for project in "${STATIC_FRONTENDS[@]}"; do
if ! _build_one "$project"; then
FAILED+=("$project")
fi
echo ""
done
if [[ ${#FAILED[@]} -gt 0 ]]; then
error "以下项目构建失败: ${FAILED[*]}"
exit 1
fi
else
valid=false
for project in "${STATIC_FRONTENDS[@]}"; do
if [[ "$TARGET" == "$project" ]]; then
valid=true
break
fi
done
if ! $valid; then
error "未知项目: $TARGET"
echo "可用: ${STATIC_FRONTENDS[*]}"
exit 1
fi
_build_one "$TARGET"
fi
echo ""
success "静态前端构建完成"
echo "下一步:"
echo " sudo ./deploy-test/00-init-tools.sh nginx"
echo " ./deploy-test/09-verify-static-frontends.sh"