Skip to content

Commit

Permalink
feat: allow user to kill all processes on a given port (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Sep 16, 2024
1 parent 04a86b6 commit 401d803
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ tsconfig.tsbuildinfo
.env
.DS_Store
.secrets
.wallet.json
.wallet.json

localnet.pid
31 changes: 18 additions & 13 deletions packages/tasks/src/localnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,35 @@ const killProcessOnPort = async (port: number, forceKill: boolean) => {
try {
const output = execSync(`lsof -ti tcp:${port}`).toString().trim();
if (output) {
const pids = output.split("\n");
console.log(
ansis.yellow(`Port ${port} is already in use by process ${output}.`)
ansis.yellow(
`Port ${port} is already in use by process(es): ${pids.join(", ")}.`
)
);

if (forceKill) {
execSync(`kill -9 ${output}`);
console.log(
ansis.green(`Successfully killed process ${output} on port ${port}.`)
);
for (const pid of pids) {
execSync(`kill -9 ${pid}`);
console.log(
ansis.green(`Successfully killed process ${pid} on port ${port}.`)
);
}
} else {
const answer = await confirm({
message: `Do you want to kill the process running on port ${port}?`,
message: `Do you want to kill all processes running on port ${port}?`,
default: true,
});

if (answer) {
execSync(`kill -9 ${output}`);
console.log(
ansis.green(
`Successfully killed process ${output} on port ${port}.`
)
);
for (const pid of pids) {
execSync(`kill -9 ${pid}`);
console.log(
ansis.green(`Successfully killed process ${pid} on port ${port}.`)
);
}
} else {
console.log(ansis.red("Process not killed. Exiting..."));
console.log(ansis.red("Processes not killed. Exiting..."));
process.exit(1);
}
}
Expand Down

0 comments on commit 401d803

Please sign in to comment.