Repository navigation
Expand file tree
/
Copy pathstart-fullstack.ps1
More file actions
86 lines (76 loc) · 3.35 KB
/
Copy pathstart-fullstack.ps1
File metadata and controls
86 lines (76 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
param(
[int]$BackendPort = 18080,
[int]$FrontendPort = 13000,
[switch]$SkipBuild,
[string]$JavaPath = ""
)
$ErrorActionPreference = "Stop"
$root = $PSScriptRoot
$runtime = Join-Path $root ".runtime"
$frontend = Join-Path $root "frontend"
. (Join-Path $PSScriptRoot "scripts\resolve-java.ps1")
$javaRuntime = Use-LabFlowJava -JavaPath $JavaPath
$javaExe = $javaRuntime.Executable
if (-not (Get-Command npm.cmd -ErrorAction SilentlyContinue)) {
throw "未找到 npm.cmd,请先安装 Node.js 22+"
}
New-Item -ItemType Directory -Force -Path $runtime | Out-Null
foreach ($name in @("backend.pid", "frontend.pid")) {
$pidFile = Join-Path $runtime $name
if (Test-Path $pidFile) {
$savedPid = [int](Get-Content $pidFile -Raw)
if (Get-Process -Id $savedPid -ErrorAction SilentlyContinue) {
throw "$name 对应的进程仍在运行,请先执行 .\stop-fullstack.ps1"
}
Remove-Item $pidFile -Force
}
}
if (-not $SkipBuild) {
& "$root\mvnw.cmd" clean package
if ($LASTEXITCODE -ne 0) { throw "后端构建失败" }
Push-Location $frontend
try {
& npm.cmd run build
if ($LASTEXITCODE -ne 0) { throw "前端构建失败" }
} finally {
Pop-Location
}
}
$jar = Get-ChildItem "$root\target\lab-equipment-platform-*.jar" | Where-Object { $_.Name -notlike "*.original" } | Select-Object -First 1
if (-not $jar) { throw "未找到后端 jar,请先运行 .\build.ps1" }
$backendOut = Join-Path $runtime "backend.stdout.log"
$backendErr = Join-Path $runtime "backend.stderr.log"
$backendProcess = Start-Process -FilePath $javaExe -ArgumentList @("-jar", $jar.FullName, "--server.port=$BackendPort") -WorkingDirectory $root -WindowStyle Hidden -RedirectStandardOutput $backendOut -RedirectStandardError $backendErr -PassThru
[IO.File]::WriteAllText((Join-Path $runtime "backend.pid"), [string]$backendProcess.Id)
$backendReady = $false
for ($i = 0; $i -lt 50; $i++) {
try {
$health = Invoke-RestMethod -Uri "http://127.0.0.1:$BackendPort/actuator/health" -TimeoutSec 2
if ($health.status -eq "UP") { $backendReady = $true; break }
} catch {}
Start-Sleep -Milliseconds 500
}
if (-not $backendReady) {
& "$root\stop-fullstack.ps1"
throw "后端未能就绪,请查看 $backendErr"
}
$env:BACKEND_BASE_URL = "http://127.0.0.1:$BackendPort"
$frontendOut = Join-Path $runtime "frontend.stdout.log"
$frontendErr = Join-Path $runtime "frontend.stderr.log"
$frontendProcess = Start-Process -FilePath "npm.cmd" -ArgumentList @("run", "dev", "--", "--port", [string]$FrontendPort, "--hostname", "0.0.0.0") -WorkingDirectory $frontend -WindowStyle Hidden -RedirectStandardOutput $frontendOut -RedirectStandardError $frontendErr -PassThru
[IO.File]::WriteAllText((Join-Path $runtime "frontend.pid"), [string]$frontendProcess.Id)
$frontendReady = $false
for ($i = 0; $i -lt 50; $i++) {
try {
$response = Invoke-WebRequest -UseBasicParsing -Uri "http://127.0.0.1:$FrontendPort/" -TimeoutSec 2
if ($response.StatusCode -eq 200) { $frontendReady = $true; break }
} catch {}
Start-Sleep -Milliseconds 500
}
if (-not $frontendReady) {
& "$root\stop-fullstack.ps1"
throw "前端未能就绪,请查看 $frontendErr"
}
Write-Host "LabFlow 已启动"
Write-Host "前端:http://localhost:$FrontendPort"
Write-Host "后端:http://127.0.0.1:$BackendPort"