Match-id-567d4a8e9d4f25bba5f2b69c47ffbb2cea552455
This commit is contained in:
parent
b56a76b8dd
commit
6830b1525f
|
@ -0,0 +1,3 @@
|
||||||
|
node_modules/
|
||||||
|
webpack/
|
||||||
|
public/
|
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
|
||||||
|
*
|
||||||
|
* openGauss is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
*
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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)
|
||||||
|
};
|
|
@ -0,0 +1,145 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
|
||||||
|
*
|
||||||
|
* openGauss is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
*
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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