# ═══════════════════════════════════════════════════════════ # canteen-delivery2 生产环境一键部署脚本 # 用法: 以管理员身份打开 PowerShell,运行此脚本 # .\deploy.ps1 # ═══════════════════════════════════════════════════════════ $ErrorActionPreference = "Stop" $ProjectRoot = "C:\Users\ADMIN\Desktop\project\canteen-delivery2" Set-Location $ProjectRoot Write-Host "" Write-Host "══════════════════════════════════════════" -ForegroundColor Cyan Write-Host " canteen-delivery2 生产部署" -ForegroundColor Cyan Write-Host "══════════════════════════════════════════" -ForegroundColor Cyan Write-Host "" # ═══════ Step 1: 检查前置条件 ═══════ Write-Host "[1/5] 检查环境..." -ForegroundColor Yellow $nodeVersion = node --version 2>$null if (-not $nodeVersion) { Write-Host "ERROR: Node.js 未安装,请从 https://nodejs.org 下载安装" -ForegroundColor Red exit 1 } Write-Host " Node.js: $nodeVersion" -ForegroundColor Green # 检查 .env.production if (-not (Test-Path ".env.production")) { Write-Host "ERROR: .env.production 不存在,请先运行 node -e `"console.log(require('crypto').randomBytes(48).toString('base64'))`" 生成密钥" -ForegroundColor Red exit 1 } Write-Host " .env.production: 已配置" -ForegroundColor Green # ═══════ Step 2: 安装依赖 ═══════ Write-Host "" Write-Host "[2/5] 安装前端依赖..." -ForegroundColor Yellow npm ci --production=false 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { Write-Host " 尝试 npm install..." -ForegroundColor Yellow npm install 2>&1 } Write-Host " 前端依赖: 完成" -ForegroundColor Green Write-Host " 安装后端依赖..." -ForegroundColor Yellow Push-Location server npm ci --production=false 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { npm install 2>&1 } Pop-Location Write-Host " 后端依赖: 完成" -ForegroundColor Green # ═══════ Step 3: 构建 ═══════ Write-Host "" Write-Host "[3/5] 构建前端..." -ForegroundColor Yellow npm run build if ($LASTEXITCODE -ne 0) { Write-Host "前端构建失败" -ForegroundColor Red; exit 1 } Write-Host " 前端构建: dist/ 已生成" -ForegroundColor Green Write-Host " 构建后端..." -ForegroundColor Yellow Push-Location server npm run build Pop-Location if ($LASTEXITCODE -ne 0) { Write-Host "后端构建失败" -ForegroundColor Red; exit 1 } Write-Host " 后端构建: server/dist/ 已生成" -ForegroundColor Green # ═══════ Step 4: 数据库初始化 ═══════ Write-Host "" Write-Host "[4/5] 初始化数据库..." -ForegroundColor Yellow Push-Location server node dist/setup-db.js 2>&1 Write-Host " setup-db: 完成" -ForegroundColor Green node dist/migrate.js 2>&1 Write-Host " migrate: 完成" -ForegroundColor Green node dist/seed-permissions.js 2>&1 Write-Host " seed-permissions: 完成" -ForegroundColor Green Pop-Location # ═══════ Step 5: 启动 ═══════ Write-Host "" Write-Host "[5/5] 启动生产服务..." -ForegroundColor Yellow Write-Host "" Write-Host "══════════════════════════════════════════" -ForegroundColor Green Write-Host " 部署完成!请运行以下命令启动服务:" -ForegroundColor Green Write-Host "" Write-Host " # 方式1: 直接启动(前台运行)" -ForegroundColor White Write-Host " cd C:\Users\ADMIN\Desktop\project\canteen-delivery2\server" -ForegroundColor White Write-Host " node dist/index.js" -ForegroundColor White Write-Host "" Write-Host " # 方式2: 使用 PM2(推荐,后台运行+自动重启)" -ForegroundColor White Write-Host " npm install -g pm2" -ForegroundColor White Write-Host " cd C:\Users\ADMIN\Desktop\project\canteen-delivery2\server" -ForegroundColor White Write-Host " pm2 start dist/index.js --name canteen-delivery" -ForegroundColor White Write-Host " pm2 save" -ForegroundColor White Write-Host " pm2 startup" -ForegroundColor White Write-Host "" Write-Host " # 访问: http://localhost:3001" -ForegroundColor Cyan Write-Host "══════════════════════════════════════════" -ForegroundColor Green