(port: number)
| 258 | |
| 259 | // Kill process by port |
| 260 | function killProcessByPort(port: number) { |
| 261 | const { exec } = require('child_process') |
| 262 | |
| 263 | if (process.platform === 'win32') { |
| 264 | // Windows: use netstat and taskkill |
| 265 | exec(`netstat -ano | findstr :${port}`, (error, stdout) => { |
| 266 | if (!error && stdout) { |
| 267 | const lines = stdout.split('\n') |
| 268 | const pids = new Set() |
| 269 | |
| 270 | lines.forEach((line) => { |
| 271 | const match = line.match(/\s+(\d+)\s*$/) |
| 272 | if (match) { |
| 273 | pids.add(match[1]) |
| 274 | } |
| 275 | }) |
| 276 | |
| 277 | pids.forEach((pid) => { |
| 278 | if (pid !== '0') { |
| 279 | logToBackendFile(`Killing process on port ${port}, PID: ${pid}`) |
| 280 | exec(`taskkill /PID ${pid} /F`, (killError) => { |
| 281 | if (killError) { |
| 282 | logToBackendFile(`Failed to kill PID ${pid}: ${killError.message}`) |
| 283 | } else { |
| 284 | logToBackendFile(`Successfully killed PID ${pid}`) |
| 285 | } |
| 286 | }) |
| 287 | } |
| 288 | }) |
| 289 | } |
| 290 | }) |
| 291 | } else { |
| 292 | // Unix/Linux/macOS: use lsof and kill |
| 293 | exec(`lsof -ti:${port}`, (error, stdout) => { |
| 294 | if (!error && stdout) { |
| 295 | const pids = stdout |
| 296 | .trim() |
| 297 | .split('\n') |
| 298 | .filter((pid) => pid) |
| 299 | pids.forEach((pid) => { |
| 300 | logToBackendFile(`Killing process on port ${port}, PID: ${pid}`) |
| 301 | try { |
| 302 | process.kill(parseInt(pid), 'SIGKILL') |
| 303 | logToBackendFile(`Successfully killed PID ${pid}`) |
| 304 | } catch (killError) { |
| 305 | logToBackendFile(`Failed to kill PID ${pid}: ${killError}`) |
| 306 | } |
| 307 | }) |
| 308 | } else if (error) { |
| 309 | logToBackendFile(`No process found on port ${port}: ${error.message}`) |
| 310 | } |
| 311 | }) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Ensure backend is running (start if not running) |
| 316 | export async function ensureBackendRunning(mainWindow: BrowserWindow) { |
no test coverage detected