Files
bj_power/bj_power_mes/scripts/setup-and-run.ps1
T

215 lines
8.1 KiB
PowerShell

# ============================================================================
# 一键安装和启动脚本
# ============================================================================
# 用法 (PowerShell):
# .\scripts\setup-and-run.ps1
#
# 功能:
# 1. 检查并安装 Node.js 依赖
# 2. 初始化 PostgreSQL 数据库
# 3. 启动后端和前端服务
# ============================================================================
$ErrorActionPreference = "Stop"
Write-Host ""
Write-Host "################################################################" -ForegroundColor Cyan
Write-Host "# #" -ForegroundColor Cyan
Write-Host "# 智能加工生产线管控系统 - 一键安装和启动 #" -ForegroundColor Cyan
Write-Host "# #" -ForegroundColor Cyan
Write-Host "################################################################" -ForegroundColor Cyan
Write-Host ""
# ==================== 检查前置条件 ====================
Write-Host ">>> 检查前置条件..." -ForegroundColor Yellow
Write-Host ""
# 检查 Go
try {
$goVersion = go version
Write-Host "[✓] Go 已安装: $goVersion" -ForegroundColor Green
} catch {
Write-Host "[✗] 错误: 未找到 Go" -ForegroundColor Red
Write-Host "请安装 Go 1.25+: https://golang.org/dl/" -ForegroundColor Yellow
exit 1
}
# 检查 Node.js
try {
$nodeVersion = node --version
Write-Host "[✓] Node.js 已安装: $nodeVersion" -ForegroundColor Green
} catch {
Write-Host "[✗] 错误: 未找到 Node.js" -ForegroundColor Red
Write-Host "请安装 Node.js 18+: https://nodejs.org/" -ForegroundColor Yellow
exit 1
}
# 检查 pnpm
try {
$pnpmVersion = pnpm --version
Write-Host "[✓] pnpm 已安装: v$pnpmVersion" -ForegroundColor Green
} catch {
Write-Host "[✗] 错误: 未找到 pnpm" -ForegroundColor Red
Write-Host "请运行: npm install -g pnpm" -ForegroundColor Yellow
exit 1
}
# 检查 PostgreSQL
try {
$psqlVersion = psql --version
Write-Host "[✓] PostgreSQL 已安装: $psqlVersion" -ForegroundColor Green
} catch {
Write-Host "[✗] 错误: 未找到 psql 命令" -ForegroundColor Red
Write-Host "请确保 PostgreSQL 已安装且 psql 在系统 PATH 中" -ForegroundColor Yellow
exit 1
}
Write-Host ""
Write-Host "所有前置条件满足!" -ForegroundColor Green
Write-Host ""
# ==================== 安装前端依赖 ====================
Write-Host ">>> 安装前端依赖..." -ForegroundColor Yellow
$frontendPkgJson = "$PSScriptRoot\..\frontend\package.json"
if (Test-Path $frontendPkgJson) {
$nodeModules = "$PSScriptRoot\..\frontend\node_modules"
if (Test-Path $nodeModules) {
Write-Host "[✓] frontend/node_modules 已存在,跳过安装" -ForegroundColor Green
} else {
Write-Host "正在安装前端依赖(这可能需要几分钟)..." -ForegroundColor Yellow
Set-Location "$PSScriptRoot\..\frontend"
pnpm install
if ($LASTEXITCODE -ne 0) {
Write-Host "[✗] 前端依赖安装失败" -ForegroundColor Red
exit 1
}
Set-Location $PSScriptRoot\..
Write-Host "[✓] 前端依赖安装完成" -ForegroundColor Green
}
} else {
Write-Host "[✗] 错误: 未找到 frontend/package.json" -ForegroundColor Red
exit 1
}
Write-Host ""
# ==================== 初始化数据库 ====================
Write-Host ">>> 初始化数据库..." -ForegroundColor Yellow
Write-Host ""
Write-Host "请选择数据库初始化方式:" -ForegroundColor White
Write-Host " 1. 使用自动脚本(推荐)" -ForegroundColor White
Write-Host " 2. 跳过数据库初始化(已手动初始化)" -ForegroundColor White
$choice = Read-Host "请输入选择 (1/2)"
if ($choice -eq "1") {
Write-Host ""
Write-Host "--- 执行数据库初始化脚本 ---" -ForegroundColor Cyan
$initScript = "$PSScriptRoot\init-db.ps1"
if (Test-Path $initScript) {
& $initScript
if ($LASTEXITCODE -ne 0) {
Write-Host "[✗] 数据库初始化失败" -ForegroundColor Red
exit 1
}
} else {
Write-Host "[✗] 错误: 未找到 init-db.ps1 脚本" -ForegroundColor Red
exit 1
}
} else {
Write-Host "[!] 跳过数据库初始化" -ForegroundColor Yellow
}
Write-Host ""
# ==================== 询问是否启动 ====================
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "准备就绪!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "下一步操作:" -ForegroundColor Yellow
Write-Host ""
Write-Host " 1. 启动服务(推荐)" -ForegroundColor White
Write-Host " 这将同时启动后端 API 和前端开发服务器" -ForegroundColor Gray
Write-Host ""
Write-Host " 2. 稍后手动启动" -ForegroundColor White
Write-Host " 后端: go run hougai.go -f etc/hougai-api.yaml" -ForegroundColor Gray
Write-Host " 前端: cd frontend && pnpm dev" -ForegroundColor Gray
Write-Host ""
$startNow = Read-Host "是否现在启动服务? (y/n)"
if ($startNow -ne "y" -and $startNow -ne "Y") {
Write-Host ""
Write-Host "提示: 你也可以使用 VS Code 调试功能" -ForegroundColor Cyan
Write-Host " 按 F5 选择 '全栈: 后端 + 前端 一键启动'" -ForegroundColor Gray
Write-Host ""
Write-Host "感谢使用!" -ForegroundColor Green
exit 0
}
# ==================== 启动服务 ====================
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "启动服务..." -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 启动后端(后台进程)
Write-Host "[1/2] 启动后端 API 服务..." -ForegroundColor Yellow
$backendProcess = Start-Process -FilePath "go" `
-ArgumentList "run", "hougai.go", "-f", "etc/hougai-api.yaml" `
-WorkingDirectory $PSScriptRoot\.. `
-PassThru `
-NoNewWindow `
-RedirectStandardOutput "$PSScriptRoot\..\backend.log" `
-RedirectStandardError "$PSScriptRoot\..\backend-error.log"
Write-Host "[✓] 后端服务已启动 (PID: $($backendProcess.Id))" -ForegroundColor Green
Write-Host " 日志文件: backend.log / backend-error.log" -ForegroundColor Gray
Start-Sleep -Seconds 3
# 启动前端(后台进程)
Write-Host ""
Write-Host "[2/2] 启动前端开发服务器..." -ForegroundColor Yellow
$frontendProcess = Start-Process -FilePath "pnpm" `
-ArgumentList "dev" `
-WorkingDirectory $PSScriptRoot\..\frontend `
-PassThru `
-NoNewWindow `
-RedirectStandardOutput "$PSScriptRoot\..\frontend.log" `
-RedirectStandardError "$PSScriptRoot\..\frontend-error.log"
Write-Host "[✓] 前端开发服务器已启动 (PID: $($frontendProcess.Id))" -ForegroundColor Green
Write-Host " 日志文件: frontend.log / frontend-error.log" -ForegroundColor Gray
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "服务启动完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "访问地址:" -ForegroundColor Yellow
Write-Host " 🌐 管理后台: http://localhost:7070" -ForegroundColor White
Write-Host " 🔌 API 服务: http://localhost:8888" -ForegroundColor White
Write-Host ""
Write-Host "登录信息:" -ForegroundColor Yellow
Write-Host " 👤 用户名: admin" -ForegroundColor White
Write-Host " 🔑 密码: 123456" -ForegroundColor White
Write-Host ""
Write-Host "查看日志:" -ForegroundColor Yellow
Write-Host " 后端: Get-Content backend.log -Tail 20 -Follow" -ForegroundColor Gray
Write-Host " 前端: Get-Content frontend.log -Tail 20 -Follow" -ForegroundColor Gray
Write-Host ""
Write-Host "停止服务: 在各个终端窗口按 Ctrl+C" -ForegroundColor Yellow
Write-Host ""
Write-Host "提示: 建议使用 VS Code 获得更好的开发体验" -ForegroundColor Cyan
Write-Host " 任务: 运行任务 → 启动全栈服务" -ForegroundColor Gray
Write-Host ""