mock-service-cli 2.0.0 → 2.0.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/CHANGELOG.md CHANGED
@@ -2,6 +2,34 @@
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.4](https://github.com/chandq/mock-service-cli/compare/v2.0.3...v2.0.4) (2021-12-27)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * get valid api url for route ([8770751](https://github.com/chandq/mock-service-cli/commit/877075107ea60244a52e345f37b7df855752d5f6))
11
+
12
+ ### [2.0.3](https://github.com/chandq/mock-service-cli/compare/v2.0.2...v2.0.3) (2021-12-27)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * convert windows file path to api url ([1f15b33](https://github.com/chandq/mock-service-cli/commit/1f15b334ab5374bc102e5e11fbfaae994569bca5))
18
+
19
+ ### [2.0.2](https://github.com/chandq/mock-service-cli/compare/v2.0.1...v2.0.2) (2021-12-27)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * issue of convert path ([5276488](https://github.com/chandq/mock-service-cli/commit/527648866f20fd25b5df0dc55b3898d7e7a1ad2c))
25
+
26
+ ### [2.0.1](https://github.com/chandq/mock-service-cli/compare/v2.0.0...v2.0.1) (2021-12-27)
27
+
28
+
29
+ ### Bug Fixes
30
+
31
+ * issue of executed failed under windows ([e160753](https://github.com/chandq/mock-service-cli/commit/e16075307e9fec8efcce464004b0881884f68ab8))
32
+
5
33
  ## [2.0.0](https://github.com/chandq/mock-service-cli/compare/v1.2.1...v2.0.0) (2021-12-25)
6
34
 
7
35
 
@@ -70,7 +70,7 @@ const watchMockFiles = function (watchDir) {
70
70
  * ext 检测的文件
71
71
  */
72
72
  nodemon({
73
- script: path.join(__dirname, '../lib/mockServer.js'),
73
+ script: path.resolve(__dirname, '../lib/mockServer.js'),
74
74
  watch: [watchDir],
75
75
  ext: 'js,json'
76
76
  });
@@ -92,14 +92,14 @@ const watchMockFiles = function (watchDir) {
92
92
  };
93
93
 
94
94
  if (specifiedFile) {
95
- process.env.SPECIFIED_FILE = path.join(process.cwd(), specifiedFile);
95
+ process.env.SPECIFIED_FILE = path.resolve(process.cwd(), specifiedFile);
96
96
  watchMockFiles(process.env.SPECIFIED_FILE);
97
97
  } else if (watchDir) {
98
- process.env.SPECIFIED_DIR = path.join(process.cwd(), watchDir);
98
+ process.env.SPECIFIED_DIR = path.resolve(process.cwd(), watchDir);
99
99
  // log.info('SPECIFIED_DIR::', process.env.SPECIFIED_DIR);
100
100
  watchMockFiles(process.env.SPECIFIED_DIR);
101
101
  } else {
102
- watchDir = process.env.SPECIFIED_DIR = path.join(process.cwd(), './mock');
102
+ watchDir = process.env.SPECIFIED_DIR = path.resolve(process.cwd(), './mock');
103
103
  // log.info('SPECIFIED_DIR::', process.env.SPECIFIED_DIR);
104
104
  watchMockFiles(watchDir);
105
105
  }
@@ -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-25 21:00:36
5
+ * @LastEditTime: 2021-12-27 10:15:01
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, logger } = require('./utils');
14
- const log = logger(process.env.SILENT);
11
+ _ = require('lodash');
12
+ // colors = require('colors/safe');
13
+ const { getFileLatestContent, SupportMethods, filePath2ApiUrl } = require('./utils');
14
+ // const log = logger(process.env.SILENT);
15
15
 
16
16
  const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
17
17
  /**
@@ -24,9 +24,9 @@ const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
24
24
  *
25
25
  */
26
26
  const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mockDir }) {
27
- const normalizeUrl = path.normalize(apiUrl);
27
+ const normalizeApiUrl = filePath2ApiUrl(path.normalize(apiUrl));
28
28
  const mockListFilePath = path.resolve(process.cwd(), `${mockDir}/mock-list.json`);
29
- const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(normalizeUrl)}.json`);
29
+ const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(path.normalize(apiUrl))}.json`);
30
30
  let newMockListContent = {},
