mock-service-cli 3.1.4 → 3.1.6

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
@@ -102,10 +102,16 @@ This will install `mock-service-cli` globally so that it may be run from the com
102
102
 
103
103
  `mock-service-cli -S`
104
104
 
105
- 6. Start static web server for SPA, and optionally specify public path and port.
106
- `mock-service-cli -D ../react-best-practice/dist [-b '/redbridge/'] [-P 9090]`
105
+ 6. Only Used as api server for test on browser console or api request tools.
106
+ > must specified server directory which can be empty but not includes index.html file, and also need config API proxy.
107
107
 
108
- 7. Enable Proxy base on above item 6. support accept js file、json file、cli arguments string
108
+ `mock-service-cli -D server -O '/api/apaas|http://192.168.0.105:60700'`
109
+
110
+ 7. Start static web server for SPA, and optionally specify public path and port.
111
+
112
+ `mock-service-cli -D ../react-best-practice/dist [-b '/redbridge/'] [-P 9090]`
113
+
114
+ 8. Enable Proxy base on above item 7. support accept js file、json file、cli arguments string
109
115
 
110
116
  - cli arguments string
111
117
 
package/lib/mockServer.js CHANGED
@@ -104,7 +104,13 @@ const composeRouteFromJsFile = function (file) {
104
104
  reqMethod = method.toLowerCase();
105
105
  reqUrl = url;
106
106
  }
107
- argv.a && log.info(colors.yellow(`path ${++count}: `), item, colors.blue(typeof fileObject[item]));
107
+ argv.a &&
108
+ log.info(
109
+ colors.grey(`path ${String(++count).padStart(3, ' ')}: `),
110
+ colors.cyan(reqMethod.padEnd(8, ' ')),
111
+ colors.cyan(reqUrl),
112
+ colors.blue(typeof fileObject[item])
113
+ );
108
114
  log.assert(typeof app[reqMethod] === 'function', colors.red(`${file}, ${reqMethod}`));
109
115
  if (typeof fileObject[item] === 'function') {
110
116
  app[reqMethod](reqUrl, fileObject[item]);
@@ -136,11 +142,20 @@ const parseMockFiles = function (specialDir = '../mock') {
136
142
  argv.a &&
137
143
  log.info(colors.green(`Mockfile ${++fileCount}: `), filePath, colors.yellow('all API URL is follows: '));
138
144
  const fileObject = getFileLatestContent(filePath);
145
+ if (!fileObject) {
146
+ console.error(colors.red(`[warning] "${filePath}" 文件解析失败,请检查并确保文件中的js语法正确`));
147
+ continue;
148
+ }
139
149
  // eslint-disable-next-line no-loop-func
140
150
  Object.keys(fileObject).forEach(method => {
141
151
  const apiUrl = filePath2ApiUrl(decodeURIComponent(el.name.split('.')[0]));
142
152
  argv.a &&
143
- log.info(colors.yellow(`path ${++count}: `), `${method} ${apiUrl}`, colors.blue(typeof fileObject[method]));
153
+ log.info(
154
+ colors.grey(`path ${String(++count).padStart(3, ' ')}: `),
155
+ colors.cyan(method.padEnd(8, ' ')),
156
+ colors.cyan(apiUrl),
157
+ colors.blue(typeof fileObject[method])
158
+ );
144
159
  log.assert(typeof app[method] === 'function', colors.red(`${filePath}, ${method}`));
145
160
  app[method](apiUrl, function (req, res) {
146
161
  res.json(fileObject[method]);
@@ -206,10 +221,12 @@ if (process.env.WEB_ROOT) {
206
221
  webApp.use(webPublicPath, express.static(process.env.WEB_ROOT)); // 将dist目录下所有文件作为静态文件来管理
207
222
  const defaultStaticEntry = `${process.env.WEB_ROOT}/index.html`;
208
223
  if (fs.existsSync(defaultStaticEntry)) {
224
+ // Used as static HTTP server for SPA
209
225
  webApp.get('*', (req, res) => {
210
226
  res.sendFile(defaultStaticEntry);
211
227
  });
212
228
  } else {
229
+ // Used as api server for test on browser console or api request tools
213
230
  webApp.use(webPublicPath, (req, res) => {
214
231
  res.status(200)
215
232
  .send(`Hi, welcome to web server entrance ui, you can test http proxy on browser console or api request tools. Examples as follows:<br />
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "3.1.4",
3
+ "version": "3.1.6",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {