Browse Source

refactor, linting

master
Lloyd Brookes 9 years ago
parent
commit
7910a424c2
  1. 1
      bin/cli.js
  2. 55
      lib/local-web-server.js
  3. 1
      package.json

1
bin/cli.js

@ -4,3 +4,4 @@ const LocalWebServer = require('../')
const ws = new LocalWebServer()
ws.listen()
.catch(err => console.error(err.stack))

55
lib/local-web-server.js

@ -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))

1
package.json

@ -46,7 +46,6 @@
"jsdoc-to-markdown": "^1.3.6",
"koa-cache-control": "^1.0.0",
"koa-livereload": "~0.2.0",
"node-fetch": "^1.5.3",
"req-then": "~0.2.4",
"tape": "^4.6.0"
}

Loading…
Cancel
Save