-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (147 loc) · 3.98 KB
/
index.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
import chalk from "chalk";
import inquirer from "inquirer";
import chalkAnimation from "chalk-animation";
import { execSync } from "child_process";
import columnify from "columnify";
let portName;
let currentProccesses = [];
let pidIndexAll = false;
let pidIndexes = [];
async function welcome() {
console.log(chalk.blue("Port Killer CLI \n"));
}
async function finish() {
console.log(`${chalk.blue("\nExiting...")}`);
}
async function killPIDs() {
if (pidIndexAll || pidIndexes.length > 0) {
const rainbowTitle = chalkAnimation.rainbow("Killing ports... \n");
rainbowTitle.stop();
if (pidIndexAll) {
currentProccesses.forEach((process) => {
const pid = process.pid;
execSync(`kill -9 ${pid}`);
console.log(`Killed PID: ${pid}`);
});
} else if (pidIndexes.length > 1) {
pidIndexes.forEach((pidIndex) => {
const pid = currentProccesses[pidIndex]?.pid;
if (pid) {
execSync(`kill -9 ${pid}`);
console.log(`Killed PID: ${pid}`);
} else {
console.log(`Process index [${pidIndexes[0]}] not found.`);
}
});
} else if (pidIndexes.length == 1) {
const pid = currentProccesses[pidIndexes[0]]?.pid;
if (pid) {
execSync(`kill -9 ${pid}`);
console.log(`Killed PID: ${pid}`);
} else {
console.log(`Process index [${pidIndexes[0]}] not found.`);
}
}
} else {
console.log(chalk.red(" \nNo ports to kill!"));
}
}
async function askPort() {
const answers = await inquirer.prompt({
name: "port_name",
type: "input",
message: "Search by port:",
default() {
return "Default: 3000";
},
});
portName = answers.port_name || 3000;
}
async function getPorts() {
try {
const stdout = execSync(`lsof -i :${portName}`);
const data = [];
const processes = [];
stdout
.toString()
.split("\n")
.forEach((line) => {
if (line.trim() !== "") {
processes.push(line.replace(/\s+/g, " ").trim());
}
});
processes.shift();
processes.forEach((process, i) => {
const processInfo = process.split(" ");
const index = i;
const name = processInfo[0];
const pid = processInfo[1];
const user = processInfo[2];
data.push({
index,
name,
pid,
user,
});
});
const uniqueResults = [
...new Map(data.map((process) => [process.pid, process])).values(),
];
uniqueResults.forEach((process, i) => {
const index = i;
uniqueResults[i].index = index;
});
currentProccesses = uniqueResults;
var columns = columnify(uniqueResults, {
columns: ["index", "name", "pid", "user"],
minWidth: 20,
columnSplitter: " | ",
headingTransform: function (heading) {
return `${chalk.blue(heading[0].toUpperCase() + heading.slice(1))}`;
},
config: {
pid: {
maxWidth: 40,
headingTransform: function (heading) {
return `${chalk.blue(heading.toUpperCase())}`;
},
},
name: {},
index: {
dataTransform: function (data) {
return `[${data.toLowerCase()}]`;
},
},
},
});
console.log(``);
console.log(columns);
console.log(``);
const answers = await inquirer.prompt({
name: "pid_indexes",
type: "input",
message: "Hunt by the index:",
default() {
return "0 or 1,3,4 or all";
},
});
console.log(``);
if (answers.pid_indexes === "all") {
pidIndexAll = true;
} else if (answers.pid_indexes.includes(",")) {
const tmpAnswers = answers.pid_indexes.split(",");
tmpAnswers.forEach((pidIndex) => {
pidIndexes.push(parseInt(pidIndex));
});
} else {
pidIndexes.push(parseInt(answers.pid_indexes));
}
} catch (err) {}
}
console.clear();
await welcome();
await askPort();
await getPorts();
await killPIDs();
await finish();