Browse Source

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

master
Lloyd Brookes 8 years ago
parent
commit
c71b2267b7
  1. 35
      bin/cli.js
  2. 82
      lib/local-web-server.js
  3. 32
      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)
})

82
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,23 +82,35 @@ class LocalWebServer {
let options = {}
const allOptionDefinitions = cli.optionDefinitions.concat(middlewareOptionDefinitions)
try {
options = commandLineArgs(allOptionDefinitions)
} catch (err) {
tool.printError(err)
tool.printError(allOptionDefinitions.map(def => {
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
}).join('\n'))
console.error(usage)
tool.halt()
if (!initOptions.ignoreCli) {
try {
options = commandLineArgs(allOptionDefinitions)
} catch (err) {
tool.printError(err)
tool.printError(allOptionDefinitions.map(def => {
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
}).join('\n'))
console.error(usage)
tool.halt()
}
}
/* combine in stored config */
options = Object.assign({ port: 8000 }, initOptions, stored || {}, options.server, options.middleware, options.misc)
options = Object.assign(
{ port: 8000 },
initOptions,
stored,
options.server,
options.middleware,
options.misc
)
/**
* Config
* @type {object}
*/
this.options = options
// console.log(initOptions, stored, 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,50 +184,12 @@ 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()
server.listen(options.port, onListening)
return server
}
}
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}`
))
}
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
}
/**
* Loads a module by either path or name.
* @returns {object}

32
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
})
const server = ws.getServer(() => {
return request('http://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/))
.then(server.close.bind(server))
.catch(c.fail(t))
})
ws.listen()
.then(() => {
return request('http://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/))
.then(ws.close.bind(ws))
.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
})
const server = ws.getServer(() => {
return request('https://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/))
.then(server.close.bind(server))
})
ws.listen()
.then(() => {
return request('https://localhost:8100/')
.then(c.checkResponse(t, 200, /1234512345/))
.then(ws.close.bind(ws))
})
.catch(c.fail(t))
})
Loading…
Cancel
Save