Browse Source

refactor

master
Lloyd Brookes 9 years ago
parent
commit
84443a7ac5
  1. 80
      lib/local-web-server.js

80
lib/local-web-server.js

@ -5,6 +5,7 @@ const path = require('path')
const arrayify = require('array-back') const arrayify = require('array-back')
const t = require('typical') const t = require('typical')
const CommandLineTool = require('command-line-tool') const CommandLineTool = require('command-line-tool')
const flatten = require('reduce-flatten')
/** /**
* @module local-web-server * @module local-web-server
@ -22,18 +23,37 @@ class LocalWebServer {
const commandLineUsage = require('command-line-usage') const commandLineUsage = require('command-line-usage')
const cli = require('../lib/cli-data') const cli = require('../lib/cli-data')
let stackPath
/* manually scan for any --stack passed, as we may need to display stack options */
const stackPaths = []
const stackIndex = process.argv.indexOf('--stack') const stackIndex = process.argv.indexOf('--stack')
if (stackIndex > -1) { if (stackIndex > -1) {
stackPath = process.argv[stackIndex + 1]
if (/^-/.test(stackPath)) stackPath = null
for (var i = stackIndex + 1; i < process.argv.length; i++) {
const stackPath = process.argv[i]
if (/^-/.test(stackPath)) {
break
} else {
stackPaths.push(stackPath)
}
}
} }
const stackModule = loadStack(stackPath) || require('local-web-server-default-stack')
this.stack = new stackModule()
this.stack.addAll()
const middlewareOptionDefinitions = this.stack.getOptionDefinitions()
// console.log(middlewareOptionDefinitions)
/* load the stack */
// if (!stackPaths.length) stackPaths.push('local-web-server-default-stack')
// console.log(stackPaths)
const stackModules = stackPaths
.map(stackPath => loadStack(stackPath))
.map(Middleware => new Middleware())
/* gather stack option definitions and parse the command line */
const middlewareOptionDefinitions = stackModules
.filter(mw => mw.optionDefinitions)
.map(mw => mw.optionDefinitions())
.reduce(flatten, [])
.map(def => {
def.group = 'middleware'
return def
})
const usage = commandLineUsage(cli.usage(middlewareOptionDefinitions)) const usage = commandLineUsage(cli.usage(middlewareOptionDefinitions))
let options = {} let options = {}
@ -44,31 +64,48 @@ class LocalWebServer {
tool.halt(err) tool.halt(err)
} }
/* combine in stored config */
const loadConfig = require('config-master') const loadConfig = require('config-master')
const stored = loadConfig('local-web-server') const stored = loadConfig('local-web-server')
/* override stored config with command line options */
options = Object.assign(stored, options.server, options.middleware, options.misc) options = Object.assign(stored, options.server, options.middleware, options.misc)
this.options = options this.options = options
// console.log(options)
if (options.verbose) { if (options.verbose) {
// debug.setLevel(1) // debug.setLevel(1)
} }
/* --config */
if (options.config) { if (options.config) {
tool.stop(JSON.stringify(options, null, ' '), 0) tool.stop(JSON.stringify(options, null, ' '), 0)
/* --version */
} else if (options.version) { } else if (options.version) {
const pkg = require(path.resolve(__dirname, '..', 'package.json')) const pkg = require(path.resolve(__dirname, '..', 'package.json'))
tool.stop(pkg.version) tool.stop(pkg.version)
} else {
if (this.options.help) {
/* --help */
} else if (options.help) {
tool.stop(usage) 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))
.filter(middleware => middleware)
.reduce(flatten, [])
.map(convert)
this.stack = compose(middlewareStack)
} }
} }
getApplication (options) { getApplication (options) {
const Koa = require('koa') const Koa = require('koa')
const app = new Koa() const app = new Koa()
app.use(this.stack.compose(this.options))
app.use(this.stack)
return app return app
} }
@ -163,22 +200,31 @@ function collectUserOptions (mwOptionDefinitions) {
return options return options
} }
/**
* Loads a module by either path or name.
* @returns {object}
*/
function loadStack (modulePath) { function loadStack (modulePath) {
let module let module
if (modulePath) { if (modulePath) {
const fs = require('fs') const fs = require('fs')
try { try {
module = require(path.resolve(modulePath)) module = require(path.resolve(modulePath))
if (!module.prototype.addAll) {
tool.halt(new Error('Must supply a MiddlewareStack'))
}
} catch (err) { } catch (err) {
const walkBack = require('walk-back') const walkBack = require('walk-back')
const foundPath = walkBack(path.resolve(process.cwd(), 'node_modules'), modulePath)
const foundPath = walkBack(process.cwd(), path.join('node_modules', 'local-web-server-' + modulePath))
if (foundPath) { if (foundPath) {
module = require(foundPath) module = require(foundPath)
} else {
const foundPath2 = walkBack(process.cwd(), path.join('node_modules', modulePath))
if (foundPath2) {
module = require(foundPath2)
}
}
} }
} }
if (!module.prototype.middleware) {
tool.halt(new Error('Must supply a Middleware'))
} }
return module return module
} }

Loading…
Cancel
Save