Browse Source

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

master
Lloyd Brookes 9 years ago
parent
commit
c71b2267b7
  1. 35
      bin/cli.js
  2. 64
      lib/local-web-server.js
  3. 18
      test/test.js

35
bin/cli.js

@ -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)
})

64
lib/local-web-server.js

@ -1,6 +1,5 @@
#!/usr/bin/env node
'use strict'
const ansi = require('ansi-escape-sequences')
const path = require('path')
const CommandLineTool = require('command-line-tool')
const flatten = require('reduce-flatten')
@ -83,6 +82,7 @@ class LocalWebServer {
let options = {}
const allOptionDefinitions = cli.optionDefinitions.concat(middlewareOptionDefinitions)
if (!initOptions.ignoreCli) {
try {
options = commandLineArgs(allOptionDefinitions)
} catch (err) {
@ -93,12 +93,23 @@ class LocalWebServer {
console.error(usage)
tool.halt()
}
}
/* combine in stored config */
options = Object.assign({ port: 8000 }, initOptions, stored || {}, options.server, options.middleware, options.misc)
this.options = options
options = Object.assign(
{ port: 8000 },
initOptions,
stored,
options.server,
options.middleware,
options.misc
)
// console.log(initOptions, stored, options)
/**
* Config
* @type {object}
*/
this.options = options
if (options.verbose) {
// debug.setLevel(1)
@ -146,9 +157,10 @@ class LocalWebServer {
return app
}
getServer () {
getServer (onListening) {
const app = this.getApplication()
const options = this.options
let key = options.key
let cert = options.cert
@ -172,48 +184,10 @@ class LocalWebServer {
const http = require('http')
server = http.createServer(app.callback())
}
return server
}
listen () {
const options = this.options
const server = this._server = this.getServer()
// console.log(options)
return new Promise((resolve, reject) => {
server.listen(options.port, () => {
onServerUp(options.port, options['static.root'], server.isHttps)
resolve(server)
})
})
}
close () {
this._server.close()
}
}
function onServerUp (port, directory, isHttps) {
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}`
))
server.listen(options.port, onListening)
return server
}
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
}
/**

18
test/test.js

@ -9,16 +9,15 @@ test('stack', function (t) {
t.plan(2)
const ws = new LocalWebServer({
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
port: 8100
port: 8100,
ignoreCli: true
})
ws.listen()
.then(() => {
const server = ws.getServer(() => {
return request('http://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/))
.then(ws.close.bind(ws))
.then(server.close.bind(server))
.catch(c.fail(t))
})
.catch(c.fail(t))
})
test('https', function (t) {
@ -26,13 +25,12 @@ test('https', function (t) {
const ws = new LocalWebServer({
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
https: true,
port: 8100
port: 8100,
ignoreCli: true
})
ws.listen()
.then(() => {
const server = ws.getServer(() => {
return request('https://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/))
.then(ws.close.bind(ws))
.then(server.close.bind(server))
})
.catch(c.fail(t))
})
Loading…
Cancel
Save