Skip to content

Commit a9d267f

Browse files
fix: prevent unnecessary re-prompting when port is provided via CLI (#1)
- If a port is passed via CLI, exit gracefully instead of re-asking. - Added `isUserInput` parameter in `killProcess()` to handle prompt logic. - Improved error handling and refined console messages. - Introduced `exit()` function for cleaner script termination. - Ensured `listPorts()` exits properly when no active ports are found.
1 parent 5d6adec commit a9d267f

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

index.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,15 @@ const rl = readline.createInterface({
1010

1111
const isWindows = process.platform === "win32";
1212

13-
const killProcess = (port) => {
13+
const killProcess = (port, isUserInput = false) => {
1414
const checkCommand = isWindows
1515
? `netstat -ano | findstr :${port}`
1616
: `lsof -ti :${port}`;
1717

1818
exec(checkCommand, (checkError, checkStdout) => {
19-
if (checkError) {
20-
console.error(`❌ Error checking port ${port}: ${checkError.message}`);
21-
return askForPort();
22-
}
23-
24-
if (!checkStdout) {
19+
if (checkError || !checkStdout.trim()) {
2520
console.log(`⚠️ No process is running on port ${port}.`);
26-
return askForPort();
21+
return isUserInput ? askForPort() : exit();
2722
}
2823

2924
rl.question(
@@ -41,11 +36,11 @@ const killProcess = (port) => {
4136
} else {
4237
console.log(`✅ Port ${port} has been freed.`);
4338
}
44-
askForPort();
39+
isUserInput ? askForPort() : exit();
4540
});
4641
} else {
4742
console.log("❌ Operation canceled.");
48-
askForPort();
43+
isUserInput ? askForPort() : exit();
4944
}
5045
}
5146
);
@@ -58,43 +53,41 @@ const listPorts = () => {
5853
: `lsof -i -P -n | grep LISTEN`;
5954

6055
exec(listCommand, (error, stdout) => {
61-
if (error) {
62-
console.error(`❌ Error listing ports: ${error.message}`);
63-
return askForPort();
64-
}
65-
66-
if (!stdout) {
56+
if (error || !stdout.trim()) {
6757
console.log("✅ No active ports found.");
68-
return askForPort();
58+
return exit();
6959
}
7060

7161
console.log("🔍 Active ports:\n");
7262
console.log(stdout);
73-
7463
askForPort();
7564
});
7665
};
7766

7867
const askForPort = () => {
7968
rl.question("Enter a port to kill (or 'q' to exit): ", (port) => {
8069
if (port.toLowerCase() === "q") {
81-
console.log("👋 Exiting...");
82-
rl.close();
83-
process.exit(0);
70+
exit();
8471
} else if (!port) {
8572
console.log("⚠️ No port entered. Try again.");
8673
askForPort();
8774
} else {
88-
killProcess(port);
75+
killProcess(port, true);
8976
}
9077
});
9178
};
9279

80+
const exit = () => {
81+
console.log("👋 Exiting...");
82+
rl.close();
83+
process.exit(0);
84+
};
85+
9386
const port = process.argv[2];
9487

9588
if (!port) {
9689
console.log("⚠️ No port provided. Searching for running ports...");
9790
listPorts();
9891
} else {
99-
killProcess(port);
92+
killProcess(port, false);
10093
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kill-my-port",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Easily kill processes running",
55
"type": "module",
66
"main": "index.js",

0 commit comments

Comments
 (0)