Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow user to kill all processes on a given port #38

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading