Skip to content

Commit

Permalink
Better way of handling used ports
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Feb 4, 2017
1 parent 3a9945f commit f7d278a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
14 changes: 6 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const path = require('path')
const micro = require('micro')
const args = require('args')
const compress = require('micro-compress')
const getPort = require('get-port')
const portUsed = require('tcp-port-used')
const detect = require('detect-port')
const {grey} = require('chalk')

// Ours
Expand Down Expand Up @@ -47,13 +46,12 @@ const handler = async (req, res) => {
}

const server = flags.unzipped ? micro(handler) : micro(compress(handler))
let port = flags.port

portUsed.check(flags.port, '127.0.0.1').then(async inUse => {
let port = flags.port

if (inUse) {
port = await getPort()
console.log(grey(`Port ${flags.port} already in use, falling back to ${port}`))
detect(port).then(open => {
if (open !== port) {
port = open
console.log(grey(`Port ${port} already in use, falling back to ${open}`))
}

server.listen(port, async () => await listening(server, current))
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
"basic-auth": "^1.1.0",
"chalk": "^1.1.3",
"copy-paste": "^1.3.0",
"detect-port": "^1.1.0",
"filesize": "^3.5.4",
"fs-promise": "^1.0.0",
"get-port": "^2.1.0",
"handlebars": "^4.0.6",
"ip": "^1.1.4",
"micro": "^6.2.1",
Expand All @@ -65,7 +65,6 @@
"node-version": "^1.0.0",
"path-type": "^2.0.0",
"send": "^0.14.2",
"tcp-port-used": "^0.1.2",
"update-notifier": "^1.0.3"
}
}

0 comments on commit f7d278a

Please sign in to comment.