|
@ -2,8 +2,6 @@ |
|
|
'use strict' |
|
|
'use strict' |
|
|
const ansi = require('ansi-escape-sequences') |
|
|
const ansi = require('ansi-escape-sequences') |
|
|
const path = require('path') |
|
|
const path = require('path') |
|
|
const arrayify = require('array-back') |
|
|
|
|
|
const t = require('typical') |
|
|
|
|
|
const CommandLineTool = require('command-line-tool') |
|
|
const CommandLineTool = require('command-line-tool') |
|
|
const flatten = require('reduce-flatten') |
|
|
const flatten = require('reduce-flatten') |
|
|
|
|
|
|
|
@ -69,6 +67,7 @@ class LocalWebServer { |
|
|
.filter(mw => mw.optionDefinitions) |
|
|
.filter(mw => mw.optionDefinitions) |
|
|
.map(mw => mw.optionDefinitions()) |
|
|
.map(mw => mw.optionDefinitions()) |
|
|
.reduce(flatten, []) |
|
|
.reduce(flatten, []) |
|
|
|
|
|
.filter(def => def) |
|
|
.map(def => { |
|
|
.map(def => { |
|
|
def.group = 'middleware' |
|
|
def.group = 'middleware' |
|
|
return def |
|
|
return def |
|
@ -112,25 +111,29 @@ class LocalWebServer { |
|
|
} else if (options.help) { |
|
|
} else if (options.help) { |
|
|
tool.stop(usage) |
|
|
tool.stop(usage) |
|
|
} else { |
|
|
} 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 () { |
|
|
getApplication () { |
|
|
const Koa = require('koa') |
|
|
const Koa = require('koa') |
|
|
const app = new 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 => { |
|
|
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')) |
|
|
console.error(ansi.format(err.stack, 'red')) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
@ -169,9 +172,9 @@ class LocalWebServer { |
|
|
listen () { |
|
|
listen () { |
|
|
const options = this.options |
|
|
const options = this.options |
|
|
const server = this._server = this.getServer() |
|
|
const server = this._server = this.getServer() |
|
|
return new Promise ((resolve, reject) => { |
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
server.listen(options.port, () => { |
|
|
server.listen(options.port, () => { |
|
|
onServerUp(options.port, options.directory, server.isHttps) |
|
|
|
|
|
|
|
|
onServerUp(options.port, options['static.root'], server.isHttps) |
|
|
resolve(server) |
|
|
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. |
|
|
* Loads a module by either path or name. |
|
|
* @returns {object} |
|
|
* @returns {object} |
|
|
*/ |
|
|
*/ |
|
@ -233,7 +217,6 @@ function loadStack (modulePath) { |
|
|
let module |
|
|
let module |
|
|
const tried = [] |
|
|
const tried = [] |
|
|
if (modulePath) { |
|
|
if (modulePath) { |
|
|
const fs = require('fs') |
|
|
|
|
|
try { |
|
|
try { |
|
|
tried.push(path.resolve(modulePath)) |
|
|
tried.push(path.resolve(modulePath)) |
|
|
module = require(path.resolve(modulePath)) |
|
|
module = require(path.resolve(modulePath)) |
|
|