-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
mocker.ts
executable file
·130 lines (114 loc) · 3.91 KB
/
mocker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
import path from 'path';
import { existsSync } from 'fs';
import prepareUrls from 'local-ip-url/prepareUrls';
import detect from 'detect-port';
import color from 'colors-cli/safe';
import express from 'express';
import minimist from 'minimist';
import apiMocker, { MockerOption } from '../';
interface MockerConfig extends MockerOption {
host: string;
port: number;
}
(async () => {
const DEFAULTMOCKERCONFIGPATH = './mocker.config.json';
const DEFAULTMOCKPATH = './mock';
const PKGPATH = path.resolve('package.json');
const argvs = minimist(process.argv.slice(2));
if (argvs.h || argvs.help) {
console.log('\n Usage: mocker <path> [--config] [--help|h]')
console.log('\n Displays help information.')
console.log('\n Options:')
console.log(' --config <path>', 'Simple configuration')
console.log('\n Example:')
console.log(' mocker mock/index.js')
console.log(' mocker mock/index.js --port 7788')
console.log(' mocker mock/index.js --host 0.0.0.0')
console.log(' mocker mock/m1.js test/m2.js')
console.log(' mocker mock/m1.js --config mocker.config.json')
return;
}
const paths = argvs['_'];
let mockPath = paths || DEFAULTMOCKPATH;
let mockConfigPath = DEFAULTMOCKERCONFIGPATH;
let mockerConfig: MockerConfig = {
host: process.env.HOST || '0.0.0.0',
port: Number(process.env.PORT) || 3721
};
if (paths.length === 0) {
console.log(color.red('Error: Need to pass parameters!'));
console.log(`E.g: ${color.yellow('mocker <File path>')}\n`);
return;
}
if (argvs.config) {
mockConfigPath = argvs.config;
}
if (!existsSync(path.resolve(mockConfigPath))) {
mockerConfig.host = process.env.HOST ? process.env.HOST : mockerConfig.host;
mockerConfig.port = await detect(mockerConfig.port);
} else {
mockerConfig = require(path.resolve(mockConfigPath));
}
/**
* Support setting configuration on package.json
* https://github.com/jaywcjlove/mocker-api/issues/144
*/
if (existsSync(PKGPATH)) {
const pkgConf = require(PKGPATH);
if (pkgConf.mocker) {
mockerConfig.port = pkgConf.mocker.port || mockerConfig.port;
mockerConfig.host = pkgConf.mocker.host || mockerConfig.host;
}
}
if (argvs.port) {
mockerConfig.port = argvs.port;
}
if (argvs.host) {
mockerConfig.host = argvs.host;
}
const DEFAULT_PORT = mockerConfig.port;
const DEFAULT_HOST = mockerConfig.host;
const app = express();
app.all('/*', (req, res, next) => {
console.log(`${color.green(req.method)} - ${req.url}`);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length,Authorization,Accept,X-Requested-With');
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
next();
});
delete mockerConfig.port;
delete mockerConfig.host;
apiMocker(app, mockPath, { ...mockerConfig });
app.listen(DEFAULT_PORT, () => {
const localIpUrl = prepareUrls({
protocol: 'http',
host: DEFAULT_HOST,
port: DEFAULT_PORT,
});
console.log(`> Server Listening at Local: ${color.green(localIpUrl.localUrl)}`);
console.log(`> On Your Network: ${color.green(localIpUrl.lanUrl)}\n`);
});
/**
* Event listener for HTTP server "error" event.
*/
app.on('error', (error: any) => {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof DEFAULT_PORT === 'string' ? `Pipe ${DEFAULT_PORT}` : `Port ${DEFAULT_PORT}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(`${bind} requires elevated privileges`); // eslint-disable-line
process.exit(1);
break;
case 'EADDRINUSE':
console.error(`${bind} is already in use`); // eslint-disable-line
process.exit(1);
break;
default:
throw error;
}
});
})();