# 一键部署脚本 - Windows PowerShell # 使用方法: .\deploy.ps1 $ErrorActionPreference = "Stop" Write-Host "🚀 开始部署 Just-Demo 项目..." -ForegroundColor Cyan # 1. 检查 Node.js Write-Host "`n📦 检查 Node.js..." -ForegroundColor Blue if (-not (Get-Command node -ErrorAction SilentlyContinue)) { Write-Host "❌ Node.js 未安装,请先安装 Node.js (v16+)" -ForegroundColor Red Write-Host "下载地址: https://nodejs.org/" -ForegroundColor Yellow exit 1 } Write-Host "✅ Node.js 版本: $(node -v)" -ForegroundColor Green # 2. 检查 pnpm,如果没有则安装 Write-Host "`n📦 检查 pnpm..." -ForegroundColor Blue if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) { Write-Host "pnpm 未安装,正在安装..." -ForegroundColor Yellow npm install -g pnpm } Write-Host "✅ pnpm 版本: $(pnpm -v)" -ForegroundColor Green # 3. 检查 PM2,如果没有则安装 Write-Host "`n📦 检查 PM2..." -ForegroundColor Blue if (-not (Get-Command pm2 -ErrorAction SilentlyContinue)) { Write-Host "PM2 未安装,正在安装..." -ForegroundColor Yellow npm install -g pm2 npm install -g pm2-windows-startup pm2-startup install } Write-Host "✅ PM2 已安装" -ForegroundColor Green # 4. 安装依赖 Write-Host "`n📦 安装项目依赖..." -ForegroundColor Blue pnpm install:all # 5. 构建项目 Write-Host "`n🔨 构建项目..." -ForegroundColor Blue pnpm build # 6. 创建生产环境配置 Write-Host "`n⚙️ 创建生产环境配置..." -ForegroundColor Blue if (-not (Test-Path "backend\.env")) { # 生成随机密钥 $bytes = New-Object byte[] 32 $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() $rng.GetBytes($bytes) $jwtSecret = [Convert]::ToBase64String($bytes) @" NODE_ENV=production PORT=3000 JWT_SECRET=$jwtSecret FRONTEND_URL=http://localhost:5500 UPLOAD_DIR=uploads "@ | Out-File -FilePath "backend\.env" -Encoding UTF8 Write-Host "✅ 已创建 backend\.env" -ForegroundColor Green } else { Write-Host "✅ backend\.env 已存在" -ForegroundColor Green } # 7. 运行数据库迁移 Write-Host "`n🗄️ 运行数据库迁移..." -ForegroundColor Blue Set-Location backend pnpm migrate:prod Set-Location .. # 8. 使用 PM2 启动服务 Write-Host "`n🚀 使用 PM2 启动服务..." -ForegroundColor Blue pm2 delete just-demo-backend 2>$null pm2 start ecosystem.config.js # 9. 保存 PM2 配置 Write-Host "`n💾 保存 PM2 配置..." -ForegroundColor Blue pm2 save # 10. 显示服务状态 Write-Host "`n📊 服务状态:" -ForegroundColor Cyan pm2 status Write-Host "`n🎉 部署成功!" -ForegroundColor Green Write-Host "`n访问地址:" -ForegroundColor Cyan Write-Host " - 前端: http://localhost:5500" -ForegroundColor White Write-Host " - 后端: http://localhost:3000" -ForegroundColor White Write-Host "`n常用命令:" -ForegroundColor Cyan Write-Host " pm2 status # 查看服务状态" -ForegroundColor White Write-Host " pm2 logs # 查看日志" -ForegroundColor White Write-Host " pm2 restart all # 重启所有服务" -ForegroundColor White Write-Host " pm2 stop all # 停止所有服务" -ForegroundColor White Write-Host ""