#!/bin/bash
npx vite build
# 开发主机（开发环境服务器 有SSH权限至生产环境）
DEV_HOST=root@dbo.fmode.cn
# 生产主机(生产环境服务器)
PROD_HOST=root@8.140.98.43
# 部署路径
DIR_SERVER=/opt/edu-textbook-server

# 部署过程
ssh $DEV_HOST "mkdir -p $DIR_SERVER"
scp dist/server/server.js $DEV_HOST:$DIR_SERVER/
scp ./package.json $DEV_HOST:$DIR_SERVER/
scp ./config.js $DEV_HOST:$DIR_SERVER/
ssh $DEV_HOST "scp -r $DIR_SERVER $PROD_HOST:/opt/"

# 启动过程
ssh $DEV_HOST "ssh $PROD_HOST \"cd $DIR_SERVER && ls -lah\"" # 仅安装prod
ssh $DEV_HOST "ssh $PROD_HOST \"cd $DIR_SERVER && npm i --omit=dev --force\"" # 仅安装prod
ssh $DEV_HOST "ssh $PROD_HOST \"cd $DIR_SERVER && pm2 stop server.js\""
ssh $DEV_HOST "ssh $PROD_HOST \"cd $DIR_SERVER && pm2 start server.js\""