1import { execSync } from 'child_process' 6export const killExistingKey = 'killExisting' as const 7export const killExistingCliSchema = {[killExistingKey]: {flag: true, desc: 'kill existing process on port'}} as const 9export const maybeKillExisting = async (portNo: number) => { 10 if (cliFlag(killExistingKey)) await killPidOnPort(portNo) 13export const killPidOnPort = async (portNo: number) => { 15 const lsofOutput = execSync(`lsof -ti :${portNo}`, { encoding: 'utf8' }).trim() 16 if (!lsofOutput) return 18 const pidA = lsofOutput.split('\n').map(s => parseInt(s, 10)).filter(Boolean) 19 for (const pid of pidA) { 20 betLog('killPidOnPortAI killing', { pid, portNo }) 21 process.kill(pid, 'SIGTERM') 24 // Give the process a moment to exit gracefully 27 // lsof returns non-zero if no process found, which is fine