mock-service-cli 2.0.2 → 2.0.3
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/manageMockFiles.js +11 -11
- package/lib/mockServer.js +4 -4
- package/lib/utils.js +24 -1
- 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.0.3](https://github.com/chandq/mock-service-cli/compare/v2.0.2...v2.0.3) (2021-12-27)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* convert windows file path to api url ([1f15b33](https://github.com/chandq/mock-service-cli/commit/1f15b334ab5374bc102e5e11fbfaae994569bca5))
|
|
11
|
+
|
|
5
12
|
### [2.0.2](https://github.com/chandq/mock-service-cli/compare/v2.0.1...v2.0.2) (2021-12-27)
|
|
6
13
|
|
|
7
14
|
|
package/lib/manageMockFiles.js
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
const fsa = require('fs-extra'),
|
|
9
9
|
fs = require('fs'),
|
|
10
10
|
path = require('path'),
|
|
11
|
-
_ = require('lodash')
|
|
12
|
-
|
|
13
|
-
const { getFileLatestContent, SupportMethods,
|
|
14
|
-
const log = logger(process.env.SILENT);
|
|
11
|
+
_ = require('lodash');
|
|
12
|
+
// colors = require('colors/safe');
|
|
13
|
+
const { getFileLatestContent, SupportMethods, filePath2ApiUrl } = require('./utils');
|
|
14
|
+
// const log = logger(process.env.SILENT);
|
|
15
15
|
|
|
16
16
|
const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
|
|
17
17
|
/**
|
|
@@ -24,9 +24,9 @@ const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
|
|
|
24
24
|
*
|
|
25
25
|
*/
|
|
26
26
|
const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mockDir }) {
|
|
27
|
-
const
|
|
27
|
+
const normalizeApiUrl = filePath2ApiUrl(path.normalize(apiUrl));
|
|
28
28
|
const mockListFilePath = path.resolve(process.cwd(), `${mockDir}/mock-list.json`);
|
|
29
|
-
const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(
|
|
29
|
+
const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(path.normalize(apiUrl))}.json`);
|
|
30
30
|
let newMockListContent = {},
|
|
31
31
|
oldMockListContent = null;
|
|
32
32
|
if (fsa.pathExistsSync(mockListFilePath)) {
|
|
@@ -37,10 +37,10 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// update mock-list.json file
|
|
40
|
-
if (!newMockListContent.hasOwnProperty(
|
|
41
|
-
newMockListContent[
|
|
42
|
-
} else if (!newMockListContent[
|
|
43
|
-
newMockListContent[
|
|
40
|
+
if (!newMockListContent.hasOwnProperty(normalizeApiUrl)) {
|
|
41
|
+
newMockListContent[normalizeApiUrl] = [method];
|
|
42
|
+
} else if (!newMockListContent[normalizeApiUrl].includes(method)) {
|
|
43
|
+
newMockListContent[normalizeApiUrl].push(method);
|
|
44
44
|
}
|
|
45
45
|
if (!oldMockListContent || !_.isEqual(oldMockListContent, newMockListContent)) {
|
|
46
46
|
fsa.writeFileSync(mockListFilePath, JSON.stringify(newMockListContent, null, 2));
|
|
@@ -63,7 +63,7 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
|
|
|
63
63
|
// fsa.ensureFileSync(mockFilePath);
|
|
64
64
|
fsa.writeFileSync(mockFilePath, JSON.stringify({ [method]: resJsonData ? resJsonData : {} }, null, 2));
|
|
65
65
|
}
|
|
66
|
-
// console.debug('file::', `${mockDir}/${
|
|
66
|
+
// console.debug('file::', `${mockDir}/${apiUrl}`, newMockListContent);
|
|
67
67
|
};
|
|
68
68
|
/**
|
|
69
69
|
* @description: 从js文件中收集mock数据
|
package/lib/mockServer.js
CHANGED
|
@@ -60,7 +60,7 @@ const crossDomain = () => (req, res, next) => {
|
|
|
60
60
|
* @param {*} file
|
|
61
61
|
* @returns {*}
|
|
62
62
|
*/
|
|
63
|
-
const
|
|
63
|
+
const composeRouteFromJsFile = function (file) {
|
|
64
64
|
// 先删除require之前导入的缓存文件,否则获取到的文件内容还是旧内容
|
|
65
65
|
const fileObject = getFileLatestContent(file);
|
|
66
66
|
|
|
@@ -121,12 +121,12 @@ const parseMockFiles = function (specialDir = '../mock') {
|
|
|
121
121
|
if (!el.name.endsWith('.js')) {
|
|
122
122
|
continue;
|
|
123
123
|
}
|
|
124
|
-
|
|
124
|
+
composeRouteFromJsFile(filePath);
|
|
125
125
|
}
|
|
126
126
|
};
|
|
127
127
|
app.use(crossDomain());
|
|
128
128
|
if (process.env.SPECIFIED_FILE) {
|
|
129
|
-
|
|
129
|
+
composeRouteFromJsFile(process.env.SPECIFIED_FILE);
|
|
130
130
|
} else {
|
|
131
131
|
parseMockFiles(process.env.SPECIFIED_DIR);
|
|
132
132
|
}
|
|
@@ -201,6 +201,6 @@ process.on('SIGTERM', function () {
|
|
|
201
201
|
});
|
|
202
202
|
|
|
203
203
|
module.exports = {
|
|
204
|
-
|
|
204
|
+
composeRouteFromJsFile,
|
|
205
205
|
parseMockFiles
|
|
206
206
|
};
|
package/lib/utils.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @description:
|
|
3
|
+
* @Date: 2021-12-25 17:52:48
|
|
4
|
+
* @LastEditors: chendq
|
|
5
|
+
* @LastEditTime: 2021-12-27 13:12:16
|
|
6
|
+
* @Author: chendq
|
|
7
|
+
*/
|
|
1
8
|
/**
|
|
2
9
|
* @description: 输出和错误输出写入不同文件
|
|
3
10
|
* @param {*}
|
|
@@ -78,5 +85,21 @@ const getFileLatestContent = function (filePath) {
|
|
|
78
85
|
delete require.cache[require.resolve(filePath)];
|
|
79
86
|
return require(path.resolve(process.cwd(), filePath));
|
|
80
87
|
};
|
|
88
|
+
/**
|
|
89
|
+
* @description: 文件路径转成API的url
|
|
90
|
+
* @param {string} filePath
|
|
91
|
+
* @return {string} apiUrl
|
|
92
|
+
*/
|
|
93
|
+
const filePath2ApiUrl = function (filePath) {
|
|
94
|
+
return filePath.split(path.sep).join('/');
|
|
95
|
+
};
|
|
81
96
|
|
|
82
|
-
module.exports = {
|
|
97
|
+
module.exports = {
|
|
98
|
+
getLogger,
|
|
99
|
+
dateFormat,
|
|
100
|
+
logger,
|
|
101
|
+
SupportMethods,
|
|
102
|
+
isValidMethod,
|
|
103
|
+
getFileLatestContent,
|
|
104
|
+
filePath2ApiUrl
|
|
105
|
+
};
|