2017-07-26 22:03:18 +08:00
|
|
|
|
import fs from 'fs-extra';
|
2017-07-11 16:50:17 +08:00
|
|
|
|
import yapi from './yapi.js';
|
|
|
|
|
import commons from './utils/commons';
|
2017-07-26 11:26:28 +08:00
|
|
|
|
import dbModule from './utils/db.js';
|
2017-07-26 22:03:18 +08:00
|
|
|
|
import userModel from './models/user.js';
|
2017-07-11 16:50:17 +08:00
|
|
|
|
|
2017-07-26 11:26:28 +08:00
|
|
|
|
yapi.commons = commons;
|
2017-07-26 22:03:18 +08:00
|
|
|
|
yapi.connect = dbModule.connect();
|
|
|
|
|
|
2017-07-11 16:50:17 +08:00
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
function install() {
|
|
|
|
|
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
|
2017-07-11 16:50:17 +08:00
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
if (exist) {
|
|
|
|
|
yapi.commons.log('runtime/init.lock文件已存在,请确认您是否已安装。如果需要重新安装,请删掉runtime/init.lock文件');
|
|
|
|
|
process.exit(0);
|
2017-07-11 16:50:17 +08:00
|
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
|
2017-07-11 16:50:17 +08:00
|
|
|
|
setupSql();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
function setupSql() {
|
|
|
|
|
let userInst = yapi.getInst(userModel);
|
2017-07-11 16:50:17 +08:00
|
|
|
|
let passsalt = yapi.commons.randStr();
|
|
|
|
|
let result = userInst.save({
|
2017-07-17 11:01:05 +08:00
|
|
|
|
username: yapi.WEBCONFIG.adminAccount.substr(0, yapi.WEBCONFIG.adminAccount.indexOf('@')),
|
2017-07-11 16:50:17 +08:00
|
|
|
|
email: yapi.WEBCONFIG.adminAccount,
|
|
|
|
|
password: yapi.commons.generatePassword('qunar.com', passsalt),
|
|
|
|
|
passsalt: passsalt,
|
|
|
|
|
role: 'admin',
|
|
|
|
|
add_time: yapi.commons.time(),
|
|
|
|
|
up_time: yapi.commons.time()
|
2017-07-26 22:03:18 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result.then(function () {
|
|
|
|
|
fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
|
2017-07-11 16:50:17 +08:00
|
|
|
|
console.log(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 成功`);
|
2017-07-26 22:03:18 +08:00
|
|
|
|
process.exit(0);
|
|
|
|
|
}, function (err) {
|
2017-07-11 16:50:17 +08:00
|
|
|
|
console.log(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`);
|
2017-07-26 22:03:18 +08:00
|
|
|
|
process.exit(0);
|
|
|
|
|
});
|
2017-07-11 16:50:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 22:03:18 +08:00
|
|
|
|
install();
|