31
31
  oldMockListContent = null;
32
32
  if (fsa.pathExistsSync(mockListFilePath)) {
@@ -37,10 +37,10 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
37
37
  }
38
38
 
39
39
  // update mock-list.json file
40
- if (!newMockListContent.hasOwnProperty(normalizeUrl)) {
41
- newMockListContent[normalizeUrl] = [method];
42
- } else if (!newMockListContent[normalizeUrl].includes(method)) {
43
- newMockListContent[normalizeUrl].push(method);
40
+ if (!newMockListContent.hasOwnProperty(normalizeApiUrl)) {
41
+ newMockListContent[normalizeApiUrl] = [method];
42
+ } else if (!newMockListContent[normalizeApiUrl].includes(method)) {
43
+ newMockListContent[normalizeApiUrl].push(method);
44
44
  }
45
45
  if (!oldMockListContent || !_.isEqual(oldMockListContent, newMockListContent)) {
46
46
  fsa.writeFileSync(mockListFilePath, JSON.stringify(newMockListContent, null, 2));
@@ -63,7 +63,7 @@ const genMockFiles = function ({ url: apiUrl, method, data: resJsonData, dir: mo
63
63
  // fsa.ensureFileSync(mockFilePath);
64
64
  fsa.writeFileSync(mockFilePath, JSON.stringify({ [method]: resJsonData ? resJsonData : {} }, null, 2));
65
65
  }
66
- // console.debug('file::', `${mockDir}/${normalizeUrl}`, newMockListContent);
66
+ // console.debug('file::', `${mockDir}/${apiUrl}`, newMockListContent);
67
67
  };
68
68
  /**
69
69
  * @description: 从js文件中收集mock数据
@@ -98,7 +98,7 @@ const collectMockDataFromJsFile = function (mockDataMap, filePath) {
98
98
  * @return {*} void
99
99
  */
100
100
  const deepCollectMockData = function (mockDataMap, specialDir = '../mock') {
101
- const files = fs.readdirSync(path.resolve(__dirname, specialDir), { withFileTypes: true });
101
+ const files = fs.readdirSync(path.resolve(process.cwd(), specialDir), { withFileTypes: true });
102
102
  const curFilesSize = files.length;
103
103
  for (let index = 0; index < curFilesSize; index++) {
104
104
  const el = files[index];
package/lib/mockServer.js CHANGED
@@ -7,7 +7,7 @@ const express = require('express'), // 引入express
7
7
  chalk = require('chalk');
8
8
  const ifaces = os.networkInterfaces();
9
9
  const { Server } = require('socket.io');
10
- const { dateFormat, logger, SupportMethods, getFileLatestContent } = require('./utils');
10
+ const { dateFormat, logger, SupportMethods, getFileLatestContent, filePath2ApiUrl } = require('./utils');
11
11
  const { genMockFiles, getMockStatFromDir, getMockStatFromFile } = require('./manageMockFiles');
12
12
 
13
13
  const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
@@ -60,7 +60,7 @@ const crossDomain = () => (req, res, next) => {
60
60
  * @param {*} file
61
61
  * @returns {*}
62
62
  */
63
- const composeRoute = function (file) {
63
+ const composeRouteFromJsFile = function (file) {
64
64
  // 先删除require之前导入的缓存文件,否则获取到的文件内容还是旧内容
65
65
  const fileObject = getFileLatestContent(file);
66
66
 
@@ -75,6 +75,7 @@ const composeRoute = function (file) {
75
75
  reqUrl = url;
76
76
  }
77
77
  log.info(colors.yellow(`path ${++count}: `), item, colors.blue(typeof fileObject[item]));
78
+ log.assert(typeof app[reqMethod] === 'function', colors.red(`${file}, ${reqMethod}`));
78
79
  if (typeof fileObject[item] === 'function') {
79
80
  app[reqMethod](reqUrl, fileObject[item]);
80
81
  } else if (typeof fileObject[item] === 'object') {
@@ -90,7 +91,7 @@ const composeRoute = function (file) {
90
91
  * @return {void}
91
92
  */
92
93
  const parseMockFiles = function (specialDir = '../mock') {
93
- const files = fs.readdirSync(path.resolve(__dirname, specialDir), { withFileTypes: true });
94
+ const files = fs.readdirSync(path.resolve(process.cwd(), specialDir), { withFileTypes: true });
94
95
  const curFilesSize = files.length;
95
96
  for (let index = 0; index < curFilesSize; index++) {
96
97
  const el = files[index];
@@ -104,9 +105,11 @@ const parseMockFiles = function (specialDir = '../mock') {
104
105
  if (el.name.endsWith('.json') && el.name !== 'mock-list.json') {
105
106
  log.info(colors.green(`Mockfile ${++fileCount}: `), filePath, colors.yellow('all API URL is follows: '));
106
107
  const fileObject = getFileLatestContent(filePath);
108
+ // eslint-disable-next-line no-loop-func
107
109
  Object.keys(fileObject).forEach(method => {
108
- const apiUrl = decodeURIComponent(el.name.split('.')[0]);
110
+ const apiUrl = filePath2ApiUrl(decodeURIComponent(el.name.split('.')[0]));
109
111
  log.info(colors.yellow(`path ${++count}: `), `${method} ${apiUrl}`, colors.blue(typeof fileObject[method]));
112
+ log.assert(typeof app[method] === 'function', colors.red(`${filePath}, ${method}`));
110
113
  app[method](apiUrl, function (req, res) {
111
114
  res.json(fileObject[method]);
112
115
  });
@@ -118,12 +121,12 @@ const parseMockFiles = function (specialDir = '../mock') {
118
121
  if (!el.name.endsWith('.js')) {
119
122
  continue;
120
123
  }
121
- composeRoute(filePath);
124
+ composeRouteFromJsFile(filePath);
122
125
  }
123
126
  };
124
127
  app.use(crossDomain());
125
128
  if (process.env.SPECIFIED_FILE) {
126
- composeRoute(process.env.SPECIFIED_FILE);
129
+ composeRouteFromJsFile(process.env.SPECIFIED_FILE);
127
130
  } else {
128
131
  parseMockFiles(process.env.SPECIFIED_DIR);
129
132
  }
@@ -198,6 +201,6 @@ process.on('SIGTERM', function () {
198
201
  });
199
202
 
200
203
  module.exports = {
201
- composeRoute,
204
+ composeRouteFromJsFile,
202
205
  parseMockFiles
203
206
  };
package/lib/utils.js CHANGED
@@ -1,3 +1,10 @@
1
+ /*
2
+ * @description:
3
+ * @Date: 2021-12-25 17:52:48
4
+ * @LastEditors: chendq
5
+ * @LastEditTime: 2021-12-27 13:12:16
6
+ * @Author: chendq
7
+ */
1
8
  /**
2
9
  * @description: 输出和错误输出写入不同文件
3
10
  * @param {*}
@@ -78,5 +85,21 @@ const getFileLatestContent = function (filePath) {
78
85
  delete require.cache[require.resolve(filePath)];
79
86
  return require(path.resolve(process.cwd(), filePath));
80
87
  };
88
+ /**
89
+ * @description: 文件路径转成API的url
90
+ * @param {string} filePath
91
+ * @return {string} apiUrl
92
+ */
93
+ const filePath2ApiUrl = function (filePath) {
94
+ return filePath.split(path.sep).join('/');
95
+ };
81
96
 
82
- module.exports = { getLogger, dateFormat, logger, SupportMethods, isValidMethod, getFileLatestContent };
97
+ module.exports = {
98
+ getLogger,
99
+ dateFormat,
100
+ logger,
101
+ SupportMethods,
102
+ isValidMethod,
103
+ getFileLatestContent,
104
+ filePath2ApiUrl
105
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "2.0.0",
3
+ "version": "2.0.4",
4
4
  "description": "🦅 Local mock server",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {