refactor
This commit is contained in:
@ -5,56 +5,53 @@ const path = require('path')
|
||||
const arrayify = require('array-back')
|
||||
const t = require('typical')
|
||||
const Tool = require('command-line-tool')
|
||||
const MiddlewareStack = require('./middleware-stack')
|
||||
|
||||
const tool = new Tool()
|
||||
|
||||
class Cli {
|
||||
constructor () {
|
||||
this.options = collectOptions()
|
||||
this.app = null
|
||||
this.middleware = null
|
||||
class Cli extends MiddlewareStack {
|
||||
start () {
|
||||
const options = collectOptions(this.getOptionDefinitions())
|
||||
this.options = options
|
||||
|
||||
const options = this.options
|
||||
if (options.misc.verbose) {
|
||||
process.env.DEBUG = '*'
|
||||
}
|
||||
|
||||
if (options.misc.config) {
|
||||
tool.stop(JSON.stringify(options.server, null, ' '), 0)
|
||||
tool.stop(JSON.stringify(options, null, ' '), 0)
|
||||
} else {
|
||||
const Koa = require('koa')
|
||||
const app = new Koa()
|
||||
app.on('error', err => {
|
||||
if (options.server['log-format']) {
|
||||
if (options.middleware['log-format']) {
|
||||
console.error(ansi.format(err.message, 'red'))
|
||||
}
|
||||
})
|
||||
this.app = app
|
||||
|
||||
const MiddlewareStack = require('./middleware-stack')
|
||||
this.middleware = new MiddlewareStack(options)
|
||||
}
|
||||
}
|
||||
app.use(this.compose(options))
|
||||
|
||||
listen () {
|
||||
this.app.use(this.middleware.compose())
|
||||
const options = this.options
|
||||
const key = this.options.server.key
|
||||
const cert = this.options.server.cert
|
||||
if (options.server.https) {
|
||||
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
|
||||
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
|
||||
}
|
||||
|
||||
if (key && cert) {
|
||||
const https = require('https')
|
||||
const fs = require('fs')
|
||||
|
||||
const serverOptions = {
|
||||
key: fs.readFileSync(key),
|
||||
cert: fs.readFileSync(cert)
|
||||
let key = options.server.key
|
||||
let cert = options.server.cert
|
||||
if (options.server.https) {
|
||||
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
|
||||
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
|
||||
}
|
||||
|
||||
const server = https.createServer(serverOptions, this.app.callback())
|
||||
server.listen(options.server.port, onServerUp.bind(null, options, true))
|
||||
} else {
|
||||
this.app.listen(options.server.port, onServerUp.bind(null, options))
|
||||
if (key && cert) {
|
||||
const https = require('https')
|
||||
const fs = require('fs')
|
||||
|
||||
const serverOptions = {
|
||||
key: fs.readFileSync(key),
|
||||
cert: fs.readFileSync(cert)
|
||||
}
|
||||
|
||||
const server = https.createServer(serverOptions, app.callback())
|
||||
server.listen(options.server.port, onServerUp.bind(null, options, true))
|
||||
} else {
|
||||
app.listen(options.server.port, onServerUp.bind(null, options))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -65,9 +62,9 @@ function onServerUp (options, isHttps) {
|
||||
.join(', ')
|
||||
|
||||
console.error(ansi.format(
|
||||
path.resolve(options.server.directory) === process.cwd()
|
||||
path.resolve(options.middleware.directory) === process.cwd()
|
||||
? `serving at ${ipList}`
|
||||
: `serving [underline]{${options.server.directory}} at ${ipList}`
|
||||
: `serving [underline]{${options.middleware.directory}} at ${ipList}`
|
||||
))
|
||||
}
|
||||
|
||||
@ -86,36 +83,38 @@ function getIPList (isHttps) {
|
||||
/**
|
||||
* Return default, stored and command-line options combined
|
||||
*/
|
||||
function collectOptions () {
|
||||
function collectOptions (mwOptionDefinitions) {
|
||||
const loadConfig = require('config-master')
|
||||
const stored = loadConfig('local-web-server')
|
||||
const cli = require('../lib/cli-data')
|
||||
|
||||
/* parse command line args */
|
||||
let options = tool.getOptions(cli.optionDefinitions, cli.usage)
|
||||
|
||||
const builtIn = {
|
||||
port: 8000,
|
||||
directory: process.cwd()
|
||||
}
|
||||
|
||||
if (options.server.rewrite) {
|
||||
options.server.rewrite = parseRewriteRules(options.server.rewrite)
|
||||
}
|
||||
const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions))
|
||||
let options = tool.getOptions(definitions, cli.usage(definitions))
|
||||
|
||||
/* override built-in defaults with stored config and then command line args */
|
||||
options.server = Object.assign(builtIn, stored, options.server)
|
||||
options.server = Object.assign({ port: 8000 }, stored.server, options.server)
|
||||
options.middleware = Object.assign({ directory: process.cwd() }, stored.middleware || {}, options.middleware)
|
||||
|
||||
if (options.middleware.rewrite) {
|
||||
options.middleware.rewrite = parseRewriteRules(options.middleware.rewrite)
|
||||
}
|
||||
|
||||
validateOptions(options)
|
||||
// console.log(options)
|
||||
return options
|
||||
}
|
||||
|
||||
function parseRewriteRules (rules) {
|
||||
return rules && rules.map(rule => {
|
||||
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
|
||||
return {
|
||||
from: matches[1],
|
||||
to: matches[2]
|
||||
if (t.isString(rule)) {
|
||||
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
|
||||
return {
|
||||
from: matches[1],
|
||||
to: matches[2]
|
||||
}
|
||||
} else {
|
||||
return rule
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user