From 9f9eae5919ed673588a29acff31f534faa24cac8 Mon Sep 17 00:00:00 2001 From: chuzhixin <1204505056@qq.com> Date: Sun, 8 Nov 2020 09:10:19 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Update=20global=20mock-server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mock/index.js | 108 +++++++++++++++++++++++++++++++++++++++------ mock/mockServer.js | 93 -------------------------------------- vue.config.js | 3 +- 3 files changed, 96 insertions(+), 108 deletions(-) delete mode 100644 mock/mockServer.js diff --git a/mock/index.js b/mock/index.js index 0b91890..1910296 100644 --- a/mock/index.js +++ b/mock/index.js @@ -1,16 +1,98 @@ -/** - * @author chuzhixin 1204505056@qq.com - * @description 导入所有 controller 模块,npm run serve时在node环境中自动输出controller文件夹下Mock接口,请勿修改。 - */ - +const chokidar = require('chokidar') +const bodyParser = require('body-parser') +const chalk = require('chalk') +const path = require('path') +const { mock } = require('mockjs') +const { baseURL } = require('../src/config') +const mockDir = path.join(process.cwd(), 'mock') const { handleMockArray } = require('./utils') -const mocks = [] -const mockArray = handleMockArray() -mockArray.forEach((item) => { - const obj = require(item) - mocks.push(...obj) -}) -module.exports = { - mocks, +/** + * + * @param app + * @returns {{mockStartIndex: number, mockRoutesLength: number}} + */ +const registerRoutes = (app) => { + let mockLastIndex + const mocks = [] + const mockArray = handleMockArray() + mockArray.forEach((item) => { + const obj = require(item) + mocks.push(...obj) + }) + const mocksForServer = mocks.map((route) => { + return responseFake(route.url, route.type, route.response) + }) + for (const mock of mocksForServer) { + app[mock.type](mock.url, mock.response) + mockLastIndex = app._router.stack.length + } + const mockRoutesLength = Object.keys(mocksForServer).length + return { + mockRoutesLength: mockRoutesLength, + mockStartIndex: mockLastIndex - mockRoutesLength, + } +} + +/** + * + * @param url + * @param type + * @param respond + * @returns {{response(*=, *=): void, type: (*|string), url: RegExp}} + */ +const responseFake = (url, type, respond) => { + return { + url: new RegExp(`${baseURL}${url}`), + type: type || 'get', + response(req, res) { + res.status(200) + if (JSON.stringify(req.body) !== '{}') { + console.log(chalk.green(`> 请求地址:${req.path}`)) + console.log(chalk.green(`> 请求参数:${JSON.stringify(req.body)}\n`)) + } else { + console.log(chalk.green(`> 请求地址:${req.path}\n`)) + } + res.json(mock(respond instanceof Function ? respond(req, res) : respond)) + }, + } +} +/** + * + * @param app + */ +module.exports = (app) => { + app.use(bodyParser.json()) + app.use( + bodyParser.urlencoded({ + extended: true, + }) + ) + + const mockRoutes = registerRoutes(app) + let mockRoutesLength = mockRoutes.mockRoutesLength + let mockStartIndex = mockRoutes.mockStartIndex + chokidar + .watch(mockDir, { + ignored: /mock-server/, + ignoreInitial: true, + }) + .on('all', (event) => { + if (event === 'change' || event === 'add') { + try { + app._router.stack.splice(mockStartIndex, mockRoutesLength) + + Object.keys(require.cache).forEach((item) => { + if (item.includes(mockDir)) { + delete require.cache[require.resolve(item)] + } + }) + const mockRoutes = registerRoutes(app) + mockRoutesLength = mockRoutes.mockRoutesLength + mockStartIndex = mockRoutes.mockStartIndex + } catch (error) { + console.log(chalk.red(error)) + } + } + }) } diff --git a/mock/mockServer.js b/mock/mockServer.js deleted file mode 100644 index 0158664..0000000 --- a/mock/mockServer.js +++ /dev/null @@ -1,93 +0,0 @@ -const chokidar = require('chokidar') -const bodyParser = require('body-parser') -const chalk = require('chalk') -const path = require('path') -const Mock = require('mockjs') -const { baseURL } = require('../src/config') -const mockDir = path.join(process.cwd(), 'mock') - -/** - * - * @param app - * @returns {{mockStartIndex: number, mockRoutesLength: number}} - */ -const registerRoutes = (app) => { - let mockLastIndex - const { mocks } = require('./index.js') - const mocksForServer = mocks.map((route) => { - return responseFake(route.url, route.type, route.response) - }) - for (const mock of mocksForServer) { - app[mock.type](mock.url, mock.response) - mockLastIndex = app._router.stack.length - } - const mockRoutesLength = Object.keys(mocksForServer).length - return { - mockRoutesLength: mockRoutesLength, - mockStartIndex: mockLastIndex - mockRoutesLength, - } -} - -/** - * - * @param url - * @param type - * @param respond - * @returns {{response(*=, *=): void, type: (*|string), url: RegExp}} - */ -const responseFake = (url, type, respond) => { - return { - url: new RegExp(`${baseURL}${url}`), - type: type || 'get', - response(req, res) { - res.status(200) - if (JSON.stringify(req.body) !== '{}') { - console.log(chalk.green(`> 请求地址:${req.path}`)) - console.log(chalk.green(`> 请求参数:${JSON.stringify(req.body)}\n`)) - } else { - console.log(chalk.green(`> 请求地址:${req.path}\n`)) - } - res.json( - Mock.mock(respond instanceof Function ? respond(req, res) : respond) - ) - }, - } -} -/** - * - * @param app - */ -module.exports = (app) => { - app.use(bodyParser.json()) - app.use( - bodyParser.urlencoded({ - extended: true, - }) - ) - - const mockRoutes = registerRoutes(app) - let mockRoutesLength = mockRoutes.mockRoutesLength - let mockStartIndex = mockRoutes.mockStartIndex - chokidar - .watch(mockDir, { - ignoreInitial: true, - }) - .on('all', (event) => { - if (event === 'change' || event === 'add') { - try { - app._router.stack.splice(mockStartIndex, mockRoutesLength) - - Object.keys(require.cache).forEach((item) => { - if (item.includes(mockDir)) { - delete require.cache[require.resolve(item)] - } - }) - const mockRoutes = registerRoutes(app) - mockRoutesLength = mockRoutes.mockRoutesLength - mockStartIndex = mockRoutes.mockStartIndex - } catch (error) { - console.log(chalk.red(error)) - } - } - }) -} diff --git a/vue.config.js b/vue.config.js index cfd7a75..edcbd4d 100644 --- a/vue.config.js +++ b/vue.config.js @@ -35,8 +35,7 @@ process.env.VUE_APP_VERSION = version const resolve = (dir) => path.join(__dirname, dir) const mockServer = () => { - if (process.env.NODE_ENV === 'development') - return require('./mock/mockServer.js') + if (process.env.NODE_ENV === 'development') return require('./mock') else return '' }