update tests.. refactor
This commit is contained in:
@ -11,19 +11,23 @@ const debug = require('./debug')
|
||||
const tool = new CommandLineTool()
|
||||
|
||||
class LocalWebServer extends MiddlewareStack {
|
||||
getApplication () {
|
||||
_init (options) {
|
||||
this.options = this.options || Object.assign(options || {}, collectUserOptions(this.getOptionDefinitions()))
|
||||
}
|
||||
getApplication (options) {
|
||||
this._init(options)
|
||||
const Koa = require('koa')
|
||||
const app = new Koa()
|
||||
app.use(this.compose(this.options))
|
||||
return app
|
||||
}
|
||||
|
||||
getServer () {
|
||||
const options = this.options
|
||||
getServer (options) {
|
||||
const app = this.getApplication(options)
|
||||
options = this.options
|
||||
let key = options.key
|
||||
let cert = options.cert
|
||||
|
||||
const app = this.getApplication()
|
||||
app.on('error', err => {
|
||||
if (options['log-format']) {
|
||||
console.error(ansi.format(err.message, 'red'))
|
||||
@ -53,9 +57,9 @@ class LocalWebServer extends MiddlewareStack {
|
||||
return server
|
||||
}
|
||||
|
||||
start () {
|
||||
const options = collectOptions(this.getOptionDefinitions())
|
||||
this.options = options
|
||||
listen (options, callback) {
|
||||
this._init(options)
|
||||
options = this.options
|
||||
|
||||
if (options.verbose) {
|
||||
debug.setLevel(1)
|
||||
@ -68,21 +72,25 @@ class LocalWebServer extends MiddlewareStack {
|
||||
tool.stop(pkg.version)
|
||||
} else {
|
||||
const server = this.getServer()
|
||||
server.listen(options.port, onServerUp.bind(null, options, server.isHttps))
|
||||
const port = options.port || 8000
|
||||
server.listen(port, () => {
|
||||
onServerUp(port, options.directory, server.isHttps)
|
||||
if (callback) callback()
|
||||
})
|
||||
return server
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onServerUp (options, isHttps) {
|
||||
function onServerUp (port, directory, isHttps) {
|
||||
const ipList = getIPList(isHttps)
|
||||
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.port}}`)
|
||||
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${port}}`)
|
||||
.join(', ')
|
||||
|
||||
console.error(ansi.format(
|
||||
path.resolve(options.directory) === process.cwd()
|
||||
path.resolve(directory || '') === process.cwd()
|
||||
? `serving at ${ipList}`
|
||||
: `serving [underline]{${options.directory}} at ${ipList}`
|
||||
: `serving [underline]{${directory}} at ${ipList}`
|
||||
))
|
||||
}
|
||||
|
||||
@ -101,7 +109,7 @@ function getIPList (isHttps) {
|
||||
/**
|
||||
* Return default, stored and command-line options combined
|
||||
*/
|
||||
function collectOptions (mwOptionDefinitions) {
|
||||
function collectUserOptions (mwOptionDefinitions) {
|
||||
const loadConfig = require('config-master')
|
||||
const stored = loadConfig('local-web-server')
|
||||
const cli = require('../lib/cli-data')
|
||||
@ -110,24 +118,11 @@ function collectOptions (mwOptionDefinitions) {
|
||||
const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions))
|
||||
let cliOptions = tool.getOptions(definitions, cli.usage(definitions))
|
||||
|
||||
/* override built-in defaults with stored config and then command line options */
|
||||
const options = Object.assign({
|
||||
port: 8000,
|
||||
directory: process.cwd()
|
||||
}, stored, cliOptions.server, cliOptions.middleware, cliOptions.misc)
|
||||
/* override stored config with command line options */
|
||||
const options = Object.assign(stored, cliOptions.server, cliOptions.middleware, cliOptions.misc)
|
||||
|
||||
// console.error(require('util').inspect(options, { depth: 3, colors: true }))
|
||||
|
||||
validateOptions(options)
|
||||
return options
|
||||
}
|
||||
|
||||
function validateOptions (options) {
|
||||
if (!t.isNumber(options.port)) {
|
||||
tool.printError('--port must be numeric')
|
||||
console.error(tool.usage)
|
||||
tool.halt()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LocalWebServer
|
||||
|
@ -246,9 +246,9 @@ class MiddlewareStack extends Array {
|
||||
description: 'Root directory, defaults to the current directory.'
|
||||
},
|
||||
middleware: function (cliOptions) {
|
||||
/* update global cliOptions */
|
||||
cliOptions.directory = cliOptions.directory || root || process.cwd()
|
||||
options = Object.assign({ hidden: true }, options)
|
||||
// console.log(root, options, cliOptions)
|
||||
if (cliOptions.directory) {
|
||||
const serve = require('koa-static')
|
||||
return serve(cliOptions.directory, options)
|
||||
|
Reference in New Issue
Block a user