ignoreCli option.. fixed tests.. removed .listen() and close().. refactor
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict'
|
||||
const ansi = require('ansi-escape-sequences')
|
||||
const path = require('path')
|
||||
const CommandLineTool = require('command-line-tool')
|
||||
const flatten = require('reduce-flatten')
|
||||
@ -83,22 +82,34 @@ class LocalWebServer {
|
||||
|
||||
let options = {}
|
||||
const allOptionDefinitions = cli.optionDefinitions.concat(middlewareOptionDefinitions)
|
||||
try {
|
||||
options = commandLineArgs(allOptionDefinitions)
|
||||
} catch (err) {
|
||||
tool.printError(err)
|
||||
tool.printError(allOptionDefinitions.map(def => {
|
||||
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
|
||||
}).join('\n'))
|
||||
console.error(usage)
|
||||
tool.halt()
|
||||
if (!initOptions.ignoreCli) {
|
||||
try {
|
||||
options = commandLineArgs(allOptionDefinitions)
|
||||
} catch (err) {
|
||||
tool.printError(err)
|
||||
tool.printError(allOptionDefinitions.map(def => {
|
||||
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
|
||||
}).join('\n'))
|
||||
console.error(usage)
|
||||
tool.halt()
|
||||
}
|
||||
}
|
||||
|
||||
/* combine in stored config */
|
||||
options = Object.assign({ port: 8000 }, initOptions, stored || {}, options.server, options.middleware, options.misc)
|
||||
this.options = options
|
||||
options = Object.assign(
|
||||
{ port: 8000 },
|
||||
initOptions,
|
||||
stored,
|
||||
options.server,
|
||||
options.middleware,
|
||||
options.misc
|
||||
)
|
||||
|
||||
// console.log(initOptions, stored, options)
|
||||
/**
|
||||
* Config
|
||||
* @type {object}
|
||||
*/
|
||||
this.options = options
|
||||
|
||||
if (options.verbose) {
|
||||
// debug.setLevel(1)
|
||||
@ -146,9 +157,10 @@ class LocalWebServer {
|
||||
return app
|
||||
}
|
||||
|
||||
getServer () {
|
||||
getServer (onListening) {
|
||||
const app = this.getApplication()
|
||||
const options = this.options
|
||||
|
||||
let key = options.key
|
||||
let cert = options.cert
|
||||
|
||||
@ -172,48 +184,10 @@ class LocalWebServer {
|
||||
const http = require('http')
|
||||
server = http.createServer(app.callback())
|
||||
}
|
||||
|
||||
server.listen(options.port, onListening)
|
||||
return server
|
||||
}
|
||||
|
||||
listen () {
|
||||
const options = this.options
|
||||
const server = this._server = this.getServer()
|
||||
// console.log(options)
|
||||
return new Promise((resolve, reject) => {
|
||||
server.listen(options.port, () => {
|
||||
onServerUp(options.port, options['static.root'], server.isHttps)
|
||||
resolve(server)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
close () {
|
||||
this._server.close()
|
||||
}
|
||||
}
|
||||
|
||||
function onServerUp (port, directory, isHttps) {
|
||||
const ipList = getIPList()
|
||||
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${port}}`)
|
||||
.join(', ')
|
||||
|
||||
console.error(ansi.format(
|
||||
path.resolve(directory || '') === process.cwd()
|
||||
? `serving at ${ipList}`
|
||||
: `serving [underline]{${directory}} at ${ipList}`
|
||||
))
|
||||
}
|
||||
|
||||
function getIPList () {
|
||||
const flatten = require('reduce-flatten')
|
||||
const os = require('os')
|
||||
|
||||
let ipList = Object.keys(os.networkInterfaces())
|
||||
.map(key => os.networkInterfaces()[key])
|
||||
.reduce(flatten, [])
|
||||
.filter(iface => iface.family === 'IPv4')
|
||||
ipList.unshift({ address: os.hostname() })
|
||||
return ipList
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user