param( [ValidateSet('start', 'restart', 'stop', 'status')] [string]$Action = 'start' ) $ErrorActionPreference = 'Stop' $projectRoot = Split-Path -Parent $PSScriptRoot $runtimeDir = Join-Path $projectRoot 'tmp' $pidFile = Join-Path $runtimeDir 'dev.pid' function Get-TrackedProcessId { if (-not (Test-Path -LiteralPath $pidFile)) { return $null } $value = (Get-Content -LiteralPath $pidFile -Raw).Trim() try { if ($value -match '^\d+$') { $processId = [int]$value $startTimeTicks = $null } else { $state = $value | ConvertFrom-Json $processId = [int]$state.pid $startTimeTicks = [long]$state.start_time_ticks } } catch { Remove-Item -LiteralPath $pidFile -Force return $null } $process = Get-Process -Id $processId -ErrorAction SilentlyContinue if (-not $process -or ($startTimeTicks -and $process.StartTime.ToUniversalTime().Ticks -ne $startTimeTicks)) { Remove-Item -LiteralPath $pidFile -Force return $null } return $processId } function Stop-TrackedProject { $processId = Get-TrackedProcessId if (-not $processId) { Write-Host 'No managed Delivery Desk process is running.' -ForegroundColor Yellow return } Write-Host "Stopping Delivery Desk process tree (PID $processId)..." & taskkill.exe /PID $processId /T /F | Out-Null Remove-Item -LiteralPath $pidFile -Force -ErrorAction SilentlyContinue Start-Sleep -Milliseconds 300 Write-Host 'Delivery Desk stopped.' -ForegroundColor Green } function Test-Endpoint([string]$Url) { try { $response = Invoke-WebRequest -UseBasicParsing -Uri $Url -TimeoutSec 2 return $response.StatusCode -eq 200 } catch { return $false } } if ($Action -eq 'status') { $processId = Get-TrackedProcessId $frontendReady = Test-Endpoint 'http://127.0.0.1:5180/' $backendReady = Test-Endpoint 'http://127.0.0.1:3010/api/health' $processStatus = if ($processId) { "running (PID $processId)" } else { 'not managed' } $frontendStatus = if ($frontendReady) { 'ready' } else { 'unavailable' } $backendStatus = if ($backendReady) { 'ready' } else { 'unavailable' } Write-Host "Process: $processStatus" Write-Host "Frontend 5180: $frontendStatus" Write-Host "Backend 3010: $backendStatus" if ($frontendReady -and $backendReady) { exit 0 } else { exit 1 } } if ($Action -eq 'stop') { Stop-TrackedProject exit 0 } $trackedProcessId = Get-TrackedProcessId if ($trackedProcessId -and $Action -eq 'start') { Write-Host "Delivery Desk is already running (PID $trackedProcessId). Use pnpm dev:restart to restart it." -ForegroundColor Yellow exit 0 } if ($Action -eq 'restart') { Stop-TrackedProject } $pnpm = Get-Command pnpm -ErrorAction Stop New-Item -ItemType Directory -Path $runtimeDir -Force | Out-Null $currentProcess = Get-Process -Id $PID @{ pid = $PID start_time_ticks = $currentProcess.StartTime.ToUniversalTime().Ticks } | ConvertTo-Json -Compress | Set-Content -LiteralPath $pidFile -Encoding ascii -NoNewline Set-Location -LiteralPath $projectRoot Write-Host 'Starting Delivery Desk...' -ForegroundColor Cyan Write-Host 'Frontend: http://localhost:5180 Backend: http://localhost:3010' Write-Host 'Press Ctrl+C to stop.' try { & $pnpm.Source dev exit $LASTEXITCODE } finally { if ((Get-TrackedProcessId) -eq $PID) { Remove-Item -LiteralPath $pidFile -Force } }