-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (46 loc) · 1.45 KB
/
index.js
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
#! /usr/bin/env node
const express = require('express');
const app = express();
const chalk = require('chalk');
// 转发端口号
const program = require('commander');
const mockClean = require('./mock/swagger/clean');
program
.version(require('./package.json').version, '-v, --version')
.description('swagger mock server')
.option('-c --clean [type]', '清理mock数据')
.option('-p --pull [type]', '生成mock数据')
.option('-s --server [type]', '启动服务')
.parse(process.argv);
const taskQueue = ['clean', 'pull', 'server'];
const taskHandler = {
server: function(conf) {
const fail = conf.fail;
const PORT = conf.port;
app.listen(PORT, () => {
console.log(chalk.green(`server start, listening port ${PORT}`));
const mockHandler = require('./mock/swagger');
// 启用mock拦截器
mockHandler(app, conf);
// 如果没有命中mock拦截器,转发到指定端口
app.use('/', function(req, res, next) {
if (fail && typeof fail === 'function') {
fail(...arguments);
}
});
});
},
pull: function({swaggerOptions}) {
const synchronizeSwagger = require('./mock/swagger/synchronizeSwagger');
return synchronizeSwagger.init(swaggerOptions);
},
clean: mockClean
}
module.exports = async function run(conf) {
for(let i = 0; i < taskQueue.length; i ++) {
const name = taskQueue[i];
if (program[name]) {
await taskHandler[name](conf)
}
}
}