{"size": 2867, "ext": "js", "max_stars_repo_path": "server/utils/check_servers.js", "max_stars_repo_name": "lcolok/SMC", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/utils/check_servers.js", "max_issues_repo_name": "lcolok/SMC", "max_issues_repo_issues_event_min_datetime": "2019-07-27T03:34:38.000Z", "max_issues_repo_issues_event_max_datetime": "2019-09-10T08:37:49.000Z", "max_forks_repo_path": "server/utils/check_servers.js", "max_forks_repo_name": "lcolok/SMC", "max_forks_repo_forks_event_min_datetime": "2019-09-06T10:45:41.000Z", "max_forks_repo_forks_event_max_datetime": "2019-09-06T10:45:41.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 29.8645833333, "max_line_length": 127, "alphanum_fraction": 0.3979769794}

const fs = require("fs");
const path = require('path');
const del = require('del');
const requestJS = require("request");
const configer = require(path.resolve('tools/configer'));

const logPath = './server/log/';
module.exports = () => {

    return new Promise((reslove, reject) => {
        requestJS.get(`https://leancloud.cn/1/clients/self/apps`, {
            headers: {
                'Cookie': process.env.lcCookie,
                'X-XSRF-TOKEN': process.env.xsrfToken,
            }
        }, (err, httpResponse, body) => {
            if (!err) {

                const apps = JSON.parse(body);
                // console.log(apps);
                const appJSON = {};


                function getGroupName(name) {
                    if (name.includes('@')) {
                        return name.split('@').shift();
                    } else {
                        return undefined
                    }
                }

                apps.map(app => {
                    appJSON[app.app_name] = app;
                })



                for (var app of apps) {

                    let groupName, groupMembers = [], currentApp = app.app_name;



                    if (app.app_id == process.env.LEANCLOUD_APP_ID) {


                        groupName = getGroupName(currentApp);
                        if (groupName) {
                            apps.forEach(e => {
                                if (getGroupName(e.app_name) == groupName) {
                                    groupMembers.push(e.app_name);
                                }
                            });
                        }

                        reslove({
                            groupName, currentApp, groupMembers
                        });

                        // //记录一下 app 的其他 data （非调试可关掉）
                        // del.sync(['!' + logPath, logPath + '**/*']);
                        // fs.writeFileSync(path.resolve(logPath + `${app.app_name}.data.json`), JSON.stringify(app, null, 8));


                        if (process.env.LEANCLOUD_APP_ENV === 'development') {//开发环境下才会进行

                            const configPath = path.resolve('config/api.config.js');
                            const apiConfig = require(configPath);

                            groupMembers.forEach(e => {
                                let name = e.split('@').pop();
                                let detail = appJSON[e];
                                apiConfig.groupMembers[name].app_id = detail.app_id;
                                apiConfig.groupMembers[name].app_key = detail.app_key;
                            })

                            configer.save(configPath, apiConfig);
                        }
                    }
                }





            } else {
                reject(err);
            }
        })

    })


}
