#!/usr/bin/env bash # ============================================================================= # 06-install-frontend.sh — 安装前端项目依赖 # # 首次使用或依赖变更后运行(对应后端的 04-build.sh) # # 用法: # ./06-install-frontend.sh # 安装全部前端项目依赖 # ./06-install-frontend.sh # 只安装指定项目 # # 可用项目: pc, meetingh5, h5, cms, build-cms, build-down # ============================================================================= set -euo pipefail source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh" init_dirs init_script_log # ← 脚本执行日志 header "前端依赖安装" # ── 工具检查 ────────────────────────────────────────────────────────────────── _check_pm() { local pm="$1" if ! command -v "$pm" &>/dev/null; then error "$pm 未安装" case "$pm" in yarn) echo " → npm install -g yarn" ;; pnpm) echo " → npm install -g pnpm" ;; npm) echo " → https://nodejs.org/" ;; esac return 1 fi return 0 } # ── 安装单个项目 ────────────────────────────────────────────────────────────── # 用法: _install <显示名> <目录> <包管理器> _install() { local name="$1" dir="$ROOT_DIR/$2" pm="$3" if [[ ! -d "$dir" ]]; then warn " 目录不存在,跳过: $dir" return 0 fi _check_pm "$pm" || return 1 local logfile="$LOG_DIR/install-${name}.log" info "安装 ${BOLD}$name${NC} 依赖 ($pm install) ..." if [[ -d "$dir/node_modules" ]]; then warn " node_modules 已存在,执行增量安装(如需全量重装请先 rm -rf $dir/node_modules)" fi local start_ts=$SECONDS (cd "$dir" && "$pm" install 2>&1) | tee -a "$logfile" | tail -5 local elapsed=$(( SECONDS - start_ts )) success " ✓ $name 安装完成 (${elapsed}s) → 日志: $logfile" } # ── 项目列表 ────────────────────────────────────────────────────────────────── TARGET="${1:-all}" case "$TARGET" in all) step "安装全部前端项目依赖(共 6 个)" _install "pc" "pc" "yarn" _install "meetingh5" "meetingh5" "npm" _install "h5" "h5" "npm" _install "cms" "cms" "pnpm" _install "build-cms" "build-cms" "pnpm" _install "build-down" "build-down" "npm" echo "" success "所有前端依赖安装完成!" echo "" echo -e "${BOLD}下一步:${NC}" echo -e " 启动前端开发服务器:" echo -e " ${CYAN}./deploy-test/07-start-frontend.sh${NC}" ;; pc|meetingh5|h5|cms|build-cms|build-down) local PM case "$TARGET" in pc) PM="yarn" ;; cms|build-cms) PM="pnpm" ;; *) PM="npm" ;; esac step "安装 $TARGET 依赖" _install "$TARGET" "$TARGET" "$PM" ;; *) error "未知项目: $TARGET" echo "可用: pc, meetingh5, h5, cms, build-cms, build-down" exit 1 ;; esac