Repository navigation
Expand file tree
/
Copy pathdev.ps1
More file actions
93 lines (78 loc) · 3.61 KB
/
Copy pathdev.ps1
File metadata and controls
93 lines (78 loc) · 3.61 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
87
88
89
90
91
92
93
<#
.SYNOPSIS
Starts the PyTransDatRO FastAPI backend and Vite frontend development servers.
.DESCRIPTION
Launches Uvicorn (REST API) using the project conda environment and Vite (Web App),
opens the application in the default web browser, and shuts down both child processes
cleanly when Enter or Ctrl+C is pressed.
#>
[CmdletBinding()]
param(
[string]$PythonPath = "D:\anaconda3\envs\pytransdat\python.exe",
[int]$ApiPort = 8000,
[switch]$NoBrowser
)
$rootDir = $PSScriptRoot
$webappDir = Join-Path $rootDir "webapp"
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host " PyTransDatRO Development Runner " -ForegroundColor Cyan
Write-Host "=================================================" -ForegroundColor Cyan
# 1. Validate Python environment
if (-not (Test-Path $PythonPath)) {
Write-Warning "Conda python executable not found at: $PythonPath"
Write-Host "Falling back to system 'python'..." -ForegroundColor Yellow
$PythonPath = "python"
}
# 2. Check for port conflicts on API port
$existingApi = Get-NetTCPConnection -LocalPort $ApiPort -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess -Unique
if ($existingApi) {
Write-Warning "Port $ApiPort is currently occupied by process ID(s): $($existingApi -join ', ')."
Write-Host "Please close the process or choose another port." -ForegroundColor Yellow
}
# 3. Start Backend (FastAPI / Uvicorn)
Write-Host "[1/2] Starting REST API on http://127.0.0.1:$ApiPort..." -ForegroundColor Green
$backendArgs = "-m uvicorn api.main:app --host 127.0.0.1 --port $ApiPort --reload"
$backendProc = Start-Process -FilePath $PythonPath `
-ArgumentList $backendArgs `
-WorkingDirectory $rootDir `
-PassThru
# 4. Start Frontend (Vite)
Write-Host "[2/2] Starting Web App (Vite)..." -ForegroundColor Green
$frontendProc = Start-Process -FilePath "cmd.exe" `
-ArgumentList "/c npm run dev" `
-WorkingDirectory $webappDir `
-PassThru
# Brief pause to let Vite bind and display the port
Start-Sleep -Seconds 2
if (-not $NoBrowser) {
Write-Host "Opening web app in default browser..." -ForegroundColor Magenta
Start-Process "http://localhost:5173"
}
Write-Host ""
Write-Host "Development servers are active:" -ForegroundColor Cyan
Write-Host " - REST API: http://127.0.0.1:$ApiPort" -ForegroundColor White
Write-Host " - Swagger Docs: http://127.0.0.1:$ApiPort/docs" -ForegroundColor White
Write-Host " - Web App: http://localhost:5173" -ForegroundColor White
Write-Host ""
Write-Host "Press [Enter] to stop all servers and exit..." -ForegroundColor Yellow
try {
[void][System.Console]::ReadLine()
}
finally {
Write-Host "`nStopping development servers..." -ForegroundColor Red
if ($backendProc -and -not $backendProc.HasExited) {
Stop-Process -Id $backendProc.Id -Force -ErrorAction SilentlyContinue
# Kill any child python processes spawned by reload
Get-CimInstance Win32_Process | Where-Object { $_.ParentProcessId -eq $backendProc.Id } | ForEach-Object {
Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue
}
}
if ($frontendProc -and -not $frontendProc.HasExited) {
# Kill cmd and node child processes
Get-CimInstance Win32_Process | Where-Object { $_.ParentProcessId -eq $frontendProc.Id } | ForEach-Object {
Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue
}
Stop-Process -Id $frontendProc.Id -Force -ErrorAction SilentlyContinue
}
Write-Host "All servers stopped cleanly. Goodbye!" -ForegroundColor Green
}