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: add port ranges #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const server = http.createServer((req, res) => {

server.listen(port, () => {
setTimeout(() => {

// Currently you can kill ports running on TCP or UDP protocols
kill(port, 'tcp')
.then(console.log)
Expand Down Expand Up @@ -128,7 +128,10 @@ $ npx kill-port 8080
$ npx kill-port 9000 --method udp
# Kill multiple ports
$ npx kill-port --port 8080,5000,3000
$ npx kill-port --port 3000-5000
$ npx kill-port 9000 3000 5000
$ npx kill-port 3000-5000
$ npx kill-port 3000,4000,5000
```

## Contributing
Expand Down
64 changes: 50 additions & 14 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,57 @@ const kill = require('./')
const args = require('get-them-args')(process.argv.slice(2))

const verbose = args.verbose || false
let port = args.port ? args.port.toString().split(',') : args.unknown
const method = args.method || 'tcp'

if (!Array.isArray(port)) {
port = [port]
const portArgStr = args.port
? args.port.toString()
: args.unknown.toString()

if (isPortsList(portArgStr)) {
const ports = portArgStr.split(",").filter(Boolean)
return killAllPorts(ports, { method, verbose })
}

if (isPortsRange(portArgStr)) {
const [min, max] = getMinMaxFromRange(portArgStr)
const ports = makeArrayFromRange(min, max)
return killAllPorts(ports, { method, verbose })
}

killAllPorts([portArgStr], { method, verbose })

function killAllPorts(ports, { method, verbose }) {
Promise.all(ports.map(current => {
return kill(current, method)
.then((result) => {
console.log(`Process on port ${current} killed`)
verbose && console.log(result)
})
.catch((error) => {
console.log(`Could not kill process on port ${current}. ${error.message}.`)
verbose && console.log(error)
})
}))
}

function isPortsList(argStr) {
return argStr.includes(",")
}

function isPortsRange(argStr) {
return argStr.includes("-")
}

Promise.all(port.map(current => {
return kill(current, method)
.then((result) => {
console.log(`Process on port ${current} killed`)
verbose && console.log(result)
})
.catch((error) => {
console.log(`Could not kill process on port ${port}. ${error.message}.`)
verbose && console.log(error)
})
}))
function getMinMaxFromRange(rangeStr) {
return rangeStr
.split("-")
.slice(0, 2)
.map(v => parseInt(v))
.sort((a, b) => a - b)
}

function makeArrayFromRange(min, max) {
const diff = max - min

return Array.from({ length: diff + 1 }).map((_, i) => min + i)
}