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() {
    try {
        let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));

        if (exist) {
            throw new Error('init.lock文件已存在，请确认您是否已安装。如果需要重新安装，请删掉init.lock文件');
        }
        fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));

        setupSql();
    } catch (err) {
        console.error(err.message);
    }

}

function setupSql() {
    let userInst = yapi.getInst(userModel);
    let passsalt = yapi.commons.randStr();
    let result = userInst.save({
        username: yapi.WEBCONFIG.adminAccount.substr(0, yapi.WEBCONFIG.adminAccount.indexOf('@')),
        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()
    });

    yapi.connect.then(function () {
        let userCol = mongoose.connection.db.collection('user')
        userCol.ensureIndex({
            username: 1
        })
        userCol.ensureIndex({
            email: 1
        }, {
                unique: true
            })

        let projectCol = mongoose.connection.db.collection('project')
        projectCol.ensureIndex({
            uid: 1
        })
        projectCol.ensureIndex({
            name: 1
        })
        projectCol.ensureIndex({
            group_id: 1
        })

        let logCol = mongoose.connection.db.collection('log')
        logCol.ensureIndex({
            uid: 1
        })

        logCol.ensureIndex({
            typeid: 1,
            type: 1
        })

        let interfaceColCol = mongoose.connection.db.collection('interface_col')
        interfaceColCol.ensureIndex({
            uid: 1
        })
        interfaceColCol.ensureIndex({
            project_id: 1
        })

        let interfaceCatCol = mongoose.connection.db.collection('interface_cat')
        interfaceCatCol.ensureIndex({
            uid: 1
        })
        interfaceCatCol.ensureIndex({
            project_id: 1
        })

        let interfaceCaseCol = mongoose.connection.db.collection('interface_case')
        interfaceCaseCol.ensureIndex({
            uid: 1
        })
        interfaceCaseCol.ensureIndex({
            col_id: 1
        })
        interfaceCaseCol.ensureIndex({
            project_id: 1
        })

        let interfaceCol = mongoose.connection.db.collection('interface')
        interfaceCol.ensureIndex({
            uid: 1
        })
        interfaceCol.ensureIndex({
            path: 1,
            method: 1
        })
        interfaceCol.ensureIndex({
            project_id: 1
        })

        let groupCol = mongoose.connection.db.collection('group')
        groupCol.ensureIndex({
            uid: 1
        })
        groupCol.ensureIndex({
            group_name: 1
        })

        let avatarCol = mongoose.connection.db.collection('avatar')
        avatarCol.ensureIndex({
            uid: 1
        })

        let followCol = mongoose.connection.db.collection('follow')
        followCol.ensureIndex({
            uid: 1
        })
        followCol.ensureIndex({
            project_id: 1
        })

        result.then(function () {
            console.log(`初始化管理员账号成功，账号名： "${yapi.WEBCONFIG.adminAccount}",默认密码：qunar.com`); // eslint-disable-line
            process.exit(0);
        }, function (err) {
            throw new Error(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`); // eslint-disable-line
        });

    })

}

install();
@@ -319,22 +319,8 @@
 ) %7B%0A
-    try %7B%0A    
     
@@ -403,28 +403,24 @@
 t.lock'));%0A%0A
-    
     if (exis
@@ -420,28 +420,24 @@
 f (exist) %7B%0A
-    
         thro
@@ -509,175 +509,26 @@
     
-    %7D%0A        fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));%0A%0A        setupSql();%0A    %7D catch (err) %7B%0A        console.error(err.message);%0A    %7D%0A
+%7D%0A%0A    setupSql();
 %0A%7D%0A%0A
@@ -1047,32 +1047,40 @@
 en(function () %7B
+        
 %0A        let use
@@ -3344,32 +3344,40 @@
 d: 1%0A        %7D)%0A
+        
 %0A        result.
@@ -3391,24 +3391,106 @@
 nction () %7B%0A
+            fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));%0A
             
@@ -3514,15 +3514,8 @@
 %E7%90%86%E5%91%98%E8%B4%A6%E5%8F%B7
-%E6%88%90%E5%8A%9F%EF%BC%8C%E8%B4%A6%E5%8F%B7%E5%90%8D%EF%BC%9A
  %22$%7B
@@ -3547,23 +3547,11 @@
 nt%7D%22
-,%E9%BB%98%E8%AE%A4%E5%AF%86%E7%A0%81%EF%BC%9Aqunar.com
+ %E6%88%90%E5%8A%9F
 %60); 
@@ -3642,31 +3642,27 @@
         
-throw new Error
+console.log
 (%60%E5%88%9D%E5%A7%8B%E5%8C%96%E7%AE%A1%E7%90%86%E5%91%98
@@ -3754,18 +3754,113 @@
     
-%7D);%0A%0A    %7D
+    process.exit(0);%0A        %7D);%0A    %0A%0A    %7D.catch(function(err)%7B%0A        console.log(err.message)%0A    %7D)
 )%0A%0A%7D

修改install.js 错误