mock-service-cli 2.3.1 → 2.3.2
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/README.md +1 -1
- package/lib/manageMockFiles.js +23 -13
- package/lib/mockServer.js +2 -2
- package/lib/utils.js +16 -11
- package/package.json +1 -1
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.3.2](https://github.com/chandq/mock-service-cli/compare/v2.3.1...v2.3.2) (2022-01-02)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* be sure to write file successfully ([55415e3](https://github.com/chandq/mock-service-cli/commit/55415e387af4d8232db2bede0a51dad1dca474b3))
|
|
11
|
+
|
|
5
12
|
### [2.3.1](https://github.com/chandq/mock-service-cli/compare/v2.2.0...v2.3.1) (2022-01-01)
|
|
6
13
|
|
|
7
14
|
|
package/README.md
CHANGED
|
@@ -162,7 +162,7 @@ const socket = io.connect('http://192.168.31.54:8090/mock-data', {
|
|
|
162
162
|
}
|
|
163
163
|
});
|
|
164
164
|
|
|
165
|
-
socket.emit('mock-dir-stat', '/home/chen/projects/
|
|
165
|
+
socket.emit('mock-dir-stat', '/home/chen/projects/model-ui/mock');
|
|
166
166
|
socket.on('mock-dir-stat', function (data) {
|
|
167
167
|
console.debug('mock-dir-stat:', data);
|
|
168
168
|
});
|
package/lib/manageMockFiles.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
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-02 12:16:12
|
|
6
6
|
* @Author: chendq
|
|
7
7
|
*/
|
|
8
8
|
const fsa = require('fs-extra'),
|
|
@@ -21,9 +21,23 @@ const {
|
|
|
21
21
|
} = require('./utils');
|
|
22
22
|
const log = logger(process.env.SILENT);
|
|
23
23
|
const processArgv = process.env.ARGV ? JSON.parse(process.env.ARGV) : {};
|
|
24
|
-
let LOGGER = getLogger();
|
|
25
24
|
|
|
26
25
|
const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
|
|
26
|
+
/**
|
|
27
|
+
* @description: 记录日志文件函数
|
|
28
|
+
* @param {boolean} isWriteLogFile 是否写入日志文件
|
|
29
|
+
* @param {string} mockDir 存放日志文件的目录
|
|
30
|
+
* @return {object}
|
|
31
|
+
*/
|
|
32
|
+
const logFile = (isWriteLogFile = false, mockDir) => {
|
|
33
|
+
if (isWriteLogFile) {
|
|
34
|
+
return getLogger(mockDir);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
log: function () {},
|
|
38
|
+
error: function () {}
|
|
39
|
+
};
|
|
40
|
+
};
|
|
27
41
|
/**
|
|
28
42
|
* @description: 生成mock文件(自动生成mock-list.json文件,并维护<url, [method]>的关系映射)
|
|
29
43
|
* @param {string} apiUrl 请求url
|
|
@@ -33,7 +47,7 @@ const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
|
|
|
33
47
|
* @return {*} void
|
|
34
48
|
*
|
|
35
49
|
*/
|
|
36
|
-
const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mockDir }) {
|
|
50
|
+
const genMockFiles = async function ({ url: apiUrl, method, data: resJsonData, dir: mockDir }) {
|
|
37
51
|
const type = getDataType(resJsonData);
|
|
38
52
|
if (!apiUrl || !method || !resJsonData || !mockDir) {
|
|
39
53
|
log.info(
|
|
@@ -53,7 +67,7 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
|
|
|
53
67
|
return;
|
|
54
68
|
}
|
|
55
69
|
apiUrl = apiUrl.trim().split('?')[0];
|
|
56
|
-
LOGGER =
|
|
70
|
+
const LOGGER = logFile(processArgv.l || processArgv.log, mockDir);
|
|
57
71
|
const normalizeApiUrl = filePath2ApiUrl(path.normalize(apiUrl));
|
|
58
72
|
const mockListFilePath = path.resolve(process.cwd(), `${mockDir}/mock-list.json`);
|
|
59
73
|
const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(path.normalize(apiUrl))}.json`);
|
|
@@ -74,7 +88,7 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
|
|
|
74
88
|
}
|
|
75
89
|
if (!oldMockListContent || !_.isEqual(oldMockListContent, newMockListContent)) {
|
|
76
90
|
Object.keys(newMockListContent).length > 0 &&
|
|
77
|
-
|
|
91
|
+
fs.writeFileSync(mockListFilePath, JSON.stringify(newMockListContent, null, 2));
|
|
78
92
|
}
|
|
79
93
|
|
|
80
94
|
// mock文件存在
|
|
@@ -89,17 +103,13 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
|
|
|
89
103
|
return;
|
|
90
104
|
}
|
|
91
105
|
mockFileContent[method] = resJsonData;
|
|
92
|
-
writeStream(mockFilePath, JSON.stringify(mockFileContent, null, 2));
|
|
93
|
-
|
|
94
|
-
LOGGER.log(`Update file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
95
|
-
}
|
|
106
|
+
await writeStream(mockFilePath, JSON.stringify(mockFileContent, null, 2));
|
|
107
|
+
LOGGER.log(`Update file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
96
108
|
} else {
|
|
97
109
|
// fsa.ensureFileSync(mockFilePath);
|
|
98
110
|
|
|
99
|
-
writeStream(mockFilePath, JSON.stringify({ [method]: resJsonData }, null, 2));
|
|
100
|
-
|
|
101
|
-
LOGGER.log(`Write new file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
102
|
-
}
|
|
111
|
+
await writeStream(mockFilePath, JSON.stringify({ [method]: resJsonData }, null, 2));
|
|
112
|
+
LOGGER.log(`Write new file: ${method} ${apiUrl} ${mockFilePath}`);
|
|
103
113
|
}
|
|
104
114
|
// console.debug('file::', `${mockDir}/${apiUrl}`, newMockListContent);
|
|
105
115
|
};
|
package/lib/mockServer.js
CHANGED
|
@@ -150,9 +150,9 @@ if (process.env.SOCKET_SERVER) {
|
|
|
150
150
|
socket.on('mock-file-stat', function (filePath) {
|
|
151
151
|
socket.emit('mock-file-stat', getMockStatFromFile(filePath));
|
|
152
152
|
});
|
|
153
|
-
socket.on('save-data', function (data) {
|
|
153
|
+
socket.on('save-data', async function (data) {
|
|
154
154
|
// console.log('lis save-data:', data);
|
|
155
|
-
genMockFiles(data);
|
|
155
|
+
await genMockFiles(data);
|
|
156
156
|
});
|
|
157
157
|
|
|
158
158
|
socket.on('disconnect', function () {
|
package/lib/utils.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @description: 工具函数库
|
|
3
3
|
* @Date: 2021-12-25 17:52:48
|
|
4
4
|
* @LastEditors: chendq
|
|
5
|
-
* @LastEditTime:
|
|
5
|
+
* @LastEditTime: 2022-01-02 12:32:33
|
|
6
6
|
* @Author: chendq
|
|
7
7
|
*/
|
|
8
8
|
const fs = require('fs');
|
|
@@ -119,20 +119,25 @@ const getDataType = function (data) {
|
|
|
119
119
|
* @description: 流方式写文件(适合写超大文件)
|
|
120
120
|
* @param {string} filePath
|
|
121
121
|
* @param {object} data
|
|
122
|
-
* @return {
|
|
122
|
+
* @return {promise}
|
|
123
123
|
*/
|
|
124
124
|
const writeStream = function (filePath, data) {
|
|
125
125
|
// 创建一个可写流 也会创建一个test1.txt文件
|
|
126
126
|
const writerStream = fs.createWriteStream(filePath);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
// 将数据写入流
|
|
129
|
+
writerStream.write(data, 'utf-8');
|
|
130
|
+
// 标记文件的结束
|
|
131
|
+
writerStream.end();
|
|
132
|
+
writerStream.on('finish', () => {
|
|
133
|
+
resolve(data);
|
|
134
|
+
// console.log('写入完成');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
writerStream.on('error', err => {
|
|
138
|
+
reject(err);
|
|
139
|
+
console.error(`${filePath} 写入失败`);
|
|
140
|
+
});
|
|
136
141
|
});
|
|
137
142
|
};
|
|
138
143
|
/**
|