mock-service-cli 3.3.0 → 3.3.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/README.md CHANGED
@@ -24,6 +24,8 @@
24
24
 
25
25
  #### Mock Server - 本地 Mock 服务器
26
26
 
27
+ > Mock File 仅支持 commonjs 规范的 js、cjs 文件,不支持 ES Module
28
+
27
29
  支持以下常见业务场景:
28
30
 
29
31
  - [x] 无服务端演示项目的数据 Mock
@@ -16,6 +16,7 @@ if (argv.h || argv.help) {
16
16
  [
17
17
  'usage: mock-service-cli [options]',
18
18
  '',
19
+ 'PS: Mock File仅支持commonjs规范的js、cjs文件,不支持ES Module',
19
20
  'options:',
20
21
  ' -p --port Mock server port to use. If 0, look for open port. [8090]',
21
22
  ' -d Specify mock directory, default [./mock] directory,',
@@ -146,7 +147,7 @@ const watchMockFiles = function (watchDir) {
146
147
  nodemon({
147
148
  script: resolve(__dirname, '../lib/mockServer.js'),
148
149
  watch: [watchDir],
149
- ext: 'js,json'
150
+ ext: 'cjs,js,json'
150
151
  });
151
152
 
152
153
  nodemon
package/lib/mockServer.js CHANGED
@@ -92,8 +92,8 @@ const composeRouteFromJsFile = function (file) {
92
92
  const fileObject = getFileLatestContent(file);
93
93
 
94
94
  argv.a && log.info(colors.green(`Mockfile ${++fileCount}: `), file, colors.yellow('all API URL is follows: '));
95
- if (!fileObject) {
96
- console.error(colors.red(`[warning] "${file}" 文件解析失败,请检查并确保文件中的js语法正确`));
95
+ if (fileObject === 'getFileLatestContent_ERROR') {
96
+ // console.error(colors.red(`[warning] "${file}" 文件解析失败,请检查并确保文件中的js语法正确`));
97
97
  return;
98
98
  }
99
99
  Object.keys(fileObject).forEach(item => {
@@ -143,8 +143,8 @@ const parseMockFiles = function (specialDir = '../mock') {
143
143
  argv.a &&
144
144
  log.info(colors.green(`Mockfile ${++fileCount}: `), filePath, colors.yellow('all API URL is follows: '));
145
145
  const fileObject = getFileLatestContent(filePath);
146
- if (!fileObject) {
147
- console.error(colors.red(`[warning] "${filePath}" 文件解析失败,请检查并确保文件中的js语法正确`));
146
+ if (fileObject === 'getFileLatestContent_ERROR') {
147
+ // console.error(colors.red(`[warning] "${filePath}" 文件解析失败,请检查并确保文件中的js语法正确`));
148
148
  continue;
149
149
  }
150
150
  // eslint-disable-next-line no-loop-func
@@ -166,7 +166,7 @@ const parseMockFiles = function (specialDir = '../mock') {
166
166
  }
167
167
 
168
168
  // 非js文件跳过
169
- if (!el.name.endsWith('.js')) {
169
+ if (!el.name.endsWith('.js') && !el.name.endsWith('.cjs')) {
170
170
  continue;
171
171
  }
172
172
  composeRouteFromJsFile(filePath);
@@ -291,6 +291,8 @@ if (process.env.SOCKET_SERVER) {
291
291
  }
292
292
 
293
293
  http.listen(Number.parseInt(process.env.PORT, 10), () => {
294
+ console.info(colors.red('\n Mock File仅支持commonjs规范的js、cjs文件,不支持ES Module'));
295
+
294
296
  console.info(
295
297
  [
296
298
  colors.yellow(`\n${process.env.RESTARTED ? 'Restarting' : 'Starting'} up mock-server, serving `),
@@ -315,6 +317,7 @@ http.listen(Number.parseInt(process.env.PORT, 10), () => {
315
317
  });
316
318
 
317
319
  if (webApp && !process.env.RESTARTED) {
320
+ console.info(colors.red('\n Mock File仅支持commonjs规范的js、cjs文件,不支持ES Module'));
318
321
  webApp.listen(Number.parseInt(process.env.WEB_PORT, 10), () => {
319
322
  console.info(colors.yellow(`\n Web server available on:\n`));
320
323
  console.info(' http://localhost:' + colors.green(process.env.WEB_PORT) + webPublicPath);
package/lib/utils.js CHANGED
@@ -2,10 +2,11 @@
2
2
  * @description: 工具函数库
3
3
  * @Date: 2021-12-25 17:52:48
4
4
  * @LastEditors: chendq
5
- * @LastEditTime: 2022-01-05 20:21:07
5
+ * @LastEditTime: 2024-01-16 22:28:37
6
6
  * @Author: chendq
7
7
  */
8
8
  const fs = require('fs');
9
+ const colors = require('colors/safe');
9
10
  const path = require('path');
10
11
  // const JSONStream = require('JSONStream');
11
12
  /**
@@ -102,9 +103,12 @@ const getFileLatestContent = function (filePath) {
102
103
  delete require.cache[require.resolve(filePath)];
103
104
  return require(path.resolve(process.cwd(), filePath));
104
105
  } catch (error) {
105
- getLogger(path.resolve(process.cwd())).error(filePath, require.cache[require.resolve(filePath)], error);
106
+ // getLogger(path.resolve(process.cwd())).error(filePath, require.cache[require.resolve(filePath)], error);
107
+ console.error(colors.red('[error]:'), error);
108
+ return 'getFileLatestContent_ERROR';
106
109
  }
107
110
  };
111
+
108
112
  /**
109
113
  * @description: 文件路径转成API的url
110
114
  * @param {string} filePath
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {
@@ -98,6 +98,5 @@
98
98
  "doc": "docs",
99
99
  "lib": "lib",
100
100
  "test": "test"
101
- },
102
- "optionalDependencies": {}
101
+ }
103
102
  }