mirror of
https://gitee.com/chu1204505056/vue-admin-beautiful.git
synced 2025-04-06 03:58:00 +08:00
96 lines
2.6 KiB
JavaScript
96 lines
2.6 KiB
JavaScript
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/settings");
|
|
const mockDir = path.join(process.cwd(), "mock");
|
|
|
|
/**
|
|
*
|
|
* @param app
|
|
* @returns {{mockStartIndex: number, mockRoutesLength: number}}
|
|
*/
|
|
const registerRoutes = (app) => {
|
|
let mockLastIndex;
|
|
const { default: 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) => {
|
|
require("@babel/register");
|
|
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));
|
|
}
|
|
}
|
|
});
|
|
};
|