You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 lines
7.1 KiB

# GoFaster 增强版启动脚本
# 解决 Electron 窗口启动不稳定的问题
param(
[switch]$Debug,
[switch]$Watch,
[switch]$BackendOnly,
[switch]$FrontendOnly,
[switch]$ForceClean
)
# 设置控制台编码为 UTF-8,解决中文显示乱码问题
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
# 设置环境变量
$env:LANG = "zh_CN.UTF-8"
$env:LC_ALL = "zh_CN.UTF-8"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " GoFaster 增强版启动脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 检查并清理可能的问题
if ($ForceClean) {
Write-Host "执行强制清理..." -ForegroundColor Yellow
# 清理可能的锁文件
$lockFiles = @(
"app/node_modules/.cache",
"app/dist",
"backend/tmp"
)
foreach ($lockFile in $lockFiles) {
if (Test-Path $lockFile) {
try {
Remove-Item -Path $lockFile -Recurse -Force -ErrorAction Stop
Write-Host "已清理: $lockFile" -ForegroundColor Green
} catch {
Write-Host "清理失败: $lockFile - $($_.Exception.Message)" -ForegroundColor Red
}
}
}
# 清理可能的进程
try {
$electronProcesses = Get-Process | Where-Object { $_.ProcessName -like "*electron*" -or $_.ProcessName -like "*node*" }
if ($electronProcesses) {
Write-Host "发现正在运行的 Electron 进程,正在停止..." -ForegroundColor Yellow
$electronProcesses | Stop-Process -Force
Start-Sleep -Seconds 2
}
} catch {
Write-Host "进程清理失败: $($_.Exception.Message)" -ForegroundColor Red
}
}
# 检查依赖
Write-Host "检查依赖..." -ForegroundColor Yellow
# 检查前端依赖
if (-not (Test-Path "app/node_modules")) {
Write-Host "前端依赖未安装,正在安装..." -ForegroundColor Yellow
Set-Location "app"
npm install
if ($LASTEXITCODE -ne 0) {
Write-Host "前端依赖安装失败" -ForegroundColor Red
exit 1
}
Set-Location ".."
}
# 检查后端依赖
if (-not (Test-Path "backend/go.mod")) {
Write-Host "后端 Go 模块未找到" -ForegroundColor Red
exit 1
}
# 检查 air 是否安装
try {
$airVersion = air -v 2>$null
if (-not $airVersion) {
Write-Host "Air 未安装,正在安装..." -ForegroundColor Yellow
go install github.com/air-verse/air@latest
}
} catch {
Write-Host "Air 未安装,正在安装..." -ForegroundColor Yellow
go install github.com/air-verse/air@latest
}
Write-Host "依赖检查完成" -ForegroundColor Green
Write-Host ""
# 选择启动模式
$frontendScript = if ($Debug) { "npm run dev:debug" } elseif ($Watch) { "npm run dev:watch" } else { "npm run dev:watch" }
$backendScript = "air"
# 启动服务
if (-not $FrontendOnly) {
Write-Host "启动后端热重载..." -ForegroundColor Green
$backendProcess = Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd backend; $backendScript" -WindowStyle Normal -PassThru
Write-Host "后端已启动 (PID: $($backendProcess.Id))" -ForegroundColor Green
# 等待后端启动
Write-Host "等待后端启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 5
# 检查后端是否成功启动
$retryCount = 0
$maxRetries = 3
$backendStarted = $false
while ($retryCount -lt $maxRetries -and -not $backendStarted) {
try {
$response = Invoke-WebRequest -Uri "http://localhost:8080/health" -Method GET -TimeoutSec 10 -ErrorAction Stop
if ($response.StatusCode -eq 200) {
Write-Host "后端启动成功" -ForegroundColor Green
$backendStarted = $true
}
} catch {
$retryCount++
if ($retryCount -lt $maxRetries) {
Write-Host "后端可能还在启动中,等待重试... (尝试 $retryCount/$maxRetries)" -ForegroundColor Yellow
Start-Sleep -Seconds 5
} else {
Write-Host "后端启动检查失败,但继续启动前端..." -ForegroundColor Yellow
}
}
}
}
if (-not $BackendOnly) {
Write-Host "启动前端热重载..." -ForegroundColor Green
Write-Host "使用脚本: $frontendScript" -ForegroundColor Cyan
# 确保前端构建目录存在
if (-not (Test-Path "app/dist")) {
Write-Host "前端构建目录不存在,正在预构建..." -ForegroundColor Yellow
Set-Location "app"
npm run build:vue
if ($LASTEXITCODE -ne 0) {
Write-Host "前端预构建失败,尝试继续启动..." -ForegroundColor Yellow
}
Set-Location ".."
}
$frontendProcess = Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd app; $frontendScript" -WindowStyle Normal -PassThru
Write-Host "前端已启动 (PID: $($frontendProcess.Id))" -ForegroundColor Green
}
Write-Host ""
Write-Host "服务启动完成!" -ForegroundColor Green
if (-not $BackendOnly) {
Write-Host "后端: http://localhost:8080" -ForegroundColor Cyan
Write-Host "Swagger: http://localhost:8080/swagger/index.html" -ForegroundColor Cyan
}
if (-not $FrontendOnly) {
Write-Host "前端: Electron 应用 (自动重载已启用)" -ForegroundColor Cyan
Write-Host "注意: 前端现在默认使用 watch 模式以获得更好的热重载体验" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "使用说明:" -ForegroundColor Yellow
Write-Host " - 使用 -Debug 参数启用详细调试信息" -ForegroundColor White
Write-Host " - 使用 -Watch 参数显式启用文件监听" -ForegroundColor White
Write-Host " - 使用 -BackendOnly 仅启动后端" -ForegroundColor White
Write-Host " - 使用 -FrontendOnly 仅启动前端" -ForegroundColor White
Write-Host " - 使用 -ForceClean 强制清理并重新启动" -ForegroundColor White
Write-Host " - 按 Ctrl+C 停止服务" -ForegroundColor White
Write-Host ""
# 显示进程信息
if (-not $BackendOnly -and -not $FrontendOnly) {
Write-Host "进程信息:" -ForegroundColor Yellow
Write-Host " 后端 PID: $($backendProcess.Id)" -ForegroundColor White
Write-Host " 前端 PID: $($frontendProcess.Id)" -ForegroundColor White
Write-Host ""
Write-Host "管理命令:" -ForegroundColor Yellow
Write-Host " 停止后端: Stop-Process -Id $($backendProcess.Id)" -ForegroundColor White
Write-Host " 停止前端: Stop-Process -Id $($frontendProcess.Id)" -ForegroundColor White
Write-Host " 停止所有: Get-Process | Where-Object {$_.ProcessName -eq 'powershell'} | Stop-Process" -ForegroundColor White
}
Write-Host "代码更改将自动触发重新构建和重载!" -ForegroundColor Yellow
Write-Host "前端现在默认使用 watch 模式以获得更好的热重载体验" -ForegroundColor Green
Write-Host "按任意键退出..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")