|
| 1 | +/** |
| 2 | + * kill-dev v2 - Ultimate Safe Process Cleanup Script |
| 3 | + * |
| 4 | + * Strategy: |
| 5 | + * 1. Clean up processes occupying ports (most critical) |
| 6 | + * 2. Wait 2 seconds for parent processes to exit naturally |
| 7 | + * 3. Check ports again to ensure complete cleanup |
| 8 | + * |
| 9 | + * About "missed processes": |
| 10 | + * - Parent processes (pnpm) usually exit automatically after child processes terminate |
| 11 | + * - vite build --watch doesn't occupy ports, exits with parent process |
| 12 | + * - Even if missed, they don't occupy ports and won't block new starts |
| 13 | + */ |
| 14 | + |
| 15 | +const { execSync } = require('child_process'); |
| 16 | +const os = require('os'); |
| 17 | + |
| 18 | +console.log('🧹 Safely cleaning up project development processes...\n'); |
| 19 | + |
| 20 | +// Limit to project-specific ports so we do not kill other apps using defaults like 5173 |
| 21 | +const PORTS = [18181]; |
| 22 | + |
| 23 | +/** |
| 24 | + * Cross-platform non-blocking sleep |
| 25 | + */ |
| 26 | +function wait(ms) { |
| 27 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 28 | +} |
| 29 | + |
| 30 | +function killByPorts(round = 1) { |
| 31 | + const platform = os.platform(); |
| 32 | + let killedCount = 0; |
| 33 | + const killedPids = new Set(); |
| 34 | + |
| 35 | + console.log(`🔍 Round ${round}: Checking port occupation...`); |
| 36 | + |
| 37 | + if (platform === 'win32') { |
| 38 | + for (const port of PORTS) { |
| 39 | + try { |
| 40 | + const output = execSync(`netstat -ano | findstr ":${port}"`, { |
| 41 | + encoding: 'utf-8', |
| 42 | + stdio: 'pipe' |
| 43 | + }); |
| 44 | + |
| 45 | + // Windows uses \r\n, so split by \n and trim each line |
| 46 | + const lines = output.split('\n').map(line => line.trim()).filter(Boolean); |
| 47 | + lines.forEach(line => { |
| 48 | + const match = line.match(/LISTENING\s+(\d+)/); |
| 49 | + if (match && !killedPids.has(match[1])) { |
| 50 | + const pid = match[1]; |
| 51 | + try { |
| 52 | + execSync(`taskkill /F /PID ${pid}`, { stdio: 'pipe' }); |
| 53 | + console.log(` ✓ Killed process on port ${port} (PID: ${pid})`); |
| 54 | + killedPids.add(pid); |
| 55 | + killedCount++; |
| 56 | + } catch (e) { |
| 57 | + // Process may have already exited |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + } catch (error) { |
| 62 | + // Port not occupied or command failed |
| 63 | + } |
| 64 | + } |
| 65 | + } else { |
| 66 | + for (const port of PORTS) { |
| 67 | + try { |
| 68 | + const output = execSync(`lsof -ti :${port}`, { |
| 69 | + encoding: 'utf-8', |
| 70 | + stdio: 'pipe' |
| 71 | + }); |
| 72 | + |
| 73 | + const pids = output.split('\n').map(pid => pid.trim()).filter(Boolean); |
| 74 | + pids.forEach(pid => { |
| 75 | + if (!killedPids.has(pid)) { |
| 76 | + try { |
| 77 | + execSync(`kill -9 ${pid}`, { stdio: 'pipe' }); |
| 78 | + console.log(` ✓ Killed process on port ${port} (PID: ${pid})`); |
| 79 | + killedPids.add(pid); |
| 80 | + killedCount++; |
| 81 | + } catch (e) { |
| 82 | + // Process may have already exited |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + } catch (error) { |
| 87 | + // Port not occupied or command failed |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (killedCount === 0) { |
| 93 | + console.log(' ℹ️ No processes found occupying ports'); |
| 94 | + } |
| 95 | + |
| 96 | + return killedCount; |
| 97 | +} |
| 98 | + |
| 99 | +async function main() { |
| 100 | + try { |
| 101 | + // First round cleanup |
| 102 | + let totalKilled = killByPorts(1); |
| 103 | + |
| 104 | + if (totalKilled > 0) { |
| 105 | + // Wait for parent processes to exit naturally (cross-platform) |
| 106 | + console.log('\n⏳ Waiting 2 seconds for parent processes to exit naturally...\n'); |
| 107 | + await wait(2000); |
| 108 | + |
| 109 | + // Second round cleanup (ensure no missed processes) |
| 110 | + const round2Killed = killByPorts(2); |
| 111 | + totalKilled += round2Killed; |
| 112 | + |
| 113 | + if (round2Killed > 0) { |
| 114 | + console.log('\n💡 Found and cleaned up missed processes'); |
| 115 | + } else { |
| 116 | + console.log('\n✅ Confirmed: All processes completely cleaned up'); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + console.log(`\n📊 Total cleaned: ${totalKilled} process(es)`); |
| 121 | + |
| 122 | + if (totalKilled > 0) { |
| 123 | + console.log('✅ Cleanup complete! You can now run pnpm dev:fresh'); |
| 124 | + } else { |
| 125 | + console.log('ℹ️ No processes found that need cleanup'); |
| 126 | + } |
| 127 | + |
| 128 | + process.exit(0); |
| 129 | + } catch (error) { |
| 130 | + console.error('\n❌ Cleanup failed:', error.message); |
| 131 | + console.error('\n💡 Please try:'); |
| 132 | + console.error(' 1. Run this script again'); |
| 133 | + console.error(' 2. Or manually terminate related processes in Task Manager'); |
| 134 | + process.exit(1); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +main(); |
0 commit comments