Skip to content

Commit

Permalink
Add isPortTaken function
Browse files Browse the repository at this point in the history
  • Loading branch information
0xGabi committed Jul 29, 2019
1 parent 2d9adee commit 826103c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/cli-utils/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aragon/cli-utils",
"version": "0.0.5",
"version": "0.0.6",
"description": "Common building blocks for CLI tools",
"main": "",
"scripts": {
Expand Down
27 changes: 27 additions & 0 deletions packages/cli-utils/src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const net = require('net')

const isPortTaken = async (port, opts) => {
opts = Object.assign({ timeout: 1000 }, opts)

return new Promise(resolve => {
const socket = new net.Socket()

const onError = () => {
socket.destroy()
resolve(false)
}

socket.setTimeout(opts.timeout)
socket.on('error', onError)
socket.on('timeout', onError)

socket.connect(port, opts.host, () => {
socket.end()
resolve(true)
})
})
}

module.exports = {
isPortTaken,
}

0 comments on commit 826103c

Please sign in to comment.