can now construct with both stack paths or modules
This commit is contained in:
@ -4,6 +4,7 @@ const ansi = require('ansi-escape-sequences')
|
||||
const path = require('path')
|
||||
const CommandLineTool = require('command-line-tool')
|
||||
const flatten = require('reduce-flatten')
|
||||
const arrayify = require('array-back')
|
||||
|
||||
/**
|
||||
* @module local-web-server
|
||||
@ -26,7 +27,7 @@ class LocalWebServer {
|
||||
const stored = loadConfig('local-web-server')
|
||||
|
||||
/* manually scan for any --stack passed, as we may need to display stack options */
|
||||
const stackPaths = initOptions.stack || stored.stack || []
|
||||
const stackPaths = arrayify(initOptions.stack || stored.stack) || []
|
||||
const stackIndex = process.argv.indexOf('--stack')
|
||||
if (stackIndex > -1) {
|
||||
for (var i = stackIndex + 1; i < process.argv.length; i++) {
|
||||
@ -39,8 +40,10 @@ class LocalWebServer {
|
||||
}
|
||||
}
|
||||
|
||||
/* load the stack */
|
||||
/* if the user did not supply a stack, use the default */
|
||||
if (!stackPaths.length) stackPaths.push(path.resolve(__dirname, '..', 'node_modules', 'local-web-server-default-stack'))
|
||||
|
||||
/* load the stack */
|
||||
const stackModules = stackPaths
|
||||
.map(stackPath => loadStack(stackPath))
|
||||
.map(Middleware => new Middleware())
|
||||
@ -216,6 +219,7 @@ function getIPList () {
|
||||
*/
|
||||
function loadStack (modulePath) {
|
||||
let module
|
||||
if (isModule(modulePath)) return modulePath
|
||||
const tried = []
|
||||
if (modulePath) {
|
||||
try {
|
||||
@ -237,7 +241,7 @@ function loadStack (modulePath) {
|
||||
}
|
||||
}
|
||||
if (module) {
|
||||
if (!(module.prototype.middleware || module.prototype.stack)) {
|
||||
if (!isModule(module)) {
|
||||
const insp = require('util').inspect(module, { depth: 3, colors: true })
|
||||
const msg = `Not valid Middleware at: ${insp}`
|
||||
tool.halt(new Error(msg))
|
||||
@ -249,4 +253,8 @@ function loadStack (modulePath) {
|
||||
return module
|
||||
}
|
||||
|
||||
function isModule (module) {
|
||||
return module.prototype.middleware || module.prototype.stack
|
||||
}
|
||||
|
||||
module.exports = LocalWebServer
|
||||
|
Reference in New Issue
Block a user