-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathport.ts
More file actions
77 lines (66 loc) · 2.13 KB
/
port.ts
File metadata and controls
77 lines (66 loc) · 2.13 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
import { execSync } from "node:child_process";
import { readFileSync } from "node:fs";
import net from "node:net";
function isWSL2(): boolean {
try {
const versionInfo = readFileSync("/proc/version", "utf8");
return versionInfo.includes("microsoft") && versionInfo.includes("WSL2");
} catch {
return false;
}
}
function killLinuxProcess(port: number): void {
try {
execSync(`lsof -ti:${port} | xargs kill -9`, { stdio: "pipe" });
} catch {
// Process might not be running on Linux side
}
}
function killWindowsProcessViaPsh(port: number): void {
try {
const command = `powershell.exe "Get-NetTCPConnection -LocalPort ${port} -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process -Id \$_.OwningProcess -Force -ErrorAction SilentlyContinue }"`;
execSync(command, { stdio: "pipe" });
} catch {
// Process might not be running on Windows side
}
}
function killWSL2Process(port: number): void {
// In WSL2, processes can run on either Linux or Windows side
// We need to check both since we don't know which one is actually using the port
killLinuxProcess(port);
killWindowsProcessViaPsh(port);
}
function killWindowsProcessViaCmd(port: number): void {
const command = `FOR /F "tokens=5" %a in ('netstat -ano ^| findstr :${port}') do taskkill /F /PID %a`;
execSync(command);
}
function killNativeLinuxProcess(port: number): void {
execSync(`lsof -ti:${port} | xargs kill -9`);
}
export async function isPortInUse(port: number): Promise<boolean> {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", () => {
resolve(true); // Port is in use
});
server.once("listening", () => {
server.close(() => {
resolve(false); // Port is free
});
});
server.listen(port);
});
}
export function killProcessOnPort(port: number): void {
try {
if (isWSL2()) {
killWSL2Process(port);
} else if (process.platform === "win32") {
killWindowsProcessViaCmd(port);
} else {
killNativeLinuxProcess(port);
}
} catch (error) {
console.error(`Failed to kill process on port ${port}:`, error);
}
}