-
Notifications
You must be signed in to change notification settings - Fork 41
/
install.js
executable file
·155 lines (136 loc) · 3.93 KB
/
install.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const fs = require('fs-extra')
const yapi = require('./yapi.js')
const commons = require('./utils/commons')
const dbModule = require('./utils/db.js')
const userModel = require('./models/user.js')
const mongoose = require('mongoose')
yapi.commons = commons
yapi.connect = dbModule.connect()
function install() {
const exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'))
if (exist) {
throw new Error(
'init.lock文件已存在,请确认您是否已安装。如果需要重新安装,请删掉init.lock文件',
)
}
setupSql()
}
function setupSql() {
const userInst = yapi.getInst(userModel)
const passsalt = yapi.commons.randStr()
const result = userInst.save({
username: yapi.WEBCONFIG.adminAccount.substr(0, yapi.WEBCONFIG.adminAccount.indexOf('@')),
email: yapi.WEBCONFIG.adminAccount,
password: yapi.commons.generatePassword(yapi.WEBCONFIG.adminPassword || 'ymfe.org', passsalt),
passsalt: passsalt,
role: 'admin',
add_time: yapi.commons.time(),
up_time: yapi.commons.time(),
})
yapi.connect
.then(function () {
const userCol = mongoose.connection.db.collection('user')
userCol.createIndex({
username: 1,
})
userCol.createIndex(
{
email: 1,
},
{
unique: true,
},
)
const projectCol = mongoose.connection.db.collection('project')
projectCol.createIndex({
uid: 1,
})
projectCol.createIndex({
name: 1,
})
projectCol.createIndex({
group_id: 1,
})
const logCol = mongoose.connection.db.collection('log')
logCol.createIndex({
uid: 1,
})
logCol.createIndex({
typeid: 1,
type: 1,
})
const interfaceColCol = mongoose.connection.db.collection('interface_col')
interfaceColCol.createIndex({
uid: 1,
})
interfaceColCol.createIndex({
project_id: 1,
})
const interfaceCatCol = mongoose.connection.db.collection('interface_cat')
interfaceCatCol.createIndex({
uid: 1,
})
interfaceCatCol.createIndex({
project_id: 1,
})
const interfaceCaseCol = mongoose.connection.db.collection('interface_case')
interfaceCaseCol.createIndex({
uid: 1,
})
interfaceCaseCol.createIndex({
col_id: 1,
})
interfaceCaseCol.createIndex({
project_id: 1,
})
const interfaceCol = mongoose.connection.db.collection('interface')
interfaceCol.createIndex({
uid: 1,
})
interfaceCol.createIndex({
path: 1,
method: 1,
})
interfaceCol.createIndex({
project_id: 1,
})
const groupCol = mongoose.connection.db.collection('group')
groupCol.createIndex({
uid: 1,
})
groupCol.createIndex({
group_name: 1,
})
const avatarCol = mongoose.connection.db.collection('avatar')
avatarCol.createIndex({
uid: 1,
})
const tokenCol = mongoose.connection.db.collection('token')
tokenCol.createIndex({
project_id: 1,
})
const followCol = mongoose.connection.db.collection('follow')
followCol.createIndex({
uid: 1,
})
followCol.createIndex({
project_id: 1,
})
result.then(
function () {
fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'))
console.log(
`初始化管理员账号成功,账号名:"${yapi.WEBCONFIG.adminAccount}",密码:"${yapi.WEBCONFIG.adminPassword || 'ymfe.org'}"`,
); // eslint-disable-line
process.exit(0)
},
function (err) {
throw new Error(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`); // eslint-disable-line
},
)
})
.catch(function (err) {
throw new Error(err.message)
})
}
install()