deploy.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. ##############################################
  3. # Snapshot 前端部署脚本
  4. # 适用于 Linux 环境
  5. ##############################################
  6. # 颜色输出
  7. RED='\033[0;31m'
  8. GREEN='\033[0;32m'
  9. YELLOW='\033[1;33m'
  10. BLUE='\033[0;34m'
  11. NC='\033[0m' # No Color
  12. # 打印带颜色的消息
  13. print_info() {
  14. echo -e "${GREEN}[INFO]${NC} $1"
  15. }
  16. print_error() {
  17. echo -e "${RED}[ERROR]${NC} $1"
  18. }
  19. print_warning() {
  20. echo -e "${YELLOW}[WARNING]${NC} $1"
  21. }
  22. # 检查命令是否存在
  23. command_exists() {
  24. command -v "$1" >/dev/null 2>&1
  25. }
  26. echo "============================================"
  27. echo "Snapshot 前端部署脚本"
  28. echo "============================================"
  29. echo
  30. # 1. 检查Node.js环境
  31. echo "[1/5] 检查Node.js环境..."
  32. if ! command_exists node; then
  33. print_error "未检测到Node.js环境"
  34. echo "请安装 Node.js 16 或更高版本"
  35. exit 1
  36. fi
  37. NODE_VERSION=$(node -v)
  38. NPM_VERSION=$(npm -v)
  39. print_info "Node.js环境检查通过"
  40. echo "Node版本: $NODE_VERSION"
  41. echo "NPM版本: $NPM_VERSION"
  42. echo
  43. # 2. 检查npm环境
  44. echo "[2/5] 检查npm环境..."
  45. if ! command_exists npm; then
  46. print_error "未检测到npm环境"
  47. exit 1
  48. fi
  49. print_info "npm环境检查通过"
  50. echo
  51. # 3. 安装项目依赖
  52. echo "[3/5] 安装项目依赖..."
  53. if [ ! -d "node_modules" ]; then
  54. print_info "正在安装依赖,请稍候..."
  55. npm install
  56. if [ $? -neq 0 ]; then
  57. print_error "依赖安装失败"
  58. exit 1
  59. fi
  60. print_info "依赖安装完成"
  61. else
  62. print_info "依赖已存在,跳过安装"
  63. fi
  64. echo
  65. # 4. 构建项目
  66. echo "[4/5] 构建项目..."
  67. npm run build
  68. if [ $? -neq 0 ]; then
  69. print_error "项目构建失败"
  70. exit 1
  71. fi
  72. print_info "项目构建完成"
  73. echo
  74. # 5. 启动服务
  75. echo "[5/5] 启动服务..."
  76. echo
  77. echo "============================================"
  78. echo "选择启动模式:"
  79. echo "============================================"
  80. echo "1. 开发模式(热重载,端口8081)"
  81. echo "2. 生产模式(使用nginx等服务器)"
  82. echo "3. 仅构建(不启动服务器)"
  83. echo
  84. read -p "请输入选项 (1/2/3): " mode
  85. case $mode in
  86. 1)
  87. echo
  88. print_info "启动开发服务器..."
  89. echo "访问地址: http://localhost:8081"
  90. echo
  91. echo "按 Ctrl+C 停止服务器"
  92. echo
  93. npm run serve
  94. ;;
  95. 2)
  96. echo
  97. print_info "启动生产服务器..."
  98. # 检查dist目录
  99. if [ ! -d "dist" ]; then
  100. print_error "dist目录不存在,请先执行构建"
  101. exit 1
  102. fi
  103. # 使用Python的简单HTTP服务器(如果可用)
  104. if command_exists python3; then
  105. echo "使用Python HTTP服务器"
  106. echo "访问地址: http://localhost:8081"
  107. cd dist && python3 -m http.server 8081
  108. # 使用npx http-server
  109. elif command_exists npx; then
  110. echo "使用http-server"
  111. echo "访问地址: http://localhost:8081"
  112. npx http-server dist -p 8081
  113. else
  114. print_warning "未找到HTTP服务器工具"
  115. echo "请安装nginx或使用以下命令:"
  116. echo " npm install -g http-server"
  117. echo " http-server dist -p 8081"
  118. echo
  119. print_info "构建文件位于: dist目录"
  120. print_info "您可以将dist目录部署到任何静态文件服务器"
  121. exit 0
  122. fi
  123. ;;
  124. 3)
  125. echo
  126. print_info "构建完成!"
  127. echo "构建文件位于: dist目录"
  128. echo
  129. echo "部署到生产环境:"
  130. echo "1. 将dist目录复制到Web服务器"
  131. echo "2. 配置nginx指向dist目录"
  132. echo "3. 或使用任何静态文件托管服务"
  133. echo
  134. echo "nginx配置示例:"
  135. echo "server {"
  136. echo " listen 80;"
  137. echo " server_name localhost;"
  138. echo " root /path/to/dist;"
  139. echo " index index.html;"
  140. echo " location / {"
  141. echo " try_files \$uri \$uri/ /index.html;"
  142. echo " }"
  143. echo "}"
  144. ;;
  145. *)
  146. print_error "无效的选项"
  147. exit 1
  148. ;;
  149. esac