server now created on contruction, before views intantiated.. refactor view API
This commit is contained in:
@ -22,17 +22,10 @@ class LocalWebServer {
|
||||
*/
|
||||
constructor (initOptions) {
|
||||
initOptions = initOptions || {}
|
||||
const commandLineArgs = require('command-line-args')
|
||||
const commandLineUsage = require('command-line-usage')
|
||||
const CliView = require('./cli-view')
|
||||
const cli = require('../lib/cli-data')
|
||||
|
||||
/**
|
||||
* Current view.
|
||||
* @type {View}
|
||||
*/
|
||||
this.view = new CliView(this)
|
||||
|
||||
/* get stored config */
|
||||
const loadConfig = require('config-master')
|
||||
const stored = loadConfig('local-web-server')
|
||||
@ -47,35 +40,10 @@ class LocalWebServer {
|
||||
this.features = this._buildFeatureStack(featurePaths)
|
||||
|
||||
/* gather feature optionDefinitions and parse the command line */
|
||||
const featureOptionDefinitions = this.features
|
||||
.filter(mw => mw.optionDefinitions)
|
||||
.map(mw => mw.optionDefinitions())
|
||||
.reduce(flatten, [])
|
||||
.filter(def => def)
|
||||
.map(def => {
|
||||
def.group = 'middleware'
|
||||
return def
|
||||
})
|
||||
|
||||
const featureOptionDefinitions = gatherOptionDefinitions(this.features)
|
||||
const usage = commandLineUsage(cli.usage(featureOptionDefinitions))
|
||||
|
||||
let options = {}
|
||||
const allOptionDefinitions = cli.optionDefinitions.concat(featureOptionDefinitions)
|
||||
if (!initOptions.testMode) {
|
||||
try {
|
||||
options = commandLineArgs(allOptionDefinitions)
|
||||
} catch (err) {
|
||||
this.view.error(err.toString())
|
||||
if (err.name === 'DUPLICATE_NAME') {
|
||||
this.view.error('\nOption Definitions:')
|
||||
this.view.error(allOptionDefinitions.map(def => {
|
||||
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
|
||||
}).join('\n'))
|
||||
}
|
||||
this.view.info(usage)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
let options = initOptions.testMode ? {} : parseCommandLineOptions(allOptionDefinitions, this.view)
|
||||
|
||||
/* combine in stored config */
|
||||
options = Object.assign(
|
||||
@ -93,26 +61,41 @@ class LocalWebServer {
|
||||
*/
|
||||
this.options = options
|
||||
|
||||
if (options.view) {
|
||||
const View = loadModule(options.view)
|
||||
this.view = new View(this)
|
||||
}
|
||||
/**
|
||||
* Current view.
|
||||
* @type {View}
|
||||
*/
|
||||
this.view = null
|
||||
|
||||
/* --config */
|
||||
if (options.config) {
|
||||
this.view.info(JSON.stringify(options, null, ' '))
|
||||
console.error(JSON.stringify(options, null, ' '))
|
||||
process.exit(0)
|
||||
|
||||
/* --version */
|
||||
} else if (options.version) {
|
||||
const pkg = require(path.resolve(__dirname, '..', 'package.json'))
|
||||
this.view.info(pkg.version)
|
||||
console.error(pkg.version)
|
||||
process.exit(0)
|
||||
|
||||
/* --help */
|
||||
} else if (options.help) {
|
||||
this.view.info(usage)
|
||||
console.error(usage)
|
||||
process.exit(0)
|
||||
|
||||
} else {
|
||||
/**
|
||||
* Node.js server
|
||||
* @type {Server}
|
||||
*/
|
||||
this.server = this.getServer()
|
||||
|
||||
if (options.view) {
|
||||
const View = loadModule(options.view)
|
||||
this.view = new View(this)
|
||||
} else {
|
||||
this.view = new CliView(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,7 +127,7 @@ class LocalWebServer {
|
||||
* Returns a listening server which processes requests using the middleware supplied.
|
||||
* @returns {Server}
|
||||
*/
|
||||
getServer (onListening) {
|
||||
getServer () {
|
||||
const app = this.getApplication()
|
||||
const options = this.options
|
||||
|
||||
@ -173,21 +156,18 @@ class LocalWebServer {
|
||||
}
|
||||
|
||||
server.listen(options.port)
|
||||
if (onListening) server.on('listening', onListening)
|
||||
// if (onListening) server.on('listening', onListening)
|
||||
|
||||
/* on server-up message */
|
||||
if (!options.testMode) {
|
||||
server.on('listening', () => {
|
||||
const ipList = getIPList()
|
||||
.map(iface => `[underline]{${server.isHttps ? 'https' : 'http'}://${iface.address}:${options.port}}`)
|
||||
.join(', ')
|
||||
this.view.info('Serving at', ansi.format(ipList))
|
||||
console.error('Serving at', ansi.format(ipList))
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Node.js server
|
||||
* @type {Server}
|
||||
*/
|
||||
this.server = server
|
||||
return server
|
||||
}
|
||||
|
||||
@ -234,7 +214,7 @@ function loadStack (modulePath) {
|
||||
process.exit(1)
|
||||
}
|
||||
} else {
|
||||
const msg = `No module found at: \n${tried.join('\n')}`
|
||||
const msg = `No module found for: ${modulePath}`
|
||||
console.error(msg)
|
||||
process.exit(1)
|
||||
}
|
||||
@ -299,4 +279,35 @@ function parseFeaturePaths (configStack) {
|
||||
return featurePaths
|
||||
}
|
||||
|
||||
function gatherOptionDefinitions (features) {
|
||||
return features
|
||||
.filter(mw => mw.optionDefinitions)
|
||||
.map(mw => mw.optionDefinitions())
|
||||
.reduce(flatten, [])
|
||||
.filter(def => def)
|
||||
.map(def => {
|
||||
def.group = 'middleware'
|
||||
return def
|
||||
})
|
||||
}
|
||||
|
||||
function parseCommandLineOptions (allOptionDefinitions) {
|
||||
const commandLineArgs = require('command-line-args')
|
||||
try {
|
||||
return commandLineArgs(allOptionDefinitions)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
|
||||
/* handle duplicate option names */
|
||||
if (err.name === 'DUPLICATE_NAME') {
|
||||
console.error('\nOption Definitions:')
|
||||
console.error(allOptionDefinitions.map(def => {
|
||||
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
|
||||
}).join('\n'))
|
||||
}
|
||||
console.error(usage)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LocalWebServer
|
||||
|
Reference in New Issue
Block a user