mock-service-cli 2.4.1 → 2.5.1
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 +24 -0
- package/README.md +32 -15
- package/bin/mock-service-cli +12 -0
- package/lib/manageMockFiles.js +1 -1
- package/lib/mockServer.js +28 -11
- package/lib/utils.js +9 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,30 @@
|
|
|
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.5.1](https://github.com/chandq/mock-service-cli/compare/v2.5.0...v2.5.1) (2022-04-25)
|
|
6
|
+
|
|
7
|
+
## [2.5.0](https://github.com/chandq/mock-service-cli/compare/v2.4.0...v2.5.0) (2022-04-25)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* **cors:** 支持cors-origin,cors-headers跨域配置 ([c2d2872](https://github.com/chandq/mock-service-cli/commit/c2d28723d047139a5165190b4ba197f46470f72f))
|
|
13
|
+
* 缓存mock统计信息 ([1ee87cd](https://github.com/chandq/mock-service-cli/commit/1ee87cd041e68b9f17b64e9e3cb906681cb855fc))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* 修复无法同时保留相同url不同请求类型的响应数据 ([90326dc](https://github.com/chandq/mock-service-cli/commit/90326dc556760eb81efadd76b031366452c95d2b))
|
|
19
|
+
|
|
20
|
+
### [2.4.3](https://github.com/chandq/mock-service-cli/compare/v2.4.2...v2.4.3) (2022-01-20)
|
|
21
|
+
|
|
22
|
+
### [2.4.2](https://github.com/chandq/mock-service-cli/compare/v2.4.1...v2.4.2) (2022-01-07)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* 缓存mock统计信息 ([1ee87cd](https://github.com/chandq/mock-service-cli/commit/1ee87cd041e68b9f17b64e9e3cb906681cb855fc))
|
|
28
|
+
|
|
5
29
|
### [2.4.1](https://github.com/chandq/mock-service-cli/compare/v2.4.0...v2.4.1) (2022-01-06)
|
|
6
30
|
|
|
7
31
|
|
package/README.md
CHANGED
|
@@ -45,17 +45,19 @@ This will install `mock-service-cli` globally so that it may be run from the com
|
|
|
45
45
|
|
|
46
46
|
`[path]` defaults to `./mock` .
|
|
47
47
|
|
|
48
|
-
| Command | Description
|
|
49
|
-
| ------------------------- |
|
|
50
|
-
| `-
|
|
51
|
-
| `-
|
|
52
|
-
| `-
|
|
53
|
-
| `-
|
|
54
|
-
| `-
|
|
55
|
-
| `-v` or `--version` | Print the version and exit.
|
|
56
|
-
| `-S` or `--socket-server` | Start socket server which used to save api response data for future mock.
|
|
57
|
-
| `-a` or `--api-stat` | Whether print api url and file path info or not, default false.
|
|
58
|
-
| `-l` or `--log` | Whether record operation info by write file, default false.
|
|
48
|
+
| Command | Description | Defaults |
|
|
49
|
+
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
|
|
50
|
+
| `-h` or `--help` | Print this list and exit. | |
|
|
51
|
+
| `-p` or `--port` | Port to use. Use `-p 0` to look for an open port, starting at 8090. It will also read from `process.env.PORT`. | 8090 |
|
|
52
|
+
| `-d` | Specify mock directory | ./mock |
|
|
53
|
+
| `-f` | Specify mock file | |
|
|
54
|
+
| `-s` or `--silent` | Suppress log messages from output | |
|
|
55
|
+
| `-v` or `--version` | Print the version and exit. | |
|
|
56
|
+
| `-S` or `--socket-server` | Start socket server which used to save api response data for future mock. | false |
|
|
57
|
+
| `-a` or `--api-stat` | Whether print api url and file path info or not, default false. | false |
|
|
58
|
+
| `-l` or `--log` | Whether record operation info by write file, default false. | false |
|
|
59
|
+
| `-o` or `--cors-origin` | Allow origin by cors, list separated by commas, must not be \* when withCredential is true .If specified, cors-headers will be `Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token` | \* |
|
|
60
|
+
| `-H` or `--cors-headers` | Optionally provide CORS headers list separated by commas | \* |
|
|
59
61
|
|
|
60
62
|
## Example
|
|
61
63
|
|
|
@@ -63,15 +65,19 @@ This will install `mock-service-cli` globally so that it may be run from the com
|
|
|
63
65
|
|
|
64
66
|
`mock-service-cli -p 8085`
|
|
65
67
|
|
|
66
|
-
2.
|
|
68
|
+
2. Enable cors, specify origin and headers.
|
|
69
|
+
|
|
70
|
+
`mock-service-cli -o "http://192.168.0.8:38200,http://10.30.30.3" -H "yy"`
|
|
71
|
+
|
|
72
|
+
3. Specify mock directory.
|
|
67
73
|
|
|
68
74
|
`mock-service-cli -d ./mock`
|
|
69
75
|
|
|
70
|
-
|
|
76
|
+
4. Specify mock file.
|
|
71
77
|
|
|
72
78
|
`mock-service-cli -f ./mock/test.js`
|
|
73
79
|
|
|
74
|
-
|
|
80
|
+
5. Start socket server for used to save api response data.
|
|
75
81
|
|
|
76
82
|
`mock-service-cli -S`
|
|
77
83
|
|
|
@@ -119,7 +125,18 @@ const mockjs = require('mockjs');
|
|
|
119
125
|
module.exports = {
|
|
120
126
|
// 使用 mockjs 等三方库
|
|
121
127
|
'GET /api/tags': mockjs.mock({
|
|
122
|
-
'list|100': [
|
|
128
|
+
'list|100': [
|
|
129
|
+
{
|
|
130
|
+
'NO|+1': 1,
|
|
131
|
+
city: '@city',
|
|
132
|
+
maintainType: '@cname(3, 5)',
|
|
133
|
+
urgentType: '@cword(3, 5)',
|
|
134
|
+
'isCrash|1': 'true',
|
|
135
|
+
createTime: '@date',
|
|
136
|
+
'value|1-100': 50,
|
|
137
|
+
'type|0-2': 1
|
|
138
|
+
}
|
|
139
|
+
]
|
|
123
140
|
})
|
|
124
141
|
};
|
|
125
142
|
```
|
package/bin/mock-service-cli
CHANGED
|
@@ -25,6 +25,10 @@ if (argv.h || argv.help) {
|
|
|
25
25
|
' -S --socket-server Whether start socket server or not, default false.',
|
|
26
26
|
' -a --api-stat Whether print api url and file path or not, default false.',
|
|
27
27
|
' -l --log Whether record operation info by write file, default false.',
|
|
28
|
+
' -o --cors-origin Allow origin by cors, list separated by commas, must not be * when withCredential is true',
|
|
29
|
+
' .If specified, cors-headers will be "Authorization,Content-Type,Accept,Origin,User-Agent,DNT',
|
|
30
|
+
' ,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token", default *.',
|
|
31
|
+
' -H --cors-headers Optionally provide CORS headers list separated by commas. default *',
|
|
28
32
|
'',
|
|
29
33
|
' -h --help Print this list and exit.',
|
|
30
34
|
' -v --version Print the version and exit.'
|
|
@@ -59,6 +63,14 @@ if (version) {
|
|
|
59
63
|
process.exit();
|
|
60
64
|
}
|
|
61
65
|
|
|
66
|
+
if (argv.o || argv['cors-origin']) {
|
|
67
|
+
process.env.CORS_ORIGIN = argv.o || argv.origin;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (argv.H || argv['cors-headers']) {
|
|
71
|
+
process.env.CORS_HEADERS = argv.H || argv['cors-headers'];
|
|
72
|
+
}
|
|
73
|
+
|
|
62
74
|
if (!port) {
|
|
63
75
|
portfinder.basePort = 8090;
|
|
64
76
|
portfinder.getPort(function (err, port) {
|
package/lib/manageMockFiles.js
CHANGED
package/lib/mockServer.js
CHANGED
|
@@ -7,7 +7,7 @@ const express = require('express'), // 引入express
|
|
|
7
7
|
chalk = require('chalk');
|
|
8
8
|
const ifaces = os.networkInterfaces();
|
|
9
9
|
const { Server } = require('socket.io');
|
|
10
|
-
const { dateFormat, logger, SupportMethods, getFileLatestContent, filePath2ApiUrl } = require('./utils');
|
|
10
|
+
const { dateFormat, logger, SupportMethods, getFileLatestContent, filePath2ApiUrl, isEmptyObj } = require('./utils');
|
|
11
11
|
const { genMockFiles, getMockStatFromDir, getMockStatFromFile } = require('./manageMockFiles');
|
|
12
12
|
|
|
13
13
|
const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
|
|
@@ -20,7 +20,9 @@ const AsyncTaskQueue = require('./asyncTaskQueue');
|
|
|
20
20
|
const saveDataAsyncTask = new AsyncTaskQueue();
|
|
21
21
|
|
|
22
22
|
let count = 0,
|
|
23
|
-
fileCount = 0
|
|
23
|
+
fileCount = 0, // 记录mock api个数,mock file个数
|
|
24
|
+
mockDirStat = {}, // 缓存mock目录的统计信息
|
|
25
|
+
mockFileStat = {}; // 缓存mock文件的统计信息
|
|
24
26
|
let done = false;
|
|
25
27
|
if (!process.env.PORT) {
|
|
26
28
|
portfinder.basePort = 8090;
|
|
@@ -38,6 +40,20 @@ if (!process.env.PORT) {
|
|
|
38
40
|
require('deasync').loopWhile(function () {
|
|
39
41
|
return !done;
|
|
40
42
|
});
|
|
43
|
+
let corsOrigin = [],
|
|
44
|
+
corsHeaders = '';
|
|
45
|
+
if (process.env.CORS_ORIGIN) {
|
|
46
|
+
process.env.CORS_ORIGIN.split(/\s*,\s*/).forEach(function (h) {
|
|
47
|
+
corsOrigin.push(h);
|
|
48
|
+
}, this);
|
|
49
|
+
corsHeaders =
|
|
50
|
+
'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token';
|
|
51
|
+
}
|
|
52
|
+
if (process.env.CORS_HEADERS) {
|
|
53
|
+
process.env.CORS_HEADERS.split(/\s*,\s*/).forEach(function (h) {
|
|
54
|
+
corsHeaders += ', ' + h;
|
|
55
|
+
}, this);
|
|
56
|
+
}
|
|
41
57
|
/**
|
|
42
58
|
* @description: 跨域设置
|
|
43
59
|
* @param {*}
|
|
@@ -46,14 +62,12 @@ require('deasync').loopWhile(function () {
|
|
|
46
62
|
const crossDomain = () => (req, res, next) => {
|
|
47
63
|
// 设置withCredentials: true时,需设置以下两项
|
|
48
64
|
res.header('Access-Control-Allow-Credentials', true);
|
|
49
|
-
//
|
|
50
|
-
res.header('Access-Control-Allow-Origin', '*');
|
|
65
|
+
// withCredentials, must be specified value instead of *
|
|
66
|
+
res.header('Access-Control-Allow-Origin', corsOrigin.includes(req.headers.origin) ? req.headers.origin : '*');
|
|
67
|
+
// res.header('Access-Control-Allow-Origin', '*');
|
|
51
68
|
res.header('Access-Control-Allow-Methods', SupportMethods.join(','));
|
|
52
|
-
|
|
53
|
-
//
|
|
54
|
-
// 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
|
|
55
|
-
// );
|
|
56
|
-
res.header('Access-Control-Allow-Headers', '*');
|
|
69
|
+
res.header('Access-Control-Allow-Headers', corsHeaders ? corsHeaders : '*');
|
|
70
|
+
// res.header('Access-Control-Allow-Headers', '*');
|
|
57
71
|
if (req.method === 'OPTIONS') res.status(200); // 让OPTIONS快速返回
|
|
58
72
|
next();
|
|
59
73
|
};
|
|
@@ -147,10 +161,13 @@ if (process.env.SOCKET_SERVER) {
|
|
|
147
161
|
colors.green(`Socket client ${clientIp} has connected. --- ${dateFormat('YYYY-mm-dd HH:MM:SS', new Date())}`)
|
|
148
162
|
);
|
|
149
163
|
socket.on('mock-dir-stat', function (dir) {
|
|
150
|
-
socket.emit('mock-dir-stat', getMockStatFromDir(dir));
|
|
164
|
+
socket.emit('mock-dir-stat', isEmptyObj(mockDirStat) ? (mockDirStat = getMockStatFromDir(dir)) : mockDirStat);
|
|
151
165
|
});
|
|
152
166
|
socket.on('mock-file-stat', function (filePath) {
|
|
153
|
-
socket.emit(
|
|
167
|
+
socket.emit(
|
|
168
|
+
'mock-file-stat',
|
|
169
|
+
isEmptyObj(mockFileStat) ? (mockFileStat = getMockStatFromFile(filePath)) : mockFileStat
|
|
170
|
+
);
|
|
154
171
|
});
|
|
155
172
|
const async = args => {
|
|
156
173
|
return async next => {
|
package/lib/utils.js
CHANGED
|
@@ -120,6 +120,14 @@ const filePath2ApiUrl = function (filePath) {
|
|
|
120
120
|
const getDataType = function (data) {
|
|
121
121
|
return Object.prototype.toString.call(data).slice(8, -1).toLowerCase();
|
|
122
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* @description: 判断对象是否为空
|
|
125
|
+
* @param {object} obj
|
|
126
|
+
* @return {boolean}
|
|
127
|
+
*/
|
|
128
|
+
const isEmptyObj = function (obj) {
|
|
129
|
+
return getDataType(obj) === 'object' && Object.keys(obj).length === 0;
|
|
130
|
+
};
|
|
123
131
|
/**
|
|
124
132
|
* @description: 流方式写文件(适合写超大文件)
|
|
125
133
|
* @param {string} filePath
|
|
@@ -275,6 +283,7 @@ module.exports = {
|
|
|
275
283
|
getFileLatestContent,
|
|
276
284
|
filePath2ApiUrl,
|
|
277
285
|
getDataType,
|
|
286
|
+
isEmptyObj,
|
|
278
287
|
writeStream,
|
|
279
288
|
readStream,
|
|
280
289
|
debounce,
|