-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.ts
83 lines (81 loc) · 2.22 KB
/
main.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
import { Swagger } from './model/swagger/swagger';
import { Config, Data } from './model/dataModel';
import { File } from './file/file';
import { Server } from './server/server';
import { log, isNew } from './utils/utils';
import { docPathError } from './model/log';
class Core {
// 缓存参数
options: Config;
// 缓存数据
data: Data;
//数据Promise
dataPromise: Promise<Object>;
constructor(opts: Object) {
// let config = Config;
this.options = Object.assign(
{
type: 'swagger',
docPath: '', //swagger文档访问路径
docPort: 80, //swagger文档端口号
path: '/v2/api-docs', //swagger模式路径
method: 'GET', //文档数据请求方式
realHostName: '', // 项目上线后访问的真实域名
mockPort: 3000, //启动服务的端口号
customProtocol: 'http', //指定协议
headers: {},
jsPath: '', //指定生成的URL文件创建路径
descInclude: [],
override: false
},
opts
);
//确保地址存在
if(this.options.docPath == "") {
log(docPathError);
return;
}
//不同类型跳转到不同数据层
switch (this.options.type) {
case 'swagger':
let swagger = new Swagger(this.options);
this.dataPromise = swagger.getData();
break;
}
this.workflow();
process.on('unhandledRejection', (error) => {
console.error('unhandledRejection', error);
process.exit(1); // To exit with a 'failure' code
});
}
// SMock主流程
workflow() {
this.dataPromise.then((data: any) => {
let process = [];
this.data = data;
//声明变量
var server = new Server(this.options, this.data);
if(!isNew() && !this.options.override) {
}else {
let file = new File(this.options, this.data);
//创建文件
let filePromise = file.createJSONFile();
process.push(filePromise);
//创建URL
let urlPromise = file.createUrlFile();
process.push(urlPromise);
}
//插入接口
let apiPromise = server.addAPI();
process.push(apiPromise);
//全部过程执行完毕,执行启动服务
Promise.all(process).then(() => {
server.startServer(() => {
log(`【${new Date()}】服务器启动!`);
// log(`http://127.0.0.1:${this.options.mockPort}`);
});
});
});
}
}
export { Core };