|
|
@ -2,8 +2,6 @@ |
|
|
|
'use strict' |
|
|
|
const ansi = require('ansi-escape-sequences') |
|
|
|
const path = require('path') |
|
|
|
const arrayify = require('array-back') |
|
|
|
const t = require('typical') |
|
|
|
const CommandLineTool = require('command-line-tool') |
|
|
|
const flatten = require('reduce-flatten') |
|
|
|
|
|
|
@ -69,6 +67,7 @@ class LocalWebServer { |
|
|
|
.filter(mw => mw.optionDefinitions) |
|
|
|
.map(mw => mw.optionDefinitions()) |
|
|
|
.reduce(flatten, []) |
|
|
|
.filter(def => def) |
|
|
|
.map(def => { |
|
|
|
def.group = 'middleware' |
|
|
|
return def |
|
|
@ -112,25 +111,29 @@ class LocalWebServer { |
|
|
|
} else if (options.help) { |
|
|
|
tool.stop(usage) |
|
|
|
} else { |
|
|
|
const compose = require('koa-compose') |
|
|
|
const convert = require('koa-convert') |
|
|
|
const middlewareStack = stackModules |
|
|
|
.filter(mw => mw.middleware) |
|
|
|
.map(mw => mw.middleware) |
|
|
|
.map(middleware => middleware(options)) |
|
|
|
.reduce(flatten, []) |
|
|
|
.filter(middleware => middleware) |
|
|
|
.map(convert) |
|
|
|
this.stack = compose(middlewareStack) |
|
|
|
this.stack = stackModules |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
getApplication () { |
|
|
|
const Koa = require('koa') |
|
|
|
const app = new Koa() |
|
|
|
app.use(this.stack) |
|
|
|
const compose = require('koa-compose') |
|
|
|
const convert = require('koa-convert') |
|
|
|
|
|
|
|
const middlewareStack = this.stack |
|
|
|
.filter(mw => mw.middleware) |
|
|
|
.map(mw => mw.middleware(this.options)) |
|
|
|
.reduce(flatten, []) |
|
|
|
.filter(mw => mw) |
|
|
|
.map(convert) |
|
|
|
|
|
|
|
app.use(compose(middlewareStack)) |
|
|
|
app.on('error', err => { |
|
|
|
if (this.options['log.format']) { |
|
|
|
const defaultLogInUse = this.stack.some(mw => mw.constructor.name === 'Log') |
|
|
|
if (defaultLogInUse) { |
|
|
|
if (this.options['log.format']) console.error(ansi.format(err.stack, 'red')) |
|
|
|
} else { |
|
|
|
console.error(ansi.format(err.stack, 'red')) |
|
|
|
} |
|
|
|
}) |
|
|
@ -169,9 +172,9 @@ class LocalWebServer { |
|
|
|
listen () { |
|
|
|
const options = this.options |
|
|
|
const server = this._server = this.getServer() |
|
|
|
return new Promise ((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
server.listen(options.port, () => { |
|
|
|
onServerUp(options.port, options.directory, server.isHttps) |
|
|
|
onServerUp(options.port, options['static.root'], server.isHttps) |
|
|
|
resolve(server) |
|
|
|
}) |
|
|
|
}) |
|
|
@ -207,25 +210,6 @@ function getIPList () { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Return default, stored and command-line options combined |
|
|
|
*/ |
|
|
|
function collectUserOptions (mwOptionDefinitions) { |
|
|
|
const loadConfig = require('config-master') |
|
|
|
const stored = loadConfig('local-web-server') |
|
|
|
const cli = require('../lib/cli-data') |
|
|
|
|
|
|
|
/* parse command line args */ |
|
|
|
const definitions = mwOptionDefinitions |
|
|
|
? cli.optionDefinitions.concat(arrayify(mwOptionDefinitions)) |
|
|
|
: cli.optionDefinitions |
|
|
|
let cliOptions = tool.getOptions(definitions, cli.usage(definitions)) |
|
|
|
|
|
|
|
/* override stored config with command line options */ |
|
|
|
const options = Object.assign(stored, cliOptions.server, cliOptions.middleware, cliOptions.misc) |
|
|
|
return options |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Loads a module by either path or name. |
|
|
|
* @returns {object} |
|
|
|
*/ |
|
|
@ -233,7 +217,6 @@ function loadStack (modulePath) { |
|
|
|
let module |
|
|
|
const tried = [] |
|
|
|
if (modulePath) { |
|
|
|
const fs = require('fs') |
|
|
|
try { |
|
|
|
tried.push(path.resolve(modulePath)) |
|
|
|
module = require(path.resolve(modulePath)) |
|
|
|