refactor
This commit is contained in:
28
bin/cli.js
28
bin/cli.js
@ -3,18 +3,18 @@
|
|||||||
const Cli = require('../')
|
const Cli = require('../')
|
||||||
|
|
||||||
const ws = new Cli()
|
const ws = new Cli()
|
||||||
ws.middleware.addCors()
|
ws.middleware
|
||||||
ws.middleware.addJson()
|
.addCors()
|
||||||
ws.middleware.addRewrite()
|
.addJson()
|
||||||
ws.middleware.addBodyParser()
|
.addRewrite()
|
||||||
ws.middleware.addBlacklist()
|
.addBodyParser()
|
||||||
ws.middleware.addCache()
|
.addBlacklist()
|
||||||
ws.middleware.addMimeType()
|
.addCache()
|
||||||
ws.middleware.addCompression()
|
.addMimeType()
|
||||||
ws.middleware.addLogging()
|
.addCompression()
|
||||||
ws.middleware.addMockResponses()
|
.addLogging()
|
||||||
ws.middleware.addSpa()
|
.addMockResponses()
|
||||||
|
.addSpa()
|
||||||
ws.middleware.addStatic()
|
.addStatic()
|
||||||
ws.middleware.addIndex()
|
.addIndex()
|
||||||
ws.listen()
|
ws.listen()
|
||||||
|
@ -1,24 +1,16 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const Cli = require('../cli')
|
const Cli = require('../')
|
||||||
const cacheControl = require('koa-cache-control')
|
const cacheControl = require('koa-cache-control')
|
||||||
const cliData = require('../lib/cli-data')
|
const cliData = require('../lib/cli-data')
|
||||||
|
|
||||||
cliData.push({ name: 'black' })
|
cliData.optionDefinitions.push({ name: 'maxage', group: 'misc' })
|
||||||
|
|
||||||
const ws = new Cli({
|
|
||||||
'no-cache': true,
|
|
||||||
log: { format: 'dev' }
|
|
||||||
})
|
|
||||||
|
|
||||||
ws.middleware.splice(
|
|
||||||
ws.middleware.findIndex(m => m.name === 'mime-type'),
|
|
||||||
1,
|
|
||||||
{
|
|
||||||
name: 'cache-control',
|
|
||||||
create: convert(cacheControl({
|
|
||||||
maxAge: 15
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
const ws = new Cli()
|
||||||
|
ws.middleware
|
||||||
|
.addLogging('dev')
|
||||||
|
.add(cacheControl({
|
||||||
|
maxAge: 15
|
||||||
|
}))
|
||||||
|
.addStatic()
|
||||||
|
.addIndex()
|
||||||
ws.listen()
|
ws.listen()
|
||||||
|
@ -1,14 +1,10 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const Koa = require('koa')
|
const Cli = require('../')
|
||||||
const localWebServer = require('../')
|
|
||||||
const liveReload = require('koa-livereload')
|
const liveReload = require('koa-livereload')
|
||||||
const convert = require('koa-convert')
|
|
||||||
|
|
||||||
const app = new Koa()
|
const ws = new Cli()
|
||||||
const ws = localWebServer({
|
ws.middleware
|
||||||
log: { format: 'dev' }
|
.addLogging('dev')
|
||||||
})
|
.add(liveReload())
|
||||||
|
.addStatic()
|
||||||
app.use(liveReload())
|
ws.listen(8000)
|
||||||
app.use(ws)
|
|
||||||
app.listen(8000)
|
|
||||||
|
@ -8,12 +8,12 @@ const Tool = require('command-line-tool')
|
|||||||
const tool = new Tool()
|
const tool = new Tool()
|
||||||
|
|
||||||
class Cli {
|
class Cli {
|
||||||
constructor () {
|
constructor (options) {
|
||||||
this.options = null
|
this.options = null
|
||||||
this.app = null
|
this.app = null
|
||||||
this.middleware = null
|
this.middleware = null
|
||||||
|
|
||||||
let options = collectOptions()
|
options = collectOptions()
|
||||||
this.options = options
|
this.options = options
|
||||||
|
|
||||||
if (options.misc.config) {
|
if (options.misc.config) {
|
||||||
@ -24,23 +24,8 @@ class Cli {
|
|||||||
this.app = app
|
this.app = app
|
||||||
|
|
||||||
const MiddlewareStack = require('./middleware-stack')
|
const MiddlewareStack = require('./middleware-stack')
|
||||||
this.middleware = new MiddlewareStack({
|
this.middleware = new MiddlewareStack(options)
|
||||||
static: {
|
const a = {
|
||||||
root: options.server.directory,
|
|
||||||
options: {
|
|
||||||
hidden: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
serveIndex: {
|
|
||||||
path: options.server.directory,
|
|
||||||
options: {
|
|
||||||
icons: true,
|
|
||||||
hidden: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
log: {
|
|
||||||
format: options.server['log-format']
|
|
||||||
},
|
|
||||||
compress: options.server.compress,
|
compress: options.server.compress,
|
||||||
mime: options.server.mime,
|
mime: options.server.mime,
|
||||||
forbid: options.server.forbid,
|
forbid: options.server.forbid,
|
||||||
@ -49,7 +34,7 @@ class Cli {
|
|||||||
rewrite: options.server.rewrite,
|
rewrite: options.server.rewrite,
|
||||||
verbose: options.server.verbose,
|
verbose: options.server.verbose,
|
||||||
mocks: options.server.mocks
|
mocks: options.server.mocks
|
||||||
})
|
}
|
||||||
|
|
||||||
app.on('error', err => {
|
app.on('error', err => {
|
||||||
if (options.server['log-format']) {
|
if (options.server['log-format']) {
|
||||||
@ -121,9 +106,7 @@ function collectOptions () {
|
|||||||
|
|
||||||
const builtIn = {
|
const builtIn = {
|
||||||
port: 8000,
|
port: 8000,
|
||||||
directory: process.cwd(),
|
directory: process.cwd()
|
||||||
forbid: [],
|
|
||||||
rewrite: []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.server.rewrite) {
|
if (options.server.rewrite) {
|
||||||
|
@ -8,17 +8,11 @@ const mw = require('./middleware')
|
|||||||
class MiddlewareStack extends Array {
|
class MiddlewareStack extends Array {
|
||||||
constructor (options) {
|
constructor (options) {
|
||||||
super()
|
super()
|
||||||
|
this.options = options
|
||||||
|
|
||||||
options = Object.assign({
|
options = Object.assign({
|
||||||
static: {},
|
|
||||||
serveIndex: {
|
|
||||||
options: {
|
|
||||||
icons: true,
|
|
||||||
hidden: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
cacheControl: {},
|
cacheControl: {},
|
||||||
spa: null,
|
spa: null,
|
||||||
log: {},
|
|
||||||
compress: false,
|
compress: false,
|
||||||
mime: {},
|
mime: {},
|
||||||
forbid: [],
|
forbid: [],
|
||||||
@ -31,20 +25,14 @@ class MiddlewareStack extends Array {
|
|||||||
process.env.DEBUG = '*'
|
process.env.DEBUG = '*'
|
||||||
}
|
}
|
||||||
|
|
||||||
const log = options.log
|
|
||||||
log.options = log.options || {}
|
|
||||||
|
|
||||||
if (options.verbose && !log.format) {
|
|
||||||
log.format = 'none'
|
|
||||||
}
|
|
||||||
this.log = log
|
|
||||||
|
|
||||||
if (!options.static.root) options.static.root = process.cwd()
|
|
||||||
if (!options.serveIndex.path) options.serveIndex.path = process.cwd()
|
|
||||||
options.rewrite = arrayify(options.rewrite)
|
options.rewrite = arrayify(options.rewrite)
|
||||||
options.forbid = arrayify(options.forbid)
|
options.forbid = arrayify(options.forbid)
|
||||||
options.mocks = arrayify(options.mocks)
|
options.mocks = arrayify(options.mocks)
|
||||||
this.options = options
|
}
|
||||||
|
|
||||||
|
add (middleware) {
|
||||||
|
this.push(middleware)
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,14 +51,13 @@ class MiddlewareStack extends Array {
|
|||||||
|
|
||||||
/* rewrite rules */
|
/* rewrite rules */
|
||||||
addRewrite () {
|
addRewrite () {
|
||||||
const _ = require('koa-route')
|
|
||||||
|
|
||||||
const options = this.options.rewrite
|
const options = this.options.rewrite
|
||||||
if (options.length) {
|
if (options.length) {
|
||||||
options.forEach(route => {
|
options.forEach(route => {
|
||||||
if (route.to) {
|
if (route.to) {
|
||||||
/* `to` address is remote if the url specifies a host */
|
/* `to` address is remote if the url specifies a host */
|
||||||
if (url.parse(route.to).host) {
|
if (url.parse(route.to).host) {
|
||||||
|
const _ = require('koa-route')
|
||||||
debug('proxy rewrite', `${route.from} -> ${route.to}`)
|
debug('proxy rewrite', `${route.from} -> ${route.to}`)
|
||||||
this.push(_.all(route.from, mw.proxyRequest(route)))
|
this.push(_.all(route.from, mw.proxyRequest(route)))
|
||||||
} else {
|
} else {
|
||||||
@ -89,6 +76,7 @@ class MiddlewareStack extends Array {
|
|||||||
See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
||||||
addBodyParser () {
|
addBodyParser () {
|
||||||
this.push(require('koa-bodyparser')())
|
this.push(require('koa-bodyparser')())
|
||||||
|
return this
|
||||||
}
|
}
|
||||||