-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart_dev.ps1
More file actions
79 lines (70 loc) · 3.13 KB
/
start_dev.ps1
File metadata and controls
79 lines (70 loc) · 3.13 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
param (
[int]$bport = 8000,
[int]$fport = 3000
)
function Get-FreePort {
param([int]$StartingPort)
$port = $StartingPort
while ($true) {
Write-Host "Checking port $port... " -NoNewline
try {
# Try to bind to IPv6Any on the port (covers IPv4 and IPv6)
$tcpListener = [System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::IPv6Any, $port)
$tcpListener.Start()
$tcpListener.Stop()
# Double check with Get-NetTCPConnection for OS-level confirmation
$occupied = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue
if (-not $occupied) {
Write-Host "Available!" -ForegroundColor Green
return $port
}
Write-Host "Occupied (OS reports active connection)." -ForegroundColor Yellow
}
catch {
Write-Host "Occupied (Failed to bind)." -ForegroundColor Yellow
}
$port++
}
}
Write-Host "Finding available ports..." -ForegroundColor Cyan
$actualBport = Get-FreePort -StartingPort $bport
$actualFport = Get-FreePort -StartingPort $fport
$backendUrl = "http://localhost:$actualBport"
$frontendUrl = "http://localhost:$actualFport"
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "Backend (FastAPI) will be available at: " -NoNewline; Write-Host $backendUrl -ForegroundColor Green
Write-Host "Frontend (Svelte) will be available at: " -NoNewline; Write-Host $frontendUrl -ForegroundColor Blue
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "Starting servers in THIS terminal. Press CTRL+C to stop both." -ForegroundColor Yellow
function Stop-ProcessTree {
param([int]$ParentId)
# Recursively find and stop all child processes
Get-CimInstance Win32_Process -Filter "ParentProcessId = $ParentId" | ForEach-Object {
Stop-ProcessTree -ParentId $_.ProcessId
}
# Stop the process itself if it hasn't exited
if (Get-Process -Id $ParentId -ErrorAction SilentlyContinue) {
Stop-Process -Id $ParentId -Force -ErrorAction SilentlyContinue
}
}
# Start Backend in the background without a new window
# Limit reload to the backend directory so it doesn't choke on the .venv junction
$backendProcess = Start-Process powershell -WorkingDirectory $PSScriptRoot -ArgumentList "-NoProfile -Command `"& .\.venv\Scripts\Activate.ps1; python -m uvicorn backend.main:app --reload --reload-dir backend --host 0.0.0.0 --port $actualBport`"" -NoNewWindow -PassThru
# Ensure the backend process is killed when the script stops or is interrupted
try {
# Start Frontend in the foreground
Push-Location frontend
if (-not (Test-Path "node_modules")) {
Write-Host "First time in this worktree? Installing frontend dependencies..." -ForegroundColor Yellow
npm install
}
$env:VITE_API_BASE = "$backendUrl/api"
npm run dev -- --port $actualFport
}
finally {
Write-Host "`nStopping servers..." -ForegroundColor Yellow
if ($backendProcess) {
Stop-ProcessTree -ParentId $backendProcess.Id
}
Pop-Location
}