$ErrorActionPreference = "Stop" param( [int]$Port = 5173 ) function Get-ListeningProcess { param([int]$TargetPort) $listener = Get-NetTCPConnection -LocalPort $TargetPort -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $listener) { return $null } $process = Get-CimInstance Win32_Process -Filter "ProcessId = $($listener.OwningProcess)" -ErrorAction SilentlyContinue if (-not $process) { return $null } return [PSCustomObject]@{ Id = $listener.OwningProcess CommandLine = [string]($process.CommandLine ?? "") } } $existing = Get-ListeningProcess -TargetPort $Port if ($existing) { if ($existing.CommandLine -notmatch "vite(?:\.js)?") { throw "Port $Port is already in use by a non-Vite process: $($existing.CommandLine)" } Stop-Process -Id $existing.Id -Force Start-Sleep -Seconds 1 } $npm = if (Get-Command npm.cmd -ErrorAction SilentlyContinue) { "npm.cmd" } else { "npm" } & $npm run dev -- --host localhost --port $Port --strictPort exit $LASTEXITCODE