mock-service-cli 3.1.8 → 3.3.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/README.md +19 -11
- package/bin/mock-service-cli +19 -0
- package/lib/staticServer.js +76 -0
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
[](https://github.com/chandq/mock-service-cli/actions/workflows/node.js.yml)
|
|
2
2
|
[](https://coveralls.io/github/chandq/mock-service-cli?branch=master)
|
|
3
3
|
[](https://github.com/chandq/mock-service-cli)
|
|
4
|
-
[](https://nodejs.org/download/release/v14.0.0/)
|
|
5
4
|
[](https://nodejs.org/download/release/v12.0.0/)
|
|
6
5
|
[](https://github.com/chandq/mock-service-cli/blob/master/LICENSE.md)
|
|
7
6
|
[](https://npmcharts.com/compare/mock-service-cli?minimal=true)
|
|
@@ -10,7 +9,7 @@
|
|
|
10
9
|
|
|
11
10
|
### 简介
|
|
12
11
|
|
|
13
|
-
内置 Mock Server
|
|
12
|
+
内置 **Static Server**、**Mock Server**、**SPA Web Server**、Http?s request proxy 等功能集。
|
|
14
13
|
|
|
15
14
|
**简易轻量**、**B/S 架构**、**0 秒启动**的本地命令行 Mock 服务套件, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,无需布署后端,可能是本地最好用的 Mock 工具之一。
|
|
16
15
|
|
|
@@ -19,6 +18,10 @@
|
|
|
19
18
|
- 支持统计 mock 的文件数量和 mock 请求数量
|
|
20
19
|
- 支持终端打开 mock 文件的所在位置
|
|
21
20
|
|
|
21
|
+
#### Static Server (和 Mock Server 互斥使用)
|
|
22
|
+
|
|
23
|
+
简易的静态资源服务器,可用于开发调试,如需更多使用场景,请选择 [http-server](https://www.npmjs.com/package/http-server)
|
|
24
|
+
|
|
22
25
|
#### Mock Server - 本地 Mock 服务器
|
|
23
26
|
|
|
24
27
|
支持以下常见业务场景:
|
|
@@ -83,40 +86,45 @@ This will install `mock-service-cli` globally so that it may be run from the com
|
|
|
83
86
|
| `-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` | \* |
|
|
84
87
|
| `-O` or `--proxy-options` | Optionally provide http request proxy options list, support accept js file、json file、cli arguments string. Examples: `-O '/api/apaas\|http://192.168.0.105:60700,/api/permission\| http://192.168.0.105:60700'`. Must start web server to use proxy for http request test under browser console or api test tools. | - |
|
|
85
88
|
| `-P` or `--web-port` | Web server port to use. Use `-P 9090` to look for an open port, starting at 9090 | 9090 |
|
|
86
|
-
| `-b` or `--web-baseurl` | Specify public path of the
|
|
89
|
+
| `-b` or `--web-baseurl` | Specify public path of the SPA web server, can be base address of SPA route. | - |
|
|
90
|
+
| `-R` or `--static-server` | Specify directory of the Static Web Server. | - |
|
|
87
91
|
|
|
88
92
|
## Example
|
|
89
93
|
|
|
90
|
-
1. Specify
|
|
94
|
+
1. Specify static resource file directory to startup Static Server.
|
|
95
|
+
|
|
96
|
+
`mock-service-cli -R ./test`
|
|
97
|
+
|
|
98
|
+
2. Specify mock server port.
|
|
91
99
|
|
|
92
100
|
`mock-service-cli -p 8085`
|
|
93
101
|
|
|
94
|
-
|
|
102
|
+
3. Enable cors of mock server, specify the origin and headers of http request.
|
|
95
103
|
|
|
96
104
|
`mock-service-cli -o "http://192.168.0.8:38200,http://10.30.30.3" -H "custom-header"`
|
|
97
105
|
|
|
98
|
-
|
|
106
|
+
4. Specify mock directory to startup Mock Server.
|
|
99
107
|
|
|
100
108
|
`mock-service-cli -d ./mock`
|
|
101
109
|
|
|
102
|
-
|
|
110
|
+
5. Specify mock file to startup Mock Server.
|
|
103
111
|
|
|
104
112
|
`mock-service-cli -f ./mock/test.js`
|
|
105
113
|
|
|
106
|
-
|
|
114
|
+
6. Start socket server for used to save api response data.
|
|
107
115
|
|
|
108
116
|
`mock-service-cli -S`
|
|
109
117
|
|
|
110
|
-
|
|
118
|
+
7. Only Used as api server for test on browser console or api request tools.
|
|
111
119
|
> must specified server directory which can be empty but not includes index.html file, and also need config API proxy.
|
|
112
120
|
|
|
113
121
|
`mock-service-cli -D server -O '/api/apaas|http://192.168.0.105:60700'`
|
|
114
122
|
|
|
115
|
-
|
|
123
|
+
8. Start static web server for SPA, and optionally specify public path and port.
|
|
116
124
|
|
|
117
125
|
`mock-service-cli -D ../react-best-practice/dist [-b '/redbridge/'] [-P 9090]`
|
|
118
126
|
|
|
119
|
-
|
|
127
|
+
9. Enable Proxy base on above item 7. support accept js file、json file、cli arguments string
|
|
120
128
|
|
|
121
129
|
- cli arguments string
|
|
122
130
|
|
package/bin/mock-service-cli
CHANGED
|
@@ -37,6 +37,8 @@ if (argv.h || argv.help) {
|
|
|
37
37
|
' -D --web-dir Enable web server, specify web directory, default [./dist] directory',
|
|
38
38
|
' -b --web-baseurl Specify website public path when enabled web server.',
|
|
39
39
|
'',
|
|
40
|
+
' -R --static-server Specify static resource file directory when enabled static server.',
|
|
41
|
+
'',
|
|
40
42
|
' -h --help Print this list and exit.',
|
|
41
43
|
' -v --version Print the version and exit.'
|
|
42
44
|
].join('\n')
|
|
@@ -81,6 +83,7 @@ injectEnvVariable('CORS_ORIGIN', argv.o || argv['cors-origin']);
|
|
|
81
83
|
injectEnvVariable('CORS_HEADERS', argv.H || argv['cors-headers']);
|
|
82
84
|
|
|
83
85
|
injectEnvVariable('WEB_PUBLIC_PATH', argv.b || argv['web-baseurl']);
|
|
86
|
+
injectEnvVariable('STATIC_DIRECTORY', argv.R || argv['static-server']);
|
|
84
87
|
// injectEnvVariable('PROXY_OPTIONS', argv.O || argv['proxy-options']);
|
|
85
88
|
// 解析Web代理配置项(来源js|config文件或命令行参数字符串)
|
|
86
89
|
const proxyArg = argv.O || argv['proxy-options'];
|
|
@@ -110,6 +113,7 @@ if (proxyArg) {
|
|
|
110
113
|
process.env.PROXY_OPTIONS = JSON.stringify(proxyTable);
|
|
111
114
|
}
|
|
112
115
|
if (argv.D || argv['web-dir']) {
|
|
116
|
+
// 解析SPA应用参数:SPA Web目录
|
|
113
117
|
process.env.WEB_ROOT = resolve(process.cwd(), argv.D || argv['web-dir']);
|
|
114
118
|
if (!existsSync(process.env.WEB_ROOT)) {
|
|
115
119
|
console.error(`web-dir: 目录 ${process.env.WEB_ROOT} 不存在`);
|
|
@@ -160,6 +164,7 @@ const watchMockFiles = function (watchDir) {
|
|
|
160
164
|
);
|
|
161
165
|
});
|
|
162
166
|
};
|
|
167
|
+
|
|
163
168
|
// Node子进程启动MockServer
|
|
164
169
|
const startMockServer = function () {
|
|
165
170
|
spawn(node, [resolve(__dirname, '../lib/mockServer.js')], {
|
|
@@ -167,7 +172,15 @@ const startMockServer = function () {
|
|
|
167
172
|
});
|
|
168
173
|
};
|
|
169
174
|
|
|
175
|
+
// Node子进程启动StaticServer
|
|
176
|
+
const startStaticServer = function () {
|
|
177
|
+
spawn(node, [resolve(__dirname, '../lib/staticServer.js')], {
|
|
178
|
+
stdio: 'inherit'
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
|
|
170
182
|
if (specifiedFile) {
|
|
183
|
+
// Mock服务器:指定文件
|
|
171
184
|
process.env.SPECIFIED_FILE = resolve(process.cwd(), specifiedFile);
|
|
172
185
|
if (isStartSocketServer) {
|
|
173
186
|
startMockServer();
|
|
@@ -175,6 +188,7 @@ if (specifiedFile) {
|
|
|
175
188
|
watchMockFiles(process.env.SPECIFIED_FILE);
|
|
176
189
|
}
|
|
177
190
|
} else if (watchDir) {
|
|
191
|
+
// Mock服务器:指定目录
|
|
178
192
|
process.env.SPECIFIED_DIR = resolve(process.cwd(), watchDir);
|
|
179
193
|
// log.info('SPECIFIED_DIR::', process.env.SPECIFIED_DIR);
|
|
180
194
|
if (isStartSocketServer) {
|
|
@@ -182,7 +196,12 @@ if (specifiedFile) {
|
|
|
182
196
|
} else {
|
|
183
197
|
watchMockFiles(process.env.SPECIFIED_DIR);
|
|
184
198
|
}
|
|
199
|
+
} else if (process.env.STATIC_DIRECTORY) {
|
|
200
|
+
// 静态服务器
|
|
201
|
+
process.env.STATIC_DIRECTORY = resolve(process.cwd(), process.env.STATIC_DIRECTORY);
|
|
202
|
+
startStaticServer();
|
|
185
203
|
} else {
|
|
204
|
+
// Mock服务:使用默认目录
|
|
186
205
|
watchDir = process.env.SPECIFIED_DIR = resolve(process.cwd(), './mock');
|
|
187
206
|
// log.info('SPECIFIED_DIR::', process.env.SPECIFIED_DIR);
|
|
188
207
|
if (isStartSocketServer) {
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Static Server
|
|
3
|
+
* @Date: 2023-12-03 18:02:24
|
|
4
|
+
* @LastEditors: chendq
|
|
5
|
+
* @LastEditTime: 2023-12-03 18:44:33
|
|
6
|
+
* @Author : chendq
|
|
7
|
+
*/
|
|
8
|
+
const express = require('express'), // 引入express
|
|
9
|
+
os = require('os'),
|
|
10
|
+
colors = require('colors/safe'),
|
|
11
|
+
portfinder = require('portfinder');
|
|
12
|
+
const { dateFormat, logger } = require('./utils');
|
|
13
|
+
const ifaces = os.networkInterfaces();
|
|
14
|
+
const log = logger(process.env.SILENT);
|
|
15
|
+
|
|
16
|
+
let done = false;
|
|
17
|
+
if (!process.env.PORT) {
|
|
18
|
+
portfinder.basePort = 8090;
|
|
19
|
+
portfinder.getPort(function (err, port) {
|
|
20
|
+
if (err) {
|
|
21
|
+
throw err;
|
|
22
|
+
}
|
|
23
|
+
process.env.PORT = port;
|
|
24
|
+
done = true;
|
|
25
|
+
});
|
|
26
|
+
} else {
|
|
27
|
+
done = true;
|
|
28
|
+
}
|
|
29
|
+
// 将异步函数转换为同步代码代码执行
|
|
30
|
+
require('deasync').loopWhile(function () {
|
|
31
|
+
return !done;
|
|
32
|
+
});
|
|
33
|
+
const app = express();
|
|
34
|
+
|
|
35
|
+
app.use(express.static(process.env.STATIC_DIRECTORY)); // 将dist目录下所有文件作为静态文件来管理
|
|
36
|
+
|
|
37
|
+
app.listen(Number.parseInt(process.env.PORT, 10), () => {
|
|
38
|
+
console.info(
|
|
39
|
+
[
|
|
40
|
+
colors.yellow(`\nStarting up Static Server, serving `),
|
|
41
|
+
colors.cyan(process.env.STATIC_DIRECTORY),
|
|
42
|
+
colors.yellow(` ${dateFormat('YYYY-mm-dd HH:MM:SS', new Date())}`)
|
|
43
|
+
].join('')
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
console.info(colors.yellow(`\n Static Server available on:\n`));
|
|
47
|
+
console.info(' http://localhost:' + colors.green(process.env.PORT));
|
|
48
|
+
Object.keys(ifaces).forEach(function (dev) {
|
|
49
|
+
ifaces[dev].forEach(function (details) {
|
|
50
|
+
if (details.family === 'IPv4') {
|
|
51
|
+
console.info(' http://' + details.address + ':' + colors.green(process.env.PORT));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (process.platform === 'win32') {
|
|
58
|
+
require('readline')
|
|
59
|
+
.createInterface({
|
|
60
|
+
input: process.stdin,
|
|
61
|
+
output: process.stdout
|
|
62
|
+
})
|
|
63
|
+
.on('SIGINT', function () {
|
|
64
|
+
process.emit('SIGINT');
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
process.on('SIGINT', function () {
|
|
69
|
+
log.info(colors.red('mock-server process stopped.'));
|
|
70
|
+
process.exit();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
process.on('SIGTERM', function () {
|
|
74
|
+
log.info(colors.red('mock-server process stopped.'));
|
|
75
|
+
process.exit();
|
|
76
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mock-service-cli",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "🦅 Local mock server",
|
|
5
5
|
"main": "./lib/mockServer.js",
|
|
6
6
|
"bin": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"prettier": "prettier -c --write \"**/*.{ts,js,jsx,css,less,scss,json}\"",
|
|
17
17
|
"release:prerelease": "standard-version --prerelease",
|
|
18
18
|
"release:prefix": "standard-version --prerelease alpha",
|
|
19
|
-
"release:patch": "standard-version --release-as patch && git push origin master
|
|
19
|
+
"release:patch": "standard-version --release-as patch && git push --follow-tags origin master",
|
|
20
20
|
"release:minor": "standard-version --release-as minor && git push --follow-tags origin master",
|
|
21
21
|
"release:major": "standard-version --release-as major && git push --follow-tags origin master"
|
|
22
22
|
},
|
|
@@ -29,8 +29,10 @@
|
|
|
29
29
|
"node": ">=12"
|
|
30
30
|
},
|
|
31
31
|
"author": "chendq <deqiaochen@gmail.com>",
|
|
32
|
+
"packageManager": "yarn@1.22.19",
|
|
32
33
|
"keywords": [
|
|
33
34
|
"MIT-licensed",
|
|
35
|
+
"Static Server",
|
|
34
36
|
"Mock",
|
|
35
37
|
"MockServer",
|
|
36
38
|
"mock-service-cli",
|
|
@@ -64,9 +66,9 @@
|
|
|
64
66
|
"eslint": "^7.0.0",
|
|
65
67
|
"eslint-config-populist": "^4.2.0",
|
|
66
68
|
"eslint-plugin-mocha": "^9.0.0",
|
|
67
|
-
"husky": "^4.
|
|
69
|
+
"husky": "^4.3.8",
|
|
68
70
|
"lint-staged": "^10.2.2",
|
|
69
|
-
"mockjs": "^
|
|
71
|
+
"mockjs": "^0.1.10",
|
|
70
72
|
"standard-version": "^8.0.0",
|
|
71
73
|
"tap": "^15.0.10"
|
|
72
74
|
},
|
|
@@ -96,5 +98,6 @@
|
|
|
96
98
|
"doc": "docs",
|
|
97
99
|
"lib": "lib",
|
|
98
100
|
"test": "test"
|
|
99
|
-
}
|
|
101
|
+
},
|
|
102
|
+
"optionalDependencies": {}
|
|
100
103
|
}
|