added --stack option.. refactor
This commit is contained in:
@ -5,8 +5,7 @@ const path = require('path')
|
||||
const arrayify = require('array-back')
|
||||
const t = require('typical')
|
||||
const CommandLineTool = require('command-line-tool')
|
||||
const DefaultStack = require('./default-stack')
|
||||
const debug = require('./debug')
|
||||
const DefaultStack = require('local-web-server-default-stack')
|
||||
|
||||
/**
|
||||
* @module local-web-server
|
||||
@ -20,17 +19,54 @@ const tool = new CommandLineTool()
|
||||
*/
|
||||
class LocalWebServer {
|
||||
constructor (stack) {
|
||||
this.stack = stack || new DefaultStack()
|
||||
const commandLineArgs = require('command-line-args')
|
||||
const commandLineUsage = require('command-line-usage')
|
||||
const cli = require('../lib/cli-data')
|
||||
|
||||
let stackPath
|
||||
const stackIndex = process.argv.indexOf('--stack')
|
||||
if (stackIndex > -1) {
|
||||
stackPath = process.argv[stackIndex + 1]
|
||||
if (/^-/.test(stackPath)) stackPath = null
|
||||
}
|
||||
|
||||
const stackModule = loadStack(stackPath) || DefaultStack
|
||||
this.stack = new stackModule()
|
||||
this.stack.addAll()
|
||||
const middlewareOptionDefinitions = this.stack.getOptionDefinitions()
|
||||
// console.log(middlewareOptionDefinitions)
|
||||
const usage = commandLineUsage(cli.usage(middlewareOptionDefinitions))
|
||||
|
||||
let options = {}
|
||||
try {
|
||||
options = commandLineArgs(cli.optionDefinitions.concat(middlewareOptionDefinitions))
|
||||
} catch (err) {
|
||||
console.error(usage)
|
||||
tool.halt(err)
|
||||
}
|
||||
|
||||
const loadConfig = require('config-master')
|
||||
const stored = loadConfig('local-web-server')
|
||||
/* override stored config with command line options */
|
||||
options = Object.assign(stored, options.server, options.middleware, options.misc)
|
||||
this.options = options
|
||||
|
||||
if (options.verbose) {
|
||||
// debug.setLevel(1)
|
||||
}
|
||||
if (options.config) {
|
||||
tool.stop(JSON.stringify(options, null, ' '), 0)
|
||||
} else if (options.version) {
|
||||
const pkg = require(path.resolve(__dirname, '..', 'package.json'))
|
||||
tool.stop(pkg.version)
|
||||
} else {
|
||||
if (this.options.help) {
|
||||
tool.stop(usage)
|
||||
}
|
||||
}
|
||||
}
|
||||
_init (options) {
|
||||
this.options = this.options || Object.assign(options || {}, collectUserOptions(this.stack.getOptionDefinitions()))
|
||||
}
|
||||
addStack (stack) {
|
||||
this.stack = stack
|
||||
}
|
||||
|
||||
getApplication (options) {
|
||||
this._init(options)
|
||||
const Koa = require('koa')
|
||||
const app = new Koa()
|
||||
app.use(this.stack.compose(this.options))
|
||||
@ -73,27 +109,14 @@ class LocalWebServer {
|
||||
}
|
||||
|
||||
listen (options, callback) {
|
||||
this._init(options)
|
||||
options = this.options
|
||||
|
||||
if (options.verbose) {
|
||||
debug.setLevel(1)
|
||||
}
|
||||
|
||||
if (options.config) {
|
||||
tool.stop(JSON.stringify(options, null, ' '), 0)
|
||||
} else if (options.version) {
|
||||
const pkg = require(path.resolve(__dirname, '..', 'package.json'))
|
||||
tool.stop(pkg.version)
|
||||
} else {
|
||||
const server = this.getServer()
|
||||
const port = options.port || 8000
|
||||
server.listen(port, () => {
|
||||
onServerUp(port, options.directory, server.isHttps)
|
||||
if (callback) callback()
|
||||
})
|
||||
return server
|
||||
}
|
||||
const server = this.getServer()
|
||||
const port = options.port || 8000
|
||||
server.listen(port, () => {
|
||||
onServerUp(port, options.directory, server.isHttps)
|
||||
if (callback) callback()
|
||||
})
|
||||
return server
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,14 +154,34 @@ function collectUserOptions (mwOptionDefinitions) {
|
||||
const cli = require('../lib/cli-data')
|
||||
|
||||
/* parse command line args */
|
||||
const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions))
|
||||
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)
|
||||
|
||||
// console.error(require('util').inspect(options, { depth: 3, colors: true }))
|
||||
return options
|
||||
}
|
||||
|
||||
function loadStack (modulePath) {
|
||||
let module
|
||||
if (modulePath) {
|
||||
const fs = require('fs')
|
||||
try {
|
||||
module = require(path.resolve(modulePath))
|
||||
if (!module.prototype.addAll) {
|
||||
tool.halt(new Error('Must supply a MiddlewareStack'))
|
||||
}
|
||||
} catch (err) {
|
||||
const walkBack = require('walk-back')
|
||||
const foundPath = walkBack(path.resolve(process.cwd(), 'node_modules'), modulePath)
|
||||
if (foundPath) {
|
||||
module = require(foundPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
return module
|
||||
}
|
||||
|
||||
module.exports = LocalWebServer
|
||||
|
Reference in New Issue
Block a user