Match-id-c5a473aa5e915236c297b8e610d7e1abdedd5999
This commit is contained in:
parent
97b26a87ab
commit
b56a76b8dd
|
@ -1,3 +0,0 @@
|
|||
node_modules/
|
||||
webpack/
|
||||
public/
|
|
@ -1,15 +0,0 @@
|
|||
export default {
|
||||
printWidth: 120, // 一行120字符数,如果超过会进行换行
|
||||
tabWidth: 2, // tab等2个空格
|
||||
useTabs: false, // 用空格缩进行
|
||||
semi: true, // 行尾使用分号
|
||||
singleQuote: true, // 字符串使用单引号
|
||||
quoteProps: 'as-needed', // 仅在需要时在对象属性添加引号
|
||||
jsxSingleQuote: false, // 在JSX中使用双引号
|
||||
trailingComma: 'es5', // 使用尾逗号(对象、数组等)
|
||||
bracketSpacing: true, // 对象的括号间增加空格
|
||||
jsxBracketSameLine: false, // 将多行JSX元素的>放在最后一行的末尾
|
||||
arrowParens: 'avoid', // 在唯一的arrow函数参数周围省略括号
|
||||
vueIndentScriptAndStyle: false, // 不缩进Vue文件中的<script>和<style>标记内的代码
|
||||
endOfLine: 'lf', // 仅限换行(\n)
|
||||
};
|
|
@ -1,130 +0,0 @@
|
|||
import chokidar from 'chokidar';
|
||||
import bodyParser from 'body-parser';
|
||||
import {globSync} from 'glob';
|
||||
import { join } from 'path';
|
||||
|
||||
import { createRequire } from 'module';
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
const mockDir = join(process.cwd(), 'mock');
|
||||
const HTTP_METHODS = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head'];
|
||||
|
||||
const jsonParser = bodyParser.json();
|
||||
const urlencodedParser = bodyParser.urlencoded({
|
||||
extended: true,
|
||||
});
|
||||
|
||||
interface Mock {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// 读取 mock 文件夹下的 js 文件
|
||||
function getMocksFile() {
|
||||
const mockFiles = globSync('**/*.js', {
|
||||
cwd: mockDir,
|
||||
});
|
||||
let ret = mockFiles.reduce((mocks: any, mockFile: string) => {
|
||||
if (!mockFile.startsWith('_')) {
|
||||
mocks = {
|
||||
...mocks,
|
||||
...require(join(mockDir, mockFile)),
|
||||
};
|
||||
console.log('mockFile', require(join(mockDir, mockFile)));
|
||||
}
|
||||
|
||||
return mocks;
|
||||
}, {});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
function generateRoutes(app: any) {
|
||||
let mockStartIndex = app._router.stack.length,
|
||||
mocks: Mock = {};
|
||||
|
||||
try {
|
||||
mocks = getMocksFile();
|
||||
} catch (error) {
|
||||
console.error('Generate mock routes error', error);
|
||||
}
|
||||
|
||||
for (const mockItem in mocks) {
|
||||
if (Object.prototype.hasOwnProperty.call(mocks, mockItem)) {
|
||||
try {
|
||||
const trimMockItemArr = mockItem
|
||||
.replace(/(^\s*)|(\s*$)/g, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.split(' ');
|
||||
|
||||
const respond = mocks[mockItem];
|
||||
|
||||
let mockType = 'get',
|
||||
mockUrl;
|
||||
if (trimMockItemArr.length === 1) {
|
||||
mockUrl = trimMockItemArr[0];
|
||||
} else {
|
||||
[mockType, mockUrl] = trimMockItemArr;
|
||||
}
|
||||
|
||||
const mockTypeLowerCase = mockType.toLowerCase();
|
||||
|
||||
if (!HTTP_METHODS.includes(mockTypeLowerCase)) {
|
||||
throw new Error(`Invalid HTTP request method ${mockType} for path ${mockUrl}`);
|
||||
}
|
||||
|
||||
app[mockTypeLowerCase](
|
||||
mockUrl,
|
||||
[jsonParser, urlencodedParser],
|
||||
respond instanceof Function
|
||||
? respond
|
||||
: (_req: any, res: { send: (arg0: any) => void }) => {
|
||||
res.send(respond);
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
mockRoutesLength: app._router.stack.length - mockStartIndex,
|
||||
mockStartIndex: mockStartIndex,
|
||||
};
|
||||
}
|
||||
|
||||
// 清除 mock 文件下的 require 缓存
|
||||
function cleanRequireCache() {
|
||||
Object.keys(require.cache).forEach(key => {
|
||||
if (key.includes(mockDir)) {
|
||||
delete require.cache[require.resolve(key)];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default (app: { _router: { stack: any[] } }) => {
|
||||
const mockRoutes = generateRoutes(app);
|
||||
let { mockRoutesLength } = mockRoutes;
|
||||
let { mockStartIndex } = mockRoutes;
|
||||
|
||||
// 监听 mock 文件夹下文件变化
|
||||
chokidar
|
||||
.watch(mockDir, {
|
||||
ignoreInitial: true,
|
||||
})
|
||||
.on('all', (event: string, _path: any) => {
|
||||
if (event === 'change' || event === 'add') {
|
||||
try {
|
||||
// 删除中间件映射
|
||||
app._router.stack.splice(mockStartIndex, mockRoutesLength);
|
||||
|
||||
cleanRequireCache();
|
||||
const mockRoutes = generateRoutes(app);
|
||||
|
||||
mockRoutesLength = mockRoutes.mockRoutesLength;
|
||||
mockStartIndex = mockRoutes.mockStartIndex;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue