Skip to content

Commit

Permalink
Improved message for used ports
Browse files Browse the repository at this point in the history
  • Loading branch information
leo committed Feb 4, 2017
1 parent 56802e5 commit 2f5a55e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 11 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const micro = require('micro')
const args = require('args')
const compress = require('micro-compress')
const detect = require('detect-port')
const {grey} = require('chalk')

// Ours
const listening = require('./listening')
Expand Down Expand Up @@ -49,10 +48,18 @@ const server = flags.unzipped ? micro(handler) : micro(compress(handler))
let port = flags.port

detect(port).then(open => {
if (open !== port) {
let inUse = open !== port

if (inUse) {
port = open
console.log(grey(`Port ${port} already in use, falling back to ${open}`))

inUse = {
old: flags.port,
open
}
}

server.listen(port, async () => await listening(server, current))
server.listen(port, async () => {
return await listening(server, current, inUse)
})
})
8 changes: 7 additions & 1 deletion lib/listening.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const copyToClipboard = async text => {
}
}

module.exports = async (server, current) => {
module.exports = async (server, current, inUse) => {
const details = server.address()
const ipAddress = ip.address()
const url = `http://${ipAddress}:${details.port}`
Expand All @@ -30,6 +30,12 @@ module.exports = async (server, current) => {

if (!process.env.NOW) {
let message = chalk.green('Serving!')

if (inUse) {
message += ' ' + chalk.red(`(on port ${inUse.old},` +
` because ${inUse.open} is already in use)`)
}

message += '\n\n'

const localURL = `http://localhost:${details.port}`
Expand Down

0 comments on commit 2f5a55e

Please sign in to comment.