mock-service-cli 3.1.2 → 3.1.4

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 CHANGED
@@ -1,4 +1,4 @@
1
- [![GitHub Workflow Status (master)](https://img.shields.io/github/workflow/status/chandq/mock-service-cli/Node.js%20CI/master?style=flat-square)](https://github.com/chandq/mock-service-cli/actions)
1
+ [![Node.js CI](https://github.com/chandq/mock-service-cli/actions/workflows/node.js.yml/badge.svg)](https://github.com/chandq/mock-service-cli/actions/workflows/node.js.yml)
2
2
  [![Coverage Status](https://coveralls.io/repos/github/chandq/mock-service-cli/badge.svg?branch=master)](https://coveralls.io/github/chandq/mock-service-cli?branch=master)
3
3
  [![mock-service-cli](https://img.shields.io/github/package-json/v/chandq/mock-service-cli?style=flat-square)](https://github.com/chandq/mock-service-cli)
4
4
  [![node](https://img.shields.io/badge/node-v14.0.0-blue)](https://nodejs.org/download/release/v14.0.0/)
@@ -10,7 +10,7 @@
10
10
 
11
11
  ### 简介
12
12
 
13
- 内置 Mock Server、Web Server、Http request proxy 等功能集。
13
+ 内置 Mock Server、Web Server、Http?s request proxy 等功能集。
14
14
 
15
15
  **简易轻量**、**B/S 架构**、**0 秒启动**的本地命令行 Mock 服务套件, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,无需布署后端,可能是本地最好用的 Mock 工具之一。
16
16
 
@@ -32,6 +32,7 @@ if (argv.h || argv.help) {
32
32
  ' -H --cors-headers Optionally provide CORS headers list separated by commas. default *',
33
33
  '',
34
34
  ' -O --proxy-options Optionally provide proxy options list separated by commas.',
35
+ ' -r --rewrite rewrite http or https request url prefix of proxy, default false.',
35
36
  ' -P --web-port Web server port to use. If 0, look for open port. [9090]',
36
37
  ' -D --web-dir Enable web server, specify web directory, default [./dist] directory',
37
38
  ' -b --web-baseurl Specify website public path when enabled web server.',
@@ -51,6 +52,7 @@ var port = argv.p || argv.port,
51
52
  specifiedFile = argv.f || argv.file,
52
53
  version = argv.v || argv.version,
53
54
  isStartSocketServer = argv.S || false,
55
+ rewrite = argv.r || argv.rewrite || false,
54
56
  log = null;
55
57
 
56
58
  if (!argv.s && !argv.silent) {
@@ -74,7 +76,7 @@ if (version) {
74
76
  log.info('v' + require('../package.json').version);
75
77
  process.exit();
76
78
  }
77
-
79
+ injectEnvVariable('PREFIX_REWRITE', rewrite);
78
80
  injectEnvVariable('CORS_ORIGIN', argv.o || argv['cors-origin']);
79
81
  injectEnvVariable('CORS_HEADERS', argv.H || argv['cors-headers']);
80
82
 
package/lib/mockServer.js CHANGED
@@ -27,6 +27,7 @@ let socketServer = null; // Socket server instance
27
27
  const AsyncTaskQueue = require('./asyncTaskQueue');
28
28
  const saveDataAsyncTask = new AsyncTaskQueue();
29
29
 
30
+ const httpsRE = /^https:\/\//;
30
31
  let count = 0,
31
32
  fileCount = 0, // 记录mock api个数,mock file个数
32
33
  mockDirStat = {}, // 缓存mock目录的统计信息
@@ -90,6 +91,10 @@ const composeRouteFromJsFile = function (file) {
90
91
  const fileObject = getFileLatestContent(file);
91
92
 
92
93
  argv.a && log.info(colors.green(`Mockfile ${++fileCount}: `), file, colors.yellow('all API URL is follows: '));
94
+ if (!fileObject) {
95
+ console.error(colors.red(`[warning] "${file}" 文件解析失败,请检查并确保文件中的js语法正确`));
96
+ return;
97
+ }
93
98
  Object.keys(fileObject).forEach(item => {
94
99
  let reqMethod = 'get',
95
100
  reqUrl = item;
@@ -172,10 +177,23 @@ if (process.env.WEB_ROOT) {
172
177
  if (process.env.PROXY_OPTIONS) {
173
178
  try {
174
179
  const options = JSON.parse(process.env.PROXY_OPTIONS);
175
- Object.keys(options).forEach(function (origin) {
176
- proxyTable[origin] = options[origin];
180
+ Object.keys(options).forEach(function (prefix) {
181
+ proxyTable[prefix] = options[prefix];
182
+ const opts = {
183
+ target: options[prefix],
184
+ changeOrigin: true,
185
+ ws: true
186
+ };
187
+ if (httpsRE.test(options[prefix])) {
188
+ // https is require secure=false
189
+ opts.secure = false;
190
+ }
191
+ if (process.env.PREFIX_REWRITE) {
192
+ opts.pathRewrite = path => path.replace(new RegExp(`^${prefix}`), '');
193
+ }
177
194
  // 接口代理
178
- webApp.use(origin, createProxyMiddleware({ target: options[origin], changeOrigin: true, ws: true }));
195
+ // https://github.com/http-party/node-http-proxy#options
196
+ webApp.use(prefix, createProxyMiddleware(opts));
179
197
  }, this);
180
198
  } catch (error) {
181
199
  console.warn(colors.yellow('\nEnable web proxy Failed:', error));
@@ -291,8 +309,8 @@ if (webApp && !process.env.RESTARTED) {
291
309
  });
292
310
  if (!isEmptyObj(proxyTable)) {
293
311
  console.info(colors.yellow(`\n Enable proxy at web server on:`));
294
- Object.keys(proxyTable).forEach(origin => {
295
- console.info(` ${origin} -> ${proxyTable[origin]}`);
312
+ Object.keys(proxyTable).forEach(prefix => {
313
+ console.info(` ${prefix} -> ${proxyTable[prefix]}`);
296
314
  });
297
315
  }
298
316
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "3.1.2",
3
+ "version": "3.1.4",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {
package/CHANGELOG.md DELETED
@@ -1,220 +0,0 @@
1
- # Changelog
2
-
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
-
5
- ### [3.1.2](https://github.com/chandq/mock-service-cli/compare/v3.1.1...v3.1.2) (2022-08-07)
6
-
7
- ### [3.1.1](https://github.com/chandq/mock-service-cli/compare/v3.1.0...v3.1.1) (2022-07-25)
8
-
9
-
10
- ### Bug Fixes
11
-
12
- * node 18下chalk丢失问题 ([fb80d8d](https://github.com/chandq/mock-service-cli/commit/fb80d8d94feee8d3177b69a2c4509a390880bd56))
13
- * update some npm version for security ([f0aa1df](https://github.com/chandq/mock-service-cli/commit/f0aa1dfa51f36bde693b0da12bec2b653b1b6003))
14
-
15
- ## [3.1.0](https://github.com/chandq/mock-service-cli/compare/v3.0.0...v3.1.0) (2022-04-27)
16
-
17
-
18
- ### Features
19
-
20
- * **web-proxy:** proxy配置项支持文件和命令行参数字符串两种 ([ee1ce1f](https://github.com/chandq/mock-service-cli/commit/ee1ce1fbefa5ce9b3c074192d2d9893a4414d346))
21
-
22
- ## [3.0.0](https://github.com/chandq/mock-service-cli/compare/v2.5.1...v3.0.0) (2022-04-27)
23
-
24
-
25
- ### Features
26
-
27
- * add web server and request proxy ([e5be340](https://github.com/chandq/mock-service-cli/commit/e5be340940042eb05e2e5cb9d2fcc6f8316052d5))
28
-
29
- ### [2.5.1](https://github.com/chandq/mock-service-cli/compare/v2.5.0...v2.5.1) (2022-04-25)
30
-
31
- ## [2.5.0](https://github.com/chandq/mock-service-cli/compare/v2.4.0...v2.5.0) (2022-04-25)
32
-
33
-
34
- ### Features
35
-
36
- * **cors:** 支持cors-origin,cors-headers跨域配置 ([c2d2872](https://github.com/chandq/mock-service-cli/commit/c2d28723d047139a5165190b4ba197f46470f72f))
37
- * 缓存mock统计信息 ([1ee87cd](https://github.com/chandq/mock-service-cli/commit/1ee87cd041e68b9f17b64e9e3cb906681cb855fc))
38
-
39
-
40
- ### Bug Fixes
41
-
42
- * 修复无法同时保留相同url不同请求类型的响应数据 ([90326dc](https://github.com/chandq/mock-service-cli/commit/90326dc556760eb81efadd76b031366452c95d2b))
43
-
44
- ### [2.4.3](https://github.com/chandq/mock-service-cli/compare/v2.4.2...v2.4.3) (2022-01-20)
45
-
46
- ### [2.4.2](https://github.com/chandq/mock-service-cli/compare/v2.4.1...v2.4.2) (2022-01-07)
47
-
48
-
49
- ### Features
50
-
51
- * 缓存mock统计信息 ([1ee87cd](https://github.com/chandq/mock-service-cli/commit/1ee87cd041e68b9f17b64e9e3cb906681cb855fc))
52
-
53
- ### [2.4.1](https://github.com/chandq/mock-service-cli/compare/v2.4.0...v2.4.1) (2022-01-06)
54
-
55
-
56
- ### Bug Fixes
57
-
58
- * 修复无法同时保留相同url不同请求类型的响应数据 ([90326dc](https://github.com/chandq/mock-service-cli/commit/90326dc556760eb81efadd76b031366452c95d2b))
59
-
60
- ## [2.4.0](https://github.com/chandq/mock-service-cli/compare/v2.3.2...v2.4.0) (2022-01-05)
61
-
62
-
63
- ### Features
64
-
65
- * 增加异步任务队列,移出不必要模块 ([c7f3608](https://github.com/chandq/mock-service-cli/commit/c7f3608e4d0185434154d6f8d84889f281147022))
66
-
67
- ### [2.3.2](https://github.com/chandq/mock-service-cli/compare/v2.3.1...v2.3.2) (2022-01-02)
68
-
69
-
70
- ### Bug Fixes
71
-
72
- * be sure to write file successfully ([55415e3](https://github.com/chandq/mock-service-cli/commit/55415e387af4d8232db2bede0a51dad1dca474b3))
73
-
74
- ### [2.3.1](https://github.com/chandq/mock-service-cli/compare/v2.2.0...v2.3.1) (2022-01-01)
75
-
76
-
77
- ### Bug Fixes
78
-
79
- * issue of execute unit test case failed ([69f4609](https://github.com/chandq/mock-service-cli/commit/69f46099ed5b26de5d4cca523c56afcbf4db5f2c))
80
-
81
- ## [2.2.0](https://github.com/chandq/mock-service-cli/compare/v2.1.1...v2.2.0) (2021-12-31)
82
-
83
-
84
- ### Features
85
-
86
- * 丰富日志工具函数 ([4dcd69c](https://github.com/chandq/mock-service-cli/commit/4dcd69c6400f65aaf77ea4a2923c9900d5954d09))
87
- * 监听文件变化和写文件互斥执行 ([cfeab8e](https://github.com/chandq/mock-service-cli/commit/cfeab8ede05573974125a9fa91f1db6afa8a898d))
88
- * record operation log ([42b74d8](https://github.com/chandq/mock-service-cli/commit/42b74d8be0e694600c999490d3fe93bfb3620051))
89
-
90
-
91
- ### Bug Fixes
92
-
93
- * issue of write large file failed ([d1c470d](https://github.com/chandq/mock-service-cli/commit/d1c470daf9829916016b73993b114fc5ffe3c985))
94
-
95
- ### [2.1.1](https://github.com/chandq/mock-service-cli/compare/v2.1.0...v2.1.1) (2021-12-30)
96
-
97
-
98
- ### Bug Fixes
99
-
100
- * issue of friendly show error tip ([e9822c2](https://github.com/chandq/mock-service-cli/commit/e9822c2038ad411d91a170af33e7541e266ca8e6))
101
- * issue of empty file content ([3949e00](https://github.com/chandq/mock-service-cli/commit/3949e00a46b4555bc08ec8564221cc230c780dcb))
102
-
103
- ## [2.1.0](https://github.com/chandq/mock-service-cli/compare/v2.0.5...v2.1.0) (2021-12-30)
104
-
105
-
106
- ### Features
107
-
108
- * 根据命令行参数决定是否 打印api统计信息 ([0e838db](https://github.com/chandq/mock-service-cli/commit/0e838db7820fd096163d14eb87abbd9b852b6a0e))
109
- * add arguments description ([097b373](https://github.com/chandq/mock-service-cli/commit/097b3733a1da0b43dae6b50080d270323b92f0d2))
110
-
111
-
112
- ### Bug Fixes
113
-
114
- * issue of comment typo ([85297a5](https://github.com/chandq/mock-service-cli/commit/85297a5895fd7c0446ee8b90a58ebed90a02e9e7))
115
- * issue of maybe write content to file failed ([8ed910b](https://github.com/chandq/mock-service-cli/commit/8ed910b1e9687cae46ad6bdab641db0c305c83fb))
116
- * socket connection show datetime ([c6db3b4](https://github.com/chandq/mock-service-cli/commit/c6db3b47a2370dc1c9aba5495dbb6606c20eb0c9))
117
-
118
- ### [2.0.5](https://github.com/chandq/mock-service-cli/compare/v2.0.4...v2.0.5) (2021-12-28)
119
-
120
-
121
- ### Bug Fixes
122
-
123
- * validate func parameter to avoid exception ([32e4d28](https://github.com/chandq/mock-service-cli/commit/32e4d28114a4907352491e16829d37345fbab9d9))
124
-
125
- ### [2.0.4](https://github.com/chandq/mock-service-cli/compare/v2.0.3...v2.0.4) (2021-12-27)
126
-
127
-
128
- ### Bug Fixes
129
-
130
- * get valid api url for route ([8770751](https://github.com/chandq/mock-service-cli/commit/877075107ea60244a52e345f37b7df855752d5f6))
131
-
132
- ### [2.0.3](https://github.com/chandq/mock-service-cli/compare/v2.0.2...v2.0.3) (2021-12-27)
133
-
134
-
135
- ### Bug Fixes
136
-
137
- * convert windows file path to api url ([1f15b33](https://github.com/chandq/mock-service-cli/commit/1f15b334ab5374bc102e5e11fbfaae994569bca5))
138
-
139
- ### [2.0.2](https://github.com/chandq/mock-service-cli/compare/v2.0.1...v2.0.2) (2021-12-27)
140
-
141
-
142
- ### Bug Fixes
143
-
144
- * issue of convert path ([5276488](https://github.com/chandq/mock-service-cli/commit/527648866f20fd25b5df0dc55b3898d7e7a1ad2c))
145
-
146
- ### [2.0.1](https://github.com/chandq/mock-service-cli/compare/v2.0.0...v2.0.1) (2021-12-27)
147
-
148
-
149
- ### Bug Fixes
150
-
151
- * issue of executed failed under windows ([e160753](https://github.com/chandq/mock-service-cli/commit/e16075307e9fec8efcce464004b0881884f68ab8))
152
-
153
- ## [2.0.0](https://github.com/chandq/mock-service-cli/compare/v1.2.1...v2.0.0) (2021-12-25)
154
-
155
-
156
- ### Features
157
-
158
- * 新增根据api自动生成mock文件、获取mock统计数据的工具函数 ([b22cb6f](https://github.com/chandq/mock-service-cli/commit/b22cb6fa15787a1343ec6d11606d2a59a670171b))
159
- * add c/s communication base on socket.io ([d0ce542](https://github.com/chandq/mock-service-cli/commit/d0ce5422ece5f7c36d6e74f06620b54096e84f73))
160
- * add hasMockApi function ([e14231b](https://github.com/chandq/mock-service-cli/commit/e14231b8385835b59f4e0960b37b4379e4853155))
161
- * add mock-dir-stat mock-file-stat event ([8d8b1fa](https://github.com/chandq/mock-service-cli/commit/8d8b1faf130c84fe35bf205e68ca9634ee8ff767))
162
-
163
-
164
- ### Bug Fixes
165
-
166
- * logical judge issue ([1e40b13](https://github.com/chandq/mock-service-cli/commit/1e40b134cfcbfd908346ed701cfd18c39502153b))
167
- * mock-list.json未及时更新的问题 ([4bb42f5](https://github.com/chandq/mock-service-cli/commit/4bb42f506ca1bab6958554ba6043a721bc296555))
168
-
169
- ### [1.2.1](https://github.com/chandq/mock-service-cli/compare/v1.2.0...v1.2.1) (2021-11-28)
170
-
171
- ## [1.2.0](https://github.com/chandq/mock-service-cli/compare/v1.1.1...v1.2.0) (2021-11-27)
172
-
173
-
174
- ### Features
175
-
176
- * 支持除get请求之外的其他所有常用类型 ([a1bd549](https://github.com/chandq/mock-service-cli/commit/a1bd549e24da958178a3b1244c4b2f472dfcc643))
177
-
178
- ### [1.1.1](https://github.com/chandq/mock-service-cli/compare/v1.1.0...v1.1.1) (2021-10-27)
179
-
180
- ## [1.1.0](https://github.com/chandq/mock-service-cli/compare/v1.0.2...v1.1.0) (2021-10-27)
181
-
182
- ### [1.0.2](https://github.com/chandq/mock-service-cli/compare/v1.0.1...v1.0.2) (2021-10-26)
183
-
184
-
185
- ### Bug Fixes
186
-
187
- * recursively through subdirectories ([d11813c](https://github.com/chandq/mock-service-cli/commit/d11813c48175550c6592e52f2fd1abe1c63f8203))
188
-
189
- ### [1.0.1](https://github.com/chandq/mock-service-cli/compare/v1.0.0...v1.0.1) (2021-10-25)
190
-
191
- ## [1.0.0](https://github.com/chandq/mock-service-cli/compare/v0.2.1...v1.0.0) (2021-10-25)
192
-
193
-
194
- ### Bug Fixes
195
-
196
- * default mock directory issue ([064cd90](https://github.com/chandq/mock-service-cli/commit/064cd903cbc891eaa068ac3c38606e37ec529257))
197
- * make silent argument take effect ([5aa472b](https://github.com/chandq/mock-service-cli/commit/5aa472b34cee4b93683a3cc282bba0b9ad52048e))
198
-
199
- ### [0.2.1](https://github.com/chandq/mock-service-cli/compare/v0.1.1...v0.2.1) (2021-10-22)
200
-
201
-
202
- ### Bug Fixes
203
-
204
- * 纠正devDep和deps的包分类 ([25cf330](https://github.com/chandq/mock-service-cli/commit/25cf3308337220210a2daccaf9d09444eddb1b24))
205
-
206
- ## [0.2.0](https://github.com/chandq/mock-service-cli/compare/v0.1.1...v0.2.0) (2021-10-22)
207
-
208
-
209
- ### Bug Fixes
210
-
211
- * 纠正devDep和deps的包分类 ([25cf330](https://github.com/chandq/mock-service-cli/commit/25cf3308337220210a2daccaf9d09444eddb1b24))
212
-
213
- ### [0.1.1](https://github.com/chandq/mock-service-cli/compare/v0.1.0...v0.1.1) (2021-10-22)
214
-
215
- ## 0.1.0 (2021-10-22)
216
-
217
-
218
- ### Features
219
-
220
- * initial commit ([c8c8a1a](https://github.com/chandq/mock-service-cli/commit/c8c8a1a8268eab16411a2d1d6b0ee90e8de4cb35))