log.format, error reporting

This commit is contained in:
Lloyd Brookes
2016-07-01 20:53:10 +01:00
parent 3d521399f6
commit 3e54a492f7
6 changed files with 32 additions and 17 deletions

View File

@ -77,11 +77,16 @@ class LocalWebServer {
const usage = commandLineUsage(cli.usage(middlewareOptionDefinitions))
let options = {}
const allOptionDefinitions = cli.optionDefinitions.concat(middlewareOptionDefinitions)
try {
options = commandLineArgs(cli.optionDefinitions.concat(middlewareOptionDefinitions))
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(err)
tool.halt()
}
/* combine in stored config */
@ -125,7 +130,7 @@ class LocalWebServer {
const app = new Koa()
app.use(this.stack)
app.on('error', err => {
if (this.options['log-format']) {
if (this.options['log.format']) {
console.error(ansi.format(err.stack, 'red'))
}
})
@ -189,7 +194,6 @@ function onServerUp (port, directory, isHttps) {
))
}
function getIPList () {
const flatten = require('reduce-flatten')
const os = require('os')
@ -227,25 +231,36 @@ function collectUserOptions (mwOptionDefinitions) {
*/
function loadStack (modulePath) {
let module
const tried = []
if (modulePath) {
const fs = require('fs')
try {
tried.push(path.resolve(modulePath))
module = require(path.resolve(modulePath))
} catch (err) {
const walkBack = require('walk-back')
const foundPath = walkBack(process.cwd(), path.join('node_modules', 'local-web-server-' + modulePath))
tried.push('local-web-server-' + modulePath)
if (foundPath) {
module = require(foundPath)
} else {
const foundPath2 = walkBack(process.cwd(), path.join('node_modules', modulePath))
tried.push(modulePath)
if (foundPath2) {
module = require(foundPath2)
}
}
}
}
if (!(module && (module.prototype.middleware || module.prototype.stack))) {
tool.halt(new Error('Not valid Middleware: ' + modulePath))
if (module) {
if (!(module.prototype.middleware || module.prototype.stack)) {
const insp = require('util').inspect(module, { depth: 3, colors: true })
const msg = `Not valid Middleware at: ${insp}`
tool.halt(new Error(msg))
}
} else {
const msg = `No module found at: \n${tried.join('\n')}`
tool.halt(new Error(msg))
}
return module
}