mock-service-cli 2.3.2 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/lib/asyncTaskQueue.js +70 -0
- package/lib/manageMockFiles.js +19 -21
- package/lib/mockServer.js +17 -2
- package/lib/utils.js +116 -16
- package/package.json +1 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.4.0](https://github.com/chandq/mock-service-cli/compare/v2.3.2...v2.4.0) (2022-01-05)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* 增加异步任务队列,移出不必要模块 ([c7f3608](https://github.com/chandq/mock-service-cli/commit/c7f3608e4d0185434154d6f8d84889f281147022))
|
|
11
|
+
|
|
5
12
|
### [2.3.2](https://github.com/chandq/mock-service-cli/compare/v2.3.1...v2.3.2) (2022-01-02)
|
|
6
13
|
|
|
7
14
|
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @description: 异步任务队列
|
|
3
|
+
* @Date: 2022-01-04 10:46:20
|
|
4
|
+
* @LastEditors: chendq
|
|
5
|
+
* @LastEditTime: 2022-01-05 18:45:36
|
|
6
|
+
* @Author: chendq
|
|
7
|
+
*/
|
|
8
|
+
// const { getDataType } = require('./utils');
|
|
9
|
+
class AsyncTaskQueue {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.list = [];
|
|
12
|
+
this.index = 0;
|
|
13
|
+
this.isStop = false;
|
|
14
|
+
this.isParallel = false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
next() {
|
|
18
|
+
// 加限制
|
|
19
|
+
if (this.index >= this.list.length - 1 || this.isStop) return;
|
|
20
|
+
const cur = this.list[++this.index];
|
|
21
|
+
cur(this.next.bind(this));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @description: 增加异步任务
|
|
25
|
+
* @param {array} fn
|
|
26
|
+
*/
|
|
27
|
+
add(...fn) {
|
|
28
|
+
this.list.push(...fn);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @description: 按序执行异步任务
|
|
32
|
+
*/
|
|
33
|
+
run() {
|
|
34
|
+
const cur = this.list[this.index];
|
|
35
|
+
typeof cur === 'function' && cur(this.next.bind(this));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @description: 并发执行异步任务
|
|
40
|
+
*/
|
|
41
|
+
parallelRun() {
|
|
42
|
+
this.isParallel = true;
|
|
43
|
+
for (const fn of this.list) {
|
|
44
|
+
fn(this.next.bind(this));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @description: 暂停执行异步任务
|
|
50
|
+
*/
|
|
51
|
+
stop() {
|
|
52
|
+
this.isStop = true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @description: 重试执行异步任务
|
|
57
|
+
*/
|
|
58
|
+
retry() {
|
|
59
|
+
this.isStop = false;
|
|
60
|
+
run();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @description: 继续执行下一个异步任务
|
|
64
|
+
*/
|
|
65
|
+
goOn() {
|
|
66
|
+
this.isStop = false;
|
|
67
|
+
this.next();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
module.exports = AsyncTaskQueue;
|
package/lib/manageMockFiles.js
CHANGED
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
* @description: 生成Mock文件、获取mock数据统计
|
|
3
3
|
* @Date: 2021-12-22 16:57:08
|
|
4
4
|
* @LastEditors: chendq
|
|
5
|
-
* @LastEditTime: 2022-01-
|
|
5
|
+
* @LastEditTime: 2022-01-05 20:39:08
|
|
6
6
|
* @Author: chendq
|
|
7
7
|
*/
|
|
8
8
|
const fsa = require('fs-extra'),
|
|
9
9
|
fs = require('fs'),
|
|
10
10
|
path = require('path'),
|
|
11
|
-
_ = require('lodash'),
|
|
12
11
|
colors = require('colors/safe');
|
|
13
12
|
const {
|
|
14
13
|
getFileLatestContent,
|
|
@@ -71,45 +70,44 @@ const genMockFiles = async function ({ url: apiUrl, method, data: resJsonData, d
|
|
|
71
70
|
const normalizeApiUrl = filePath2ApiUrl(path.normalize(apiUrl));
|
|
72
71
|
const mockListFilePath = path.resolve(process.cwd(), `${mockDir}/mock-list.json`);
|
|
73
72
|
const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(path.normalize(apiUrl))}.json`);
|
|
74
|
-
let newMockListContent = {}
|
|
75
|
-
oldMockListContent = null;
|
|
73
|
+
let newMockListContent = {};
|
|
76
74
|
if (fsa.pathExistsSync(mockListFilePath)) {
|
|
77
|
-
|
|
78
|
-
newMockListContent = _.cloneDeep(oldMockListContent);
|
|
75
|
+
newMockListContent = getFileLatestContent(mockListFilePath);
|
|
79
76
|
} else {
|
|
80
77
|
fsa.ensureFileSync(mockListFilePath);
|
|
81
78
|
}
|
|
82
79
|
|
|
80
|
+
let isChange = false;
|
|
83
81
|
// update mock-list.json file
|
|
84
82
|
if (!newMockListContent.hasOwnProperty(normalizeApiUrl)) {
|
|
85
83
|
newMockListContent[normalizeApiUrl] = [method];
|
|
84
|
+
isChange = true;
|
|
86
85
|
} else if (!newMockListContent[normalizeApiUrl].includes(method)) {
|
|
87
86
|
newMockListContent[normalizeApiUrl].push(method);
|
|
87
|
+
isChange = true;
|
|
88
88
|
}
|
|
89
|
-
if (
|
|
89
|
+
if (isChange) {
|
|
90
90
|
Object.keys(newMockListContent).length > 0 &&
|
|
91
91
|
fs.writeFileSync(mockListFilePath, JSON.stringify(newMockListContent, null, 2));
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
// mock文件存在
|
|
95
95
|
if (fsa.pathExistsSync(mockFilePath)) {
|
|
96
|
-
const mockFileContent =
|
|
97
|
-
// 内容一样不作修改
|
|
98
|
-
if (mockFileContent.hasOwnProperty(method) && _.isEqual(mockFileContent[method], resJsonData)) {
|
|
99
|
-
// log.assert(
|
|
100
|
-
// !_.isEqual(mockFileContent[method], resJsonData),
|
|
101
|
-
// colors.bgYellow(`${method} ${apiUrl}, Mock data is same`)
|
|
102
|
-
// );
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
96
|
+
const mockFileContent = {};
|
|
105
97
|
mockFileContent[method] = resJsonData;
|
|
106
|
-
|
|
98
|
+
try {
|
|
99
|
+
await writeStream(mockFilePath, JSON.stringify(mockFileContent, null, 2));
|
|
100
|
+
} catch (error) {
|
|
101
|
+
LOGGER.error(`Update file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
102
|
+
}
|
|
107
103
|
LOGGER.log(`Update file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
108
104
|
} else {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
105
|
+
try {
|
|
106
|
+
await writeStream(mockFilePath, JSON.stringify({ [method]: resJsonData }, null, 2));
|
|
107
|
+
} catch (error) {
|
|
108
|
+
LOGGER.error(`New file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
109
|
+
}
|
|
110
|
+
LOGGER.log(`New file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
113
111
|
}
|
|
114
112
|
// console.debug('file::', `${mockDir}/${apiUrl}`, newMockListContent);
|
|
115
113
|
};
|
package/lib/mockServer.js
CHANGED
|
@@ -16,6 +16,8 @@ const app = express();
|
|
|
16
16
|
const log = logger(process.env.SILENT);
|
|
17
17
|
const argv = JSON.parse(process.env.ARGV);
|
|
18
18
|
let socketServer = null; // Socket server instance
|
|
19
|
+
const AsyncTaskQueue = require('./asyncTaskQueue');
|
|
20
|
+
const saveDataAsyncTask = new AsyncTaskQueue();
|
|
19
21
|
|
|
20
22
|
let count = 0,
|
|
21
23
|
fileCount = 0; // 记录mock api个数,mock file个数
|
|
@@ -150,9 +152,22 @@ if (process.env.SOCKET_SERVER) {
|
|
|
150
152
|
socket.on('mock-file-stat', function (filePath) {
|
|
151
153
|
socket.emit('mock-file-stat', getMockStatFromFile(filePath));
|
|
152
154
|
});
|
|
153
|
-
|
|
155
|
+
const async = args => {
|
|
156
|
+
return async next => {
|
|
157
|
+
await genMockFiles(args);
|
|
158
|
+
next();
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
socket.on('save-data', function (data) {
|
|
154
162
|
// console.log('lis save-data:', data);
|
|
155
|
-
|
|
163
|
+
saveDataAsyncTask.add(async(data));
|
|
164
|
+
|
|
165
|
+
if (saveDataAsyncTask.list.length === 1) {
|
|
166
|
+
saveDataAsyncTask.run();
|
|
167
|
+
} else {
|
|
168
|
+
saveDataAsyncTask.next();
|
|
169
|
+
}
|
|
170
|
+
// await genMockFiles(data);
|
|
156
171
|
});
|
|
157
172
|
|
|
158
173
|
socket.on('disconnect', function () {
|
package/lib/utils.js
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
* @description: 工具函数库
|
|
3
3
|
* @Date: 2021-12-25 17:52:48
|
|
4
4
|
* @LastEditors: chendq
|
|
5
|
-
* @LastEditTime: 2022-01-
|
|
5
|
+
* @LastEditTime: 2022-01-05 20:21:07
|
|
6
6
|
* @Author: chendq
|
|
7
7
|
*/
|
|
8
8
|
const fs = require('fs');
|
|
9
9
|
const path = require('path');
|
|
10
|
+
// const JSONStream = require('JSONStream');
|
|
10
11
|
/**
|
|
11
12
|
* @description: 输出和错误输出写入不同文件
|
|
12
13
|
* @param {string} logFileDirPath
|
|
@@ -18,11 +19,11 @@ const getLogger = function (logFileDirPath) {
|
|
|
18
19
|
encoding: 'utf8' // utf8编码
|
|
19
20
|
};
|
|
20
21
|
const output = fs.createWriteStream(
|
|
21
|
-
path.resolve(logFileDirPath ? logFileDirPath : process.cwd(), './stdout.log'),
|
|
22
|
+
path.resolve(logFileDirPath ? logFileDirPath : path.resolve(process.cwd()), './stdout.log'),
|
|
22
23
|
options
|
|
23
24
|
);
|
|
24
25
|
const errorOutput = fs.createWriteStream(
|
|
25
|
-
path.resolve(logFileDirPath ? logFileDirPath : process.cwd(), './stderr.log'),
|
|
26
|
+
path.resolve(logFileDirPath ? logFileDirPath : path.resolve(process.cwd()), './stderr.log'),
|
|
26
27
|
options
|
|
27
28
|
);
|
|
28
29
|
// 自定义日志打印
|
|
@@ -38,9 +39,9 @@ const getLogger = function (logFileDirPath) {
|
|
|
38
39
|
};
|
|
39
40
|
/**
|
|
40
41
|
* @description: 格式化时间日期
|
|
41
|
-
* @param {
|
|
42
|
-
* @param {
|
|
43
|
-
* @returns {
|
|
42
|
+
* @param {string} fmt 时间日期字符串格式化模板
|
|
43
|
+
* @param {date} date 时间日期Date
|
|
44
|
+
* @returns {string}
|
|
44
45
|
*/
|
|
45
46
|
function dateFormat(fmt, date = new Date()) {
|
|
46
47
|
let ret;
|
|
@@ -65,8 +66,8 @@ function dateFormat(fmt, date = new Date()) {
|
|
|
65
66
|
}
|
|
66
67
|
/**
|
|
67
68
|
* @description: 控制是否写日志
|
|
68
|
-
* @param {
|
|
69
|
-
* @return {
|
|
69
|
+
* @param {boolean} isSilent 是否静默
|
|
70
|
+
* @return {object}
|
|
70
71
|
*/
|
|
71
72
|
function logger(isSilent = false) {
|
|
72
73
|
let logObj = null;
|
|
@@ -96,8 +97,12 @@ const isValidMethod = function (m) {
|
|
|
96
97
|
* @return {object} 文件内容
|
|
97
98
|
*/
|
|
98
99
|
const getFileLatestContent = function (filePath) {
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
try {
|
|
101
|
+
delete require.cache[require.resolve(filePath)];
|
|
102
|
+
return require(path.resolve(process.cwd(), filePath));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
getLogger(path.resolve(process.cwd())).error(filePath, require.cache[require.resolve(filePath)], error);
|
|
105
|
+
}
|
|
101
106
|
};
|
|
102
107
|
/**
|
|
103
108
|
* @description: 文件路径转成API的url
|
|
@@ -123,20 +128,20 @@ const getDataType = function (data) {
|
|
|
123
128
|
*/
|
|
124
129
|
const writeStream = function (filePath, data) {
|
|
125
130
|
// 创建一个可写流 也会创建一个test1.txt文件
|
|
126
|
-
const writerStream = fs.createWriteStream(filePath);
|
|
127
131
|
return new Promise((resolve, reject) => {
|
|
132
|
+
const writerStream = fs.createWriteStream(filePath);
|
|
128
133
|
// 将数据写入流
|
|
129
134
|
writerStream.write(data, 'utf-8');
|
|
130
135
|
// 标记文件的结束
|
|
131
136
|
writerStream.end();
|
|
132
|
-
writerStream.on('finish', () => {
|
|
137
|
+
writerStream.on('finish', () => {});
|
|
138
|
+
writerStream.on('close', () => {
|
|
133
139
|
resolve(data);
|
|
134
|
-
// console.log('写入完成');
|
|
135
140
|
});
|
|
136
141
|
|
|
137
142
|
writerStream.on('error', err => {
|
|
143
|
+
getLogger(path.resolve(process.cwd())).error(filePath, require.cache[require.resolve(filePath)], err);
|
|
138
144
|
reject(err);
|
|
139
|
-
console.error(`${filePath} 写入失败`);
|
|
140
145
|
});
|
|
141
146
|
});
|
|
142
147
|
};
|
|
@@ -145,7 +150,7 @@ const writeStream = function (filePath, data) {
|
|
|
145
150
|
* @param {string} filePath
|
|
146
151
|
* @return {*}
|
|
147
152
|
*/
|
|
148
|
-
const
|
|
153
|
+
const readStream = function (filePath) {
|
|
149
154
|
let data = '';
|
|
150
155
|
// 创建可读流
|
|
151
156
|
return new Promise((resovle, reject) => {
|
|
@@ -166,6 +171,99 @@ const readeStream = function (filePath) {
|
|
|
166
171
|
});
|
|
167
172
|
};
|
|
168
173
|
|
|
174
|
+
/**
|
|
175
|
+
* 防抖函数
|
|
176
|
+
* 当函数被连续调用时,该函数并不执行,只有当其全部停止调用超过一定时间后才执行1次。
|
|
177
|
+
* 例如:上电梯的时候,大家陆陆续续进来,电梯的门不会关上,只有当一段时间都没有人上来,电梯才会关门。
|
|
178
|
+
* @param {F} func
|
|
179
|
+
* @param {number} wait
|
|
180
|
+
* @returns {DebounceFunc<F>}
|
|
181
|
+
*/
|
|
182
|
+
const debounce = (func, wait) => {
|
|
183
|
+
let timeout;
|
|
184
|
+
let canceled = false;
|
|
185
|
+
const f = function (...args) {
|
|
186
|
+
if (canceled) return;
|
|
187
|
+
clearTimeout(timeout);
|
|
188
|
+
timeout = setTimeout(() => {
|
|
189
|
+
func.call(this, ...args);
|
|
190
|
+
}, wait);
|
|
191
|
+
};
|
|
192
|
+
f.cancel = () => {
|
|
193
|
+
clearTimeout(timeout);
|
|
194
|
+
canceled = true;
|
|
195
|
+
};
|
|
196
|
+
return f;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* 节流函数
|
|
200
|
+
* 节流就是节约流量,将连续触发的事件稀释成预设评率。 比如每间隔1秒执行一次函数,无论这期间触发多少次事件。
|
|
201
|
+
* 这有点像公交车,无论在站点等车的人多不多,公交车只会按时来一班,不会来一个人就来一辆公交车。
|
|
202
|
+
* @param {F} func
|
|
203
|
+
* @param {number} wait
|
|
204
|
+
* @param {boolean} immediate
|
|
205
|
+
* @returns {ThrottleFunc<F>}
|
|
206
|
+
*/
|
|
207
|
+
const throttle = (func, wait, immediate) => {
|
|
208
|
+
let timeout;
|
|
209
|
+
let canceled = false;
|
|
210
|
+
let lastCalledTime = 0;
|
|
211
|
+
const f = function (...args) {
|
|
212
|
+
if (canceled) return;
|
|
213
|
+
const now = Date.now();
|
|
214
|
+
const call = () => {
|
|
215
|
+
lastCalledTime = now;
|
|
216
|
+
func.call(this, ...args);
|
|
217
|
+
};
|
|
218
|
+
// 第一次执行
|
|
219
|
+
if (lastCalledTime === 0) {
|
|
220
|
+
if (immediate) {
|
|
221
|
+
return call();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
lastCalledTime = now;
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const remain = lastCalledTime + wait - now;
|
|
228
|
+
if (remain > 0) {
|
|
229
|
+
clearTimeout(timeout);
|
|
230
|
+
timeout = setTimeout(() => call(), wait);
|
|
231
|
+
} else {
|
|
232
|
+
call();
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
f.cancel = () => {
|
|
236
|
+
clearTimeout(timeout);
|
|
237
|
+
canceled = true;
|
|
238
|
+
};
|
|
239
|
+
return f;
|
|
240
|
+
};
|
|
241
|
+
// /**
|
|
242
|
+
// * @description: 带模糊搜索的分块读取大JSON文件
|
|
243
|
+
// * @param {string} filePath
|
|
244
|
+
// * @return {*}
|
|
245
|
+
// */
|
|
246
|
+
// const readBigJson = function (filePath) {
|
|
247
|
+
// return new Promise((resolve, reject) => {
|
|
248
|
+
// let res = '';
|
|
249
|
+
// const readable = fs.createReadStream(filePath, {
|
|
250
|
+
// encoding: 'utf8',
|
|
251
|
+
// highWaterMark: 10
|
|
252
|
+
// });
|
|
253
|
+
// const parser = JSONStream.parse('*');
|
|
254
|
+
// readable.pipe(parser);
|
|
255
|
+
// parser.on('end', function () {
|
|
256
|
+
// // I know it ends here,
|
|
257
|
+
// console.log('end::', res);
|
|
258
|
+
// resolve(res);
|
|
259
|
+
// });
|
|
260
|
+
// parser.on('data', function (data) {
|
|
261
|
+
// res += data;
|
|
262
|
+
// console.log('yy::', data);
|
|
263
|
+
// });
|
|
264
|
+
// });
|
|
265
|
+
// };
|
|
266
|
+
|
|
169
267
|
module.exports = {
|
|
170
268
|
getLogger,
|
|
171
269
|
dateFormat,
|
|
@@ -176,5 +274,7 @@ module.exports = {
|
|
|
176
274
|
filePath2ApiUrl,
|
|
177
275
|
getDataType,
|
|
178
276
|
writeStream,
|
|
179
|
-
|
|
277
|
+
readStream,
|
|
278
|
+
debounce,
|
|
279
|
+
throttle
|
|
180
280
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mock-service-cli",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "🦅 Local mock server",
|
|
5
5
|
"main": "./lib/mockServer.js",
|
|
6
6
|
"bin": {
|
|
@@ -49,7 +49,6 @@
|
|
|
49
49
|
"deasync": "^0.1.23",
|
|
50
50
|
"express": "^4.17.1",
|
|
51
51
|
"fs-extra": "^10.0.0",
|
|
52
|
-
"lodash": "^4.17.21",
|
|
53
52
|
"minimist": "^1.2.5",
|
|
54
53
|
"nodemon": "^2.0.14",
|
|
55
54
|
"portfinder": "^1.0.28",
|