{"size": 2197, "ext": "js", "max_stars_repo_path": "core/common/main.js", "max_stars_repo_name": "soymikey/Sugar-Electron", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "core/common/main.js", "max_issues_repo_name": "soymikey/Sugar-Electron", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "core/common/main.js", "max_forks_repo_name": "soymikey/Sugar-Electron", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 36.0163934426, "max_line_length": 117, "alphanum_fraction": 0.5011379153}
const path = require('path');
const { SUGAR_OPTION } = require('../const');
global[SUGAR_OPTION] = {};
const config = require('../config/main');
const store = require('../store/main');
const fs = require('fs');
module.exports = { 
    /**
     * 启动sugar
     * @param option [object] 启动参数
     * @param option.appName [string] 应用名
     * @param option.basePath [string] 启动目录
     * @param option.configPath [string] 配置目录
     * @param option.storePath [string] 进程状态共享目录
     * @param option.windowCenterPath [string] 窗口中心目录
     * @param option.pluginsPath [string] 插件目录
    */
    start(option = {}) {
        const { appName, basePath, configPath, storePath, windowCenterPath } = option;
        global[SUGAR_OPTION] = option;
        if (basePath) {
            try {
                // 自动初始化config
                const _configPath = configPath || path.join(basePath, '/config');
                if (fs.existsSync(_configPath)) {
                    config.setOption({
                        appName,
                        configPath: _configPath
                    });
                }
            } catch (error) {
                console.log(error);        
            }
          
            try {
                // 自动初始化store
                const _storePath = storePath || path.join(basePath, '/store');
                if (fs.existsSync(_storePath)) {
                    const storeJson = require(_storePath)
                    store.createStore(storeJson);
                } 
            } catch (error) {
                console.log(error);    
            }

            try {
                // 自动初始化windowCenter
                const _windowCenterPath = windowCenterPath || path.join(basePath, '/windowCenter');
                if (fs.existsSync(_windowCenterPath)) {
                    const items = fs.readdirSync(_windowCenterPath);
                    const dirs = items.filter(item => fs.statSync(path.join(_windowCenterPath, item)).isDirectory());
                    dirs.forEach(item => require(path.join(_windowCenterPath, item)));  
                }  
            } catch (error) {
                console.log(error);    
            }
        }   
    }
};

