mock-service-cli 2.0.4 → 2.0.5

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 CHANGED
@@ -2,6 +2,13 @@
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.0.5](https://github.com/chandq/mock-service-cli/compare/v2.0.4...v2.0.5) (2021-12-28)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * validate func parameter to avoid exception ([32e4d28](https://github.com/chandq/mock-service-cli/commit/32e4d28114a4907352491e16829d37345fbab9d9))
11
+
5
12
  ### [2.0.4](https://github.com/chandq/mock-service-cli/compare/v2.0.3...v2.0.4) (2021-12-27)
6
13
 
7
14
 
@@ -2,16 +2,16 @@
2
2
  * @description: 生成Mock文件、获取mock数据统计
3
3
  * @Date: 2021-12-22 16:57:08
4
4
  * @LastEditors: chendq
5
- * @LastEditTime: 2021-12-27 10:15:01
5
+ * @LastEditTime: 2021-12-28 16:23:43
6
6
  * @Author: chendq
7
7
  */
8
8
  const fsa = require('fs-extra'),
9
9
  fs = require('fs'),
10
10
  path = require('path'),
11
- _ = require('lodash');
12
- // colors = require('colors/safe');
13
- const { getFileLatestContent, SupportMethods, filePath2ApiUrl } = require('./utils');
14
- // const log = logger(process.env.SILENT);
11
+ _ = require('lodash'),
12
+ colors = require('colors/safe');
13
+ const { getFileLatestContent, SupportMethods, filePath2ApiUrl, logger } = require('./utils');
14
+ const log = logger(process.env.SILENT);
15
15
 
16
16
  const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
17
17
  /**
@@ -24,6 +24,13 @@ const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
24
24
  *
25
25
  */
26
26
  const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mockDir }) {
27
+ if (!apiUrl || !method || !resJsonData || !mockDir) {
28
+ log.info(
29
+ colors.red(`参数: apiUrl: ${apiUrl}, method: ${method}, resJsonData: ${resJsonData}, mockDir: ${mockDir}无效`)
30
+ );
31
+ return;
32
+ }
33
+ apiUrl = apiUrl.trim().split('?')[0];
27
34
  const normalizeApiUrl = filePath2ApiUrl(path.normalize(apiUrl));
28
35
  const mockListFilePath = path.resolve(process.cwd(), `${mockDir}/mock-list.json`);
29
36
  const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(path.normalize(apiUrl))}.json`);
@@ -43,7 +50,8 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
43
50
  newMockListContent[normalizeApiUrl].push(method);
44
51
  }
45
52
  if (!oldMockListContent || !_.isEqual(oldMockListContent, newMockListContent)) {
46
- fsa.writeFileSync(mockListFilePath, JSON.stringify(newMockListContent, null, 2));
53
+ Object.keys(newMockListContent).length > 0 &&
54
+ fsa.writeFileSync(mockListFilePath, JSON.stringify(newMockListContent, null, 2));
47
55
  }
48
56
 
49
57
  // mock文件存在
@@ -58,10 +66,12 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
58
66
  return;
59
67
  }
60
68
  mockFileContent[method] = resJsonData ? resJsonData : {};
61
- fsa.writeFileSync(mockFilePath, JSON.stringify(mockFileContent, null, 2));
69
+ Object.keys(mockFileContent).length > 0 &&
70
+ fsa.writeFileSync(mockFilePath, JSON.stringify(mockFileContent, null, 2));
62
71
  } else {
63
72
  // fsa.ensureFileSync(mockFilePath);
64
- fsa.writeFileSync(mockFilePath, JSON.stringify({ [method]: resJsonData ? resJsonData : {} }, null, 2));
73
+ Object.keys(mockFilePath).length > 0 &&
74
+ fsa.writeFileSync(mockFilePath, JSON.stringify({ [method]: resJsonData ? resJsonData : {} }, null, 2));
65
75
  }
66
76
  // console.debug('file::', `${mockDir}/${apiUrl}`, newMockListContent);
67
77
  };
@@ -131,6 +141,9 @@ const deepCollectMockData = function (mockDataMap, specialDir = '../mock') {
131
141
  * @return {map} mock数据统计
132
142
  */
133
143
  const getMockStatFromDir = dirPath => {
144
+ if (!fsa.pathExistsSync(dirPath)) {
145
+ throw new Error(`如参无效,${dirPath} 目录不存在`);
146
+ }
134
147
  const mockDataMap = {};
135
148
  deepCollectMockData(mockDataMap, dirPath);
136
149
  return mockDataMap;
@@ -142,6 +155,9 @@ const getMockStatFromDir = dirPath => {
142
155
  * @return {map} mock数据统计
143
156
  */
144
157
  const getMockStatFromFile = filePath => {
158
+ if (!fsa.pathExistsSync(filePath)) {
159
+ throw new Error(`如参无效,${filePath} 文件不存在`);
160
+ }
145
161
  const mockDataMap = {};
146
162
  collectMockDataFromJsFile(mockDataMap, filePath);
147
163
  return mockDataMap;
@@ -154,7 +170,7 @@ const getMockStatFromFile = filePath => {
154
170
  * @return {boolean}
155
171
  */
156
172
  const hasMockApi = function (mockDataMap, apiUrl, method) {
157
- if (!mockDataMap || !(mockDataMap instanceof Object)) {
173
+ if (!apiUrl || !method || !mockDataMap || !(mockDataMap instanceof Object)) {
158
174
  throw new Error(`如参无效,${mockDataMap} 必须是 mock数据的Object对象`);
159
175
  }
160
176
  return mockDataMap.hasOwnProperty(`${method.toLocaleLowerCase()} ${path.normalize(apiUrl)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "2.0.4",
3
+ "version": "2.0.5",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {