#!/bin/bash # ============================================================ # 部署脚本 — 订单发货管理系统 v2.0 # 使用方法:bash deploy.sh [install|build|start|restart|stop|status] # ============================================================ set -e APP_NAME="order-shipping" APP_DIR="$(cd "$(dirname "$0")" && pwd)" NODE_VERSION="18" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' log() { echo -e "${GREEN}[✓]${NC} $1"; } warn() { echo -e "${YELLOW}[!]${NC} $1"; } err() { echo -e "${RED}[]${NC} $1"; } # 检查 Node.js check_node() { if ! command -v node &>/dev/null; then err "Node.js 未安装,请先安装 Node.js ${NODE_VERSION}+" exit 1 fi log "Node.js $(node -v) 已安装" } # 安装依赖 install() { log "安装后端依赖..." cd "$APP_DIR" && npm install log "安装前端依赖并构建..." cd "$APP_DIR/web" && npm install && npm run build log "安装 PM2..." npm install -g pm2 log "依赖安装完成" } # 构建前端 build() { log "构建前端..." cd "$APP_DIR/web" && npm run build log "前端构建完成 → web/dist/" } # 启动服务 start() { check_node log "使用 PM2 启动服务..." cd "$APP_DIR" && pm2 start ecosystem.config.js log "服务已启动" pm2 status } # 重启服务 restart() { log "重启服务..." cd "$APP_DIR" && pm2 restart "$APP_NAME" log "服务已重启" pm2 status } # 停止服务 stop() { log "停止服务..." cd "$APP_DIR" && pm2 stop "$APP_NAME" log "服务已停止" } # 查看状态 status() { pm2 status echo "" pm2 logs "$APP_NAME" --lines 20 --nostream } # 查看日志 logs() { pm2 logs "$APP_NAME" --lines 50 } # 主逻辑 case "${1:-help}" in install) install ;; build) build ;; start) start ;; restart) restart ;; stop) stop ;; status) status ;; logs) logs ;; *) echo "用法: bash deploy.sh [install|build|start|restart|stop|status|logs]" echo "" echo " install - 安装所有依赖 + 构建前端 + 安装PM2" echo " build - 仅构建前端" echo " start - 启动服务(PM2)" echo " restart - 重启服务" echo " stop - 停止服务" echo " status - 查看服务状态 + 最近日志" echo " logs - 实时查看日志" ;; esac