mock-service-cli 1.0.0 → 1.1.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 +9 -6
- package/bin/mock-service-cli +6 -6
- package/lib/mockServer.js +16 -5
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
[](https://github.com/chandq/mock-service-cli/actions)
|
|
2
2
|
[](https://coveralls.io/github/chandq/mock-service-cli?branch=master)
|
|
3
|
-
[](https://github.com/chandq/mock-service-cli)
|
|
4
|
+
[](https://nodejs.org/download/release/v14.0.0/)
|
|
5
|
+
[](https://nodejs.org/download/release/v14.0.0/)
|
|
6
|
+
[](https://github.com/chandq/mock-service-cli/blob/master/LICENSE.md)
|
|
5
7
|
[](https://npmcharts.com/compare/mock-service-cli?minimal=true)
|
|
6
8
|
|
|
7
9
|
# mock-service-cli: 🦅 一个基于 node 和 express 的 本地 Mock Server 命令行工具
|
|
@@ -41,8 +43,8 @@ This will install `mock-service-cli` globally so that it may be run from the com
|
|
|
41
43
|
| Command | Description | Defaults |
|
|
42
44
|
| ------------------- | -------------------------------------------------------------------------------------------------------------- | -------- |
|
|
43
45
|
| `-p` or `--port` | Port to use. Use `-p 0` to look for an open port, starting at 8090. It will also read from `process.env.PORT`. | 8090 |
|
|
44
|
-
| `-d` |
|
|
45
|
-
| `-f` |
|
|
46
|
+
| `-d` | Specify mock directory | ./mock |
|
|
47
|
+
| `-f` | Specify mock file | |
|
|
46
48
|
| `-s` or `--silent` | Suppress log messages from output | |
|
|
47
49
|
| `-h` or `--help` | Print this list and exit. | |
|
|
48
50
|
| `-v` or `--version` | Print the version and exit. | |
|
|
@@ -64,6 +66,7 @@ This will install `mock-service-cli` globally so that it may be run from the com
|
|
|
64
66
|
### mock file template
|
|
65
67
|
|
|
66
68
|
> e.g. `./mock/test.js`
|
|
69
|
+
|
|
67
70
|
```javascript
|
|
68
71
|
module.exports = {
|
|
69
72
|
'/mock/:id/test': { aa: 1, bb: '西西小飞龙' },
|
|
@@ -72,6 +75,6 @@ module.exports = {
|
|
|
72
75
|
},
|
|
73
76
|
'/mock/image/test': (req, res) => {
|
|
74
77
|
res.json({ aa: 1, bb: 'yyds' });
|
|
75
|
-
}
|
|
78
|
+
}
|
|
76
79
|
};
|
|
77
80
|
```
|
package/bin/mock-service-cli
CHANGED
|
@@ -11,13 +11,13 @@ process.title = 'mock-service-cli';
|
|
|
11
11
|
if (argv.h || argv.help) {
|
|
12
12
|
console.log(
|
|
13
13
|
[
|
|
14
|
-
'usage:
|
|
14
|
+
'usage: mock-service-cli [options]',
|
|
15
15
|
'',
|
|
16
16
|
'options:',
|
|
17
17
|
' -p --port Port to use. If 0, look for open port. [8090]',
|
|
18
|
-
' -d
|
|
18
|
+
' -d Specify mock directory, default [./mock] directory,',
|
|
19
19
|
' Also watch js files changes and support hot reload',
|
|
20
|
-
' -f --file Specify the input data source, support javascript
|
|
20
|
+
' -f --file Specify the input data source, support javascript ',
|
|
21
21
|
' The priority is over the directory watches',
|
|
22
22
|
' -s --silent Suppress log messages from output',
|
|
23
23
|
'',
|
|
@@ -71,15 +71,15 @@ const watchMockFiles = function (watchDir) {
|
|
|
71
71
|
|
|
72
72
|
nodemon
|
|
73
73
|
.on('start', function () {
|
|
74
|
-
log.info('nodemon: mockServer has started');
|
|
74
|
+
// log.info('nodemon: mockServer has started');
|
|
75
75
|
})
|
|
76
76
|
.on('quit', function () {
|
|
77
|
-
log.info('nodemon: mockServer has quit');
|
|
77
|
+
// log.info('nodemon: mockServer has quit');
|
|
78
78
|
process.exit();
|
|
79
79
|
})
|
|
80
80
|
.on('restart', function (files) {
|
|
81
81
|
process.env.RESTARTED = true;
|
|
82
|
-
log.info([colors.
|
|
82
|
+
log.info([colors.green('\nnodemon: mockServer restarted due to: '), files, '\n'].join(''));
|
|
83
83
|
});
|
|
84
84
|
};
|
|
85
85
|
|
package/lib/mockServer.js
CHANGED
|
@@ -57,9 +57,9 @@ const composeRoute = function (file) {
|
|
|
57
57
|
delete require.cache[require.resolve(file)];
|
|
58
58
|
const fileObject = require(path.resolve(__dirname, file));
|
|
59
59
|
|
|
60
|
-
log.info(colors.
|
|
60
|
+
log.info(colors.green('Mockfile: '), file);
|
|
61
61
|
Object.keys(fileObject).forEach(item => {
|
|
62
|
-
log.info(colors.
|
|
62
|
+
log.info(colors.yellow('Traversal url: '), item, colors.blue(typeof fileObject[item]));
|
|
63
63
|
if (typeof fileObject[item] === 'function') {
|
|
64
64
|
app.use(item, fileObject[item]);
|
|
65
65
|
} else if (typeof fileObject[item] === 'object') {
|
|
@@ -74,18 +74,24 @@ const composeRoute = function (file) {
|
|
|
74
74
|
* @param {*}
|
|
75
75
|
* @return {*}
|
|
76
76
|
*/
|
|
77
|
-
const parseMockFiles = function (specialDir = '../mock') {
|
|
77
|
+
const parseMockFiles = function (specialDir = '../mock', isRootDir = true) {
|
|
78
78
|
const files = fs.readdirSync(path.resolve(__dirname, specialDir), { withFileTypes: true });
|
|
79
79
|
for (let index = 0; index < files.length; index++) {
|
|
80
80
|
const el = files[index];
|
|
81
81
|
// 过滤目录
|
|
82
|
-
if (!el.isFile()
|
|
82
|
+
if (!el.isFile()) {
|
|
83
|
+
parseMockFiles(`${specialDir}/${el.name}`, false);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!el.name.endsWith('.js')) {
|
|
83
87
|
continue;
|
|
84
88
|
}
|
|
85
89
|
composeRoute(`${specialDir}/${el.name}`);
|
|
86
90
|
}
|
|
87
91
|
|
|
88
|
-
|
|
92
|
+
if (isRootDir) {
|
|
93
|
+
log.info(chalk.hex('#ce5b34bd')(`${files.length} mock file are parsed in total.`));
|
|
94
|
+
}
|
|
89
95
|
};
|
|
90
96
|
app.use(crossDomain());
|
|
91
97
|
|
|
@@ -138,3 +144,8 @@ process.on('SIGTERM', function () {
|
|
|
138
144
|
log.info(colors.red('mock-server process stopped.'));
|
|
139
145
|
process.exit();
|
|
140
146
|
});
|
|
147
|
+
|
|
148
|
+
module.exports = {
|
|
149
|
+
composeRoute,
|
|
150
|
+
parseMockFiles
|
|
151
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mock-service-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "🦅 Local mock server",
|
|
5
5
|
"main": "./lib/mockServer.js",
|
|
6
6
|
"bin": {
|
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"start": "node ./bin/mock-service-cli",
|
|
11
|
-
"test": "tap --reporter=spec test/*.test.js",
|
|
12
|
-
"test
|
|
11
|
+
"test:unit": "tap --reporter=spec test/*.test.js",
|
|
12
|
+
"test": "tap --cov --coverage-report=lcov --reporter=spec test/*.test.js ",
|
|
13
13
|
"coverage": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls -v",
|
|
14
|
+
"test-watch": "tap --reporter=spec --watch test/*.test.js",
|
|
14
15
|
"fix": "eslint --ext .js,.vue,.css --fix",
|
|
15
16
|
"prettier": "prettier -c --write \"**/*.{ts,js,jsx,css,less,scss,json}\"",
|
|
16
17
|
"release:prerelease": "standard-version --prerelease",
|
|
@@ -25,7 +26,7 @@
|
|
|
25
26
|
"doc"
|
|
26
27
|
],
|
|
27
28
|
"engines": {
|
|
28
|
-
"node": ">=
|
|
29
|
+
"node": ">=14"
|
|
29
30
|
},
|
|
30
31
|
"author": "chendq <deqiaochen@gmail.com>",
|
|
31
32
|
"keywords": [
|
|
@@ -44,28 +45,27 @@
|
|
|
44
45
|
"dependencies": {
|
|
45
46
|
"colors": "^1.4.0",
|
|
46
47
|
"deasync": "^0.1.23",
|
|
48
|
+
"express": "^4.17.1",
|
|
47
49
|
"minimist": "^1.2.5",
|
|
48
50
|
"nodemon": "^2.0.14",
|
|
49
|
-
"ora": "^4.0.3",
|
|
50
|
-
"express": "^4.17.1",
|
|
51
51
|
"portfinder": "^1.0.28"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@commitlint/cli": "^8.3.5",
|
|
55
55
|
"@commitlint/config-conventional": "^8.3.4",
|
|
56
56
|
"babel-eslint": "^10.1.0",
|
|
57
|
-
"coveralls": "^3.1.0",
|
|
58
57
|
"eslint": "^7.0.0",
|
|
59
58
|
"eslint-config-populist": "^4.2.0",
|
|
60
59
|
"eslint-plugin-mocha": "^9.0.0",
|
|
61
60
|
"husky": "^4.2.5",
|
|
62
61
|
"lint-staged": "^10.2.2",
|
|
63
62
|
"mockjs": "^1.1.0",
|
|
64
|
-
"nyc": "^15.0.1",
|
|
65
|
-
"prettier": "^2.4.1",
|
|
66
63
|
"standard-version": "^8.0.0",
|
|
67
64
|
"tap": "^15.0.10"
|
|
68
65
|
},
|
|
66
|
+
"tap": {
|
|
67
|
+
"check-coverage": false
|
|
68
|
+
},
|
|
69
69
|
"husky": {
|
|
70
70
|
"hooks": {
|
|
71
71
|
"pre-commit": "lint-staged",
|