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

Faster 10 time for mac os #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ module.exports = function (port, method = 'tcp') {
})
}

return sh('lsof -i -P')
const protocol = method === 'udp' ? 'udp' : 'tcp';

return sh(`lsof -i ${protocol}:${port}`)
.then(res => {
const { stdout } = res
if (!stdout) return res
const lines = stdout.split('\n')
const existProccess = lines.filter((line) => line.match(new RegExp(`:*${port}`))).length > 0
if (!existProccess) return Promise.reject(new Error('No process running on port'))

return sh(
`lsof -i ${method === 'udp' ? 'udp' : 'tcp'}:${port} | grep ${method === 'udp' ? 'UDP' : 'LISTEN'} | awk '{print $2}' | xargs kill -9`
)
const { stdout } = res;

if (!stdout || !stdout.includes(protocol.toUpperCase())) {
return Promise.reject(new Error('No process running on port'));
}

return sh(`kill -9 $(lsof -t -i ${protocol}:${port})`);
})
.catch(error => {
return console.log(`Failed to terminate process on port ${portNumber}: ${error.message}`);
});
}