refine
This commit is contained in:
parent
5e8bc40509
commit
519847e865
|
@ -1,16 +1,6 @@
|
||||||
|
|
||||||
const chokidar = require('chokidar')
|
const chokidar = require('chokidar')
|
||||||
const bodyParser = require('body-parser')
|
const bodyParser = require('body-parser')
|
||||||
|
const chalk = require('chalk')
|
||||||
function getPath(path) {
|
|
||||||
var match = path.toString()
|
|
||||||
.replace('\\/?', '')
|
|
||||||
.replace('(?=\\/|$)', '$')
|
|
||||||
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//)
|
|
||||||
return match
|
|
||||||
? match[1].replace(/\\(.)/g, '$1').split('/')
|
|
||||||
: path.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
function registerRoutes(app) {
|
function registerRoutes(app) {
|
||||||
const { default: mocks } = require('./index.js')
|
const { default: mocks } = require('./index.js')
|
||||||
|
@ -31,6 +21,16 @@ function unregisterRoutes(caches) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPath(path) {
|
||||||
|
var match = path.toString()
|
||||||
|
.replace('\\/?', '')
|
||||||
|
.replace('(?=\\/|$)', '$')
|
||||||
|
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//)
|
||||||
|
return match
|
||||||
|
? match[1].replace(/\\(.)/g, '$1').split('/')
|
||||||
|
: path.toString()
|
||||||
|
}
|
||||||
|
|
||||||
function getMockRoutesIndex(app) {
|
function getMockRoutesIndex(app) {
|
||||||
for (let index = 0; index <= app._router.stack.length; index++) {
|
for (let index = 0; index <= app._router.stack.length; index++) {
|
||||||
const r = app._router.stack[index]
|
const r = app._router.stack[index]
|
||||||
|
@ -44,8 +44,11 @@ function getMockRoutesIndex(app) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = app => {
|
module.exports = app => {
|
||||||
|
// es6 polyfill
|
||||||
require('@babel/register')
|
require('@babel/register')
|
||||||
|
|
||||||
|
// parse app.body
|
||||||
|
// http://expressjs.com/en/4x/api.html#req.body
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
app.use(bodyParser.urlencoded({
|
app.use(bodyParser.urlencoded({
|
||||||
extended: true
|
extended: true
|
||||||
|
@ -53,17 +56,25 @@ module.exports = app => {
|
||||||
|
|
||||||
const { mockRoutesLength, caches } = registerRoutes(app)
|
const { mockRoutesLength, caches } = registerRoutes(app)
|
||||||
|
|
||||||
chokidar.watch(('./mock'), { ignored: 'mock/mock-server.js', persistent: true, ignoreInitial: false }).on('all', (event, path) => {
|
// watch files, hot reload mock server
|
||||||
|
chokidar.watch(('./mock'), {
|
||||||
|
ignored: 'mock/mock-server.js',
|
||||||
|
persistent: true,
|
||||||
|
ignoreInitial: true
|
||||||
|
}).on('all', (event, path) => {
|
||||||
if (event === 'change' || event === 'add') {
|
if (event === 'change' || event === 'add') {
|
||||||
|
// find mock routes stack index
|
||||||
const index = getMockRoutesIndex(app)
|
const index = getMockRoutesIndex(app)
|
||||||
|
|
||||||
|
// remove mock routes stack
|
||||||
app._router.stack.splice(index, mockRoutesLength)
|
app._router.stack.splice(index, mockRoutesLength)
|
||||||
console.log('caches')
|
|
||||||
console.log(index)
|
// clear routes cache
|
||||||
// console.log(path)
|
|
||||||
unregisterRoutes(caches)
|
unregisterRoutes(caches)
|
||||||
// console.log(app)
|
|
||||||
registerRoutes(app)
|
registerRoutes(app)
|
||||||
|
|
||||||
|
console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue