refactor[mock-server]: refactor mock-server #1860

This commit is contained in:
Pan 2019-04-09 14:25:14 +08:00
parent b627d3d0ba
commit dbee6ff707
1 changed files with 12 additions and 29 deletions

View File

@ -3,12 +3,16 @@ const bodyParser = require('body-parser')
const chalk = require('chalk')
function registerRoutes(app) {
let mockStartIndex
const { default: mocks } = require('./index.js')
for (const mock of mocks) {
app[mock.type](mock.url, mock.response)
mockStartIndex = app._router.stack.length
}
const mockRoutesLength = Object.keys(mocks).length
return {
mockRoutesLength: Object.keys(mocks).length
mockRoutesLength: mockRoutesLength,
mockStartIndex: mockStartIndex - mockRoutesLength
}
}
@ -20,28 +24,6 @@ function unregisterRoutes() {
})
}
function getPath(path) {
var match = path.toString()
.replace('\\/?', '')
.replace('(?=\\/|$)', '$')
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//)
return match
? match[1].replace(/\\(.)/g, '$1').split('/')
: path.toString()
}
function getMockRoutesIndex(app) {
for (let index = 0; index <= app._router.stack.length; index++) {
const r = app._router.stack[index]
if (r.route && r.route.path) {
const path = getPath(r.route.path)
if (path.includes('mock')) {
return index
}
}
}
}
module.exports = app => {
// es6 polyfill
require('@babel/register')
@ -53,7 +35,9 @@ module.exports = app => {
extended: true
}))
const { mockRoutesLength } = registerRoutes(app)
const mockRoutes = registerRoutes(app)
var mockRoutesLength = mockRoutes.mockRoutesLength
var mockStartIndex = mockRoutes.mockStartIndex
// watch files, hot reload mock server
chokidar.watch(('./mock'), {
@ -62,16 +46,15 @@ module.exports = app => {
ignoreInitial: true
}).on('all', (event, path) => {
if (event === 'change' || event === 'add') {
// find mock routes stack index
const index = getMockRoutesIndex(app)
// remove mock routes stack
app._router.stack.splice(index, mockRoutesLength)
app._router.stack.splice(mockStartIndex, mockRoutesLength)
// clear routes cache
unregisterRoutes()
registerRoutes(app)
const mockRoutes = registerRoutes(app)
mockRoutesLength = mockRoutes.mockRoutesLength
mockStartIndex = mockRoutes.mockStartIndex
console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`))
}