ignoreCli option.. fixed tests.. removed .listen() and close().. refactor

This commit is contained in:
Lloyd Brookes
2016-07-11 22:19:04 +01:00
parent 7bb45ab0ae
commit c71b2267b7
3 changed files with 75 additions and 74 deletions

View File

@ -1,7 +1,36 @@
#!/usr/bin/env node
'use strict'
const LocalWebServer = require('../')
const path = require('path')
function onServerUp (port, directory, isHttps) {
const ansi = require('ansi-escape-sequences')
const ipList = getIPList()
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${port}}`)
.join(', ')
console.error(ansi.format(
path.resolve(directory || '') === process.cwd()
? `serving at ${ipList}`
: `serving [underline]{${directory}} at ${ipList}`
))
}
function getIPList () {
const flatten = require('reduce-flatten')
const os = require('os')
let ipList = Object.keys(os.networkInterfaces())
.map(key => os.networkInterfaces()[key])
.reduce(flatten, [])
.filter(iface => iface.family === 'IPv4')
ipList.unshift({ address: os.hostname() })
return ipList
}
const LocalWebServer = require('../')
const ws = new LocalWebServer()
ws.listen()
.catch(err => console.error(err.stack))
const server = ws.getServer()
server.on('listening', function () {
onServerUp(ws.options.port, ws.options['static.root'], server.isHttps)
})