1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const mockjs = require('mockjs');
const fs = require('fs');
const path = require('path');
const util = require('./util');
const openAPIParserMock = require('./openAPIParserMock/index');
const log = require('./log');
mockjs.Random.extend({
country() {
const data = [
'阿根廷',
'澳大利亚',
'巴西',
'加拿大',
'中国',
'法国',
'德国',
'印度',
'印度尼西亚',
'意大利',
'日本',
'韩国',
'墨西哥',
'俄罗斯',
'沙特阿拉伯',
'南非',
'土耳其',
'英国',
'美国',
];
const id = (Math.random() * data.length).toFixed();
return data[id];
},
phone() {
const phonepreFix = ['111', '112', '114']; // 自己写前缀哈
return this.pick(phonepreFix) + mockjs.mock(/\d{8}/); // Number()
},
status() {
const status = ['success', 'error', 'default', 'processing', 'warning'];
return status[(Math.random() * 4).toFixed(0)];
},
authority() {
const status = ['admin', 'user', 'guest'];
return status[(Math.random() * status.length).toFixed(0)];
},
avatar() {
const avatar = [
'https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg',
'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
'https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png',
'https://avatars0.githubusercontent.com/u/507615?s=40&v=4',
'https://avatars1.githubusercontent.com/u/8186664?s=40&v=4',
];
const id = (Math.random() * avatar.length).toFixed();
return avatar[id];
},
group() {
const data = [
'体验技术部',
'创新科技组',
'前端 6 组',
'区块链平台部',
'服务技术部',
];
const id = (Math.random() * data.length).toFixed();
return data[id];
},
label() {
const label = [
'很有想法的',
'小清新',
'傻白甜',
'阳光少年',
'大咖',
'健身达人',
'程序员',
'算法工程师',
'川妹子',
'名望程序员',
'大长腿',
'海纳百川',
'专注设计',
'爱好广泛',
'IT 互联网',
];
const id = (Math.random() * label.length).toFixed();
return label[id];
},
href() {
const href = [
'https://preview.pro.ant.design/dashboard/analysis',
'https://ant.design',
'https://procomponents.ant.design/',
'https://umijs.org/',
'https://github.com/umijs/dumi',
];
const id = (Math.random() * href.length).toFixed();
return href[id];
},
});
const genMockData = example => {
if (!example) {
return {};
}
if (typeof example === 'string') {
return mockjs.mock(example);
}
if (Array.isArray(example)) {
return mockjs.mock(example);
}
return Object.keys(example)
.map(name => ({
[name]: mockjs.mock(example[name]),
}))
.reduce(
(pre, next) => ({
...pre,
...next,
}),
{},
);
};
// eslint-disable-next-line no-shadow
const genByTemp = ({ method, path, status, data }) => {
if (
!['get', 'put', 'post', 'delete', 'patch'].includes(
method.toLocaleLowerCase(),
)
) {
return '';
}
return `'${method.toUpperCase()} ${path}': (req, res) => {
res.status(${status}).send(${data});
}`;
};
const genMockFiles = mockFunction =>
util.prettierFile(`
// @ts-ignore
//import { Request, Response } from 'express';
module.exports = {
${mockFunction.join('\n,')}
}`)[0];
const mockGenerator = async ({ openAPI, mockFolder }) => {
// eslint-disable-next-line new-cap
const openAPParse = new openAPIParserMock(openAPI);
const docs = openAPParse.parser();
const pathList = Object.keys(docs.paths);
const { paths } = docs;
const mockActionsObj = {};
// eslint-disable-next-line no-shadow
pathList.forEach(path => {
const pathConfig = paths[path];
Object.keys(pathConfig).forEach(method => {
const methodConfig = pathConfig[method];
if (methodConfig) {
const conte =
(methodConfig && methodConfig.tags && methodConfig.tags.join('/')) ||
path.replace('/', '').split('/')[1] ||
methodConfig.operationId;
const data = genMockData(
methodConfig &&
methodConfig.responses &&
methodConfig.responses['200'] &&
methodConfig.responses['200'].example,
);
if (!mockActionsObj[conte]) {
mockActionsObj[conte] = [];
}
const tempFile = genByTemp({
method,
path,
status: '200',
data: JSON.stringify(data),
});
if (tempFile) {
mockActionsObj[conte].push(tempFile);
}
}
});
});
Object.keys(mockActionsObj).forEach(file => {
if (!file || file === 'undefined') {
return;
}
fs.writeFileSync(
path.join(mockFolder, `${file}.mock.js`),
genMockFiles(mockActionsObj[file]),
{
encoding: 'utf8',
},
);
});
log('✅ 生成 mock 文件成功');
};
module.exports = {
mockGenerator,
};