#!/bin/bash
# lotteryfront 一键部署脚本
# 用法: bash scripts/deploy.sh

set -e

SERVER="root@47.242.205.221"
REMOTE_DIR="/www/wwwroot/lotteryFront"

echo "🔨 构建中..."
npm run build

echo "📦 上传到服务器..."
ssh ${SERVER} "mkdir -p ${REMOTE_DIR}/.next/standalone ${REMOTE_DIR}/logs"
rsync -avz --delete .next/standalone/ ${SERVER}:${REMOTE_DIR}/.next/standalone/
rsync -avz ecosystem.config.cjs ${SERVER}:${REMOTE_DIR}/ecosystem.config.cjs

echo "🚀 服务器重启 PM2..."
ssh ${SERVER} "source /etc/profile && source ~/.bashrc && mkdir -p ${REMOTE_DIR}/logs && cd ${REMOTE_DIR} && (pm2 restart lotteryfront 2>/dev/null || pm2 start ecosystem.config.cjs) && pm2 save"

echo "✅ lotteryFront 部署完成！"
