Skip to content

Commit 46c8337

Browse files
authored
Merge pull request #13 from 954053260/master
更新v1.1.0
2 parents 594a65c + 2b361f5 commit 46c8337

File tree

26 files changed

+1537
-518
lines changed

26 files changed

+1537
-518
lines changed

.eslintrc.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
module.exports = {
2-
"env": {
3-
"browser": true,
4-
"commonjs": true,
5-
"es6": true
6-
},
7-
"extends": "eslint:recommended",
8-
"globals": {
9-
"Atomics": "readonly",
10-
"SharedArrayBuffer": "readonly"
11-
},
12-
"parserOptions": {
13-
"ecmaVersion": 2018
14-
},
15-
"rules": {
16-
}
17-
};
2+
env: {
3+
browser: true,
4+
commonjs: true,
5+
es6: true,
6+
},
7+
extends: "eslint:recommended",
8+
globals: {
9+
Atomics: "readonly",
10+
SharedArrayBuffer: "readonly",
11+
},
12+
parserOptions: {
13+
ecmaVersion: 2018,
14+
},
15+
rules: {},
16+
};

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
test/
2+
test/
3+
jest.config.jest.js
4+
.eslintrc.js

core/BaseWindow/main.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ipc = require('../ipc/main');
44
const windowCenter = require('../windowCenter/main');
55
const Events = require('events')
66
const windowEvents = [
7+
'close',
78
'closed',
89
'session-end',
910
'unresponsive',
@@ -35,7 +36,7 @@ const windowEvents = [
3536
'rotate-gesture',
3637
'sheet-begin',
3738
'sheet-end',
38-
'new-window-for-tab'
39+
'new-window-for-tab'
3940
];
4041
// 窗体默认属性
4142
const defaultOption = {};
@@ -82,11 +83,16 @@ class BaseWindow extends Events {
8283
return this.instance;
8384
}
8485

86+
// 判断窗口实例是否存在
87+
isInstanceExist() {
88+
return !!this.instance;
89+
}
90+
8591
getInstance() {
8692
return this.instance;
8793
}
8894

89-
// 发布通知
95+
// 发布通知
9096
publisher(eventName, params = {}) {
9197
ipc._publisher({ header: { fromId: this.name, eventName }, body: params });
9298
this.emit(eventName, params);

core/common/main.js

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,79 @@
11
const path = require('path');
2+
const { app } = require('electron');
23
const { SUGAR_OPTION } = require('../const');
34
global[SUGAR_OPTION] = {};
45
const config = require('../config/main');
56
const store = require('../store/main');
7+
const plugins = require('../plugins/main');
68
const fs = require('fs');
9+
10+
function ready() {
11+
return new Promise((resolve) => {
12+
if (app.isReady()) {
13+
resolve();
14+
} else {
15+
app.on('ready', () => {
16+
resolve();
17+
});
18+
}
19+
})
20+
}
21+
722
module.exports = {
823
/**
924
* 启动sugar
1025
* @param option [object] 启动参数
11-
* @param option.appName [string] 应用名
26+
* @param option.useAppPathConfig [boolean] 是否使用%appData%配置
1227
* @param option.basePath [string] 启动目录
1328
* @param option.configPath [string] 配置目录
1429
* @param option.storePath [string] 进程状态共享目录
1530
* @param option.windowCenterPath [string] 窗口中心目录
1631
* @param option.pluginsPath [string] 插件目录
1732
*/
18-
start(option = {}) {
19-
const { appName, basePath, configPath, storePath, windowCenterPath } = option;
20-
global[SUGAR_OPTION] = option;
21-
if (basePath) {
22-
try {
23-
// 自动初始化config
24-
const _configPath = configPath || path.join(basePath, '/config');
25-
if (fs.existsSync(_configPath)) {
26-
config.setOption({
27-
appName,
28-
configPath: _configPath
29-
});
30-
}
31-
} catch (error) {
32-
console.log(error);
33-
}
34-
35-
try {
36-
// 自动初始化store
37-
const _storePath = storePath || path.join(basePath, '/store');
38-
if (fs.existsSync(_storePath)) {
39-
const storeJson = require(_storePath)
40-
store.createStore(storeJson);
41-
}
42-
} catch (error) {
43-
console.log(error);
33+
async start(option = {}) {
34+
await ready();
35+
const basePath = option.basePath || process.cwd();
36+
global[SUGAR_OPTION] = Object.assign({
37+
basePath: basePath,
38+
useAppPathConfig: false,
39+
configPath: path.join(basePath, '/config'),
40+
storePath: path.join(basePath, '/store'),
41+
windowCenterPath: path.join(basePath, '/windowCenter'),
42+
pluginsPath: path.join(basePath, '/plugins')
43+
}, option);
44+
const { configPath, storePath, windowCenterPath } = global[SUGAR_OPTION];
45+
try {
46+
if (fs.existsSync(configPath)) {
47+
config.getConfig({ configPath: configPath });
4448
}
49+
} catch (error) {
50+
console.log('[sugar-electron] init config fail', error);
51+
}
52+
53+
try {
54+
if (fs.existsSync(storePath)) {
55+
const storeJson = require(storePath)
56+
store.createStore(storeJson);
57+
}
58+
} catch (error) {
59+
console.log('[sugar-electron] init store fail', error);
60+
}
4561

46-
try {
47-
// 自动初始化windowCenter
48-
const _windowCenterPath = windowCenterPath || path.join(basePath, '/windowCenter');
49-
if (fs.existsSync(_windowCenterPath)) {
50-
const items = fs.readdirSync(_windowCenterPath);
51-
const dirs = items.filter(item => fs.statSync(path.join(_windowCenterPath, item)).isDirectory());
52-
dirs.forEach(item => require(path.join(_windowCenterPath, item)));
53-
}
54-
} catch (error) {
55-
console.log(error);
56-
}
57-
}
62+
try {
63+
if (fs.existsSync(windowCenterPath)) {
64+
const items = fs.readdirSync(windowCenterPath);
65+
const dirs = items.filter(item => fs.statSync(path.join(windowCenterPath, item)).isDirectory());
66+
dirs.forEach(item => require(path.join(windowCenterPath, item)));
67+
}
68+
} catch (error) {
69+
console.log('[sugar-electron] init windowCenter fail', error);
70+
}
71+
72+
try {
73+
plugins.installPlugins();
74+
} catch (error) {
75+
console.log('[sugar-electron] init plugins fail', error);
76+
}
5877
}
5978
};
6079

core/config/main.js

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
const path = require('path');
22
const fs = require('fs');
3-
const os = require('os');
4-
// eslint-disable-next-line no-undef
5-
const DEFAULT_PATH = path.join(process.cwd(), 'config');
6-
const APP_DATA = path.join(os.homedir(), '/AppData/Roaming');
7-
const { SUGAR_OPTION, CONFIG_GET } = require('../const');
8-
global[SUGAR_OPTION].configPath = DEFAULT_PATH;
9-
const config = {};
10-
let hasInit = false;
11-
let appName = '';
3+
const { app } = require('electron');
4+
const { CONFIG } = require('../const');
5+
126
// 获取环境变量参数
137
function getProcessArgv() {
148
const argv = {};
@@ -24,15 +18,17 @@ function getProcessArgv() {
2418
return argv;
2519
}
2620
// 从appData获取配置
27-
function getConfigFromAppData(appName) {
28-
const configPath = path.join(APP_DATA, appName, 'config.json');
21+
function getConfigFromAppData(useAppPathConfig) {
2922
let config = {};
30-
try {
31-
// 从appData读取环境变量
32-
const res = fs.readFileSync(configPath);
33-
config = JSON.parse(res.toString());
34-
} catch (error) {
35-
console.error('获取appData配置失败,不影响使用');
23+
if (useAppPathConfig) {
24+
try {
25+
const configPath = path.join(app.getPath('userData'), 'config.json');
26+
// 从appData读取环境变量
27+
const res = fs.readFileSync(configPath);
28+
config = JSON.parse(res.toString());
29+
} catch (error) {
30+
console.log('[sugar-electron] get appData fail,can continue to use');
31+
}
3632
}
3733
return Object.assign({ env: '', config: {} }, config);
3834
}
@@ -41,7 +37,7 @@ function getLocalBaseConfig(configPath) {
4137
try {
4238
return require(path.join(configPath, 'config.base')) || {}
4339
} catch (error) {
44-
console.error(error);
40+
console.error('[sugar-electron]', error);
4541
return {};
4642
}
4743
}
@@ -51,39 +47,23 @@ function getLocalConfig(configPath, env) {
5147
try {
5248
return require(path.join(configPath, configName)) || {};
5349
} catch (error) {
54-
console.error(error);
50+
console.error('[sugar-electron]', error);
5551
return {};
5652
}
5753
}
5854

59-
const getConfig = global[CONFIG_GET] = function () {
60-
if (hasInit === false) {
61-
const appData = getConfigFromAppData(appName);
55+
class Config {
56+
getConfig({ useAppPathConfig, configPath }) {
57+
const appData = getConfigFromAppData(useAppPathConfig);
6258
const argv = getProcessArgv();
6359
const env = appData.env || argv.env || '';
64-
const baseLocalConfig = getLocalBaseConfig(global[SUGAR_OPTION].configPath);
65-
const localConfig = getLocalConfig(global[SUGAR_OPTION].configPath, env);
66-
Object.assign(config, { argv }, baseLocalConfig, localConfig, appData.config);
67-
hasInit = true;
60+
const baseLocalConfig = getLocalBaseConfig(configPath);
61+
const localConfig = getLocalConfig(configPath, env);
62+
Object.assign(this, { argv }, baseLocalConfig, localConfig, appData.config);
63+
return this;
6864
}
69-
return config;
7065
}
7166

72-
/**
73-
* 设置参数
74-
* @param {object} params
75-
* option.appName 应用名
76-
* option.configPath 默认配置目录路径,如果不传则自动加载根目录config目录
77-
* */
78-
const setOption = function(params = {}) {
79-
appName = params.appName || '';
80-
global[SUGAR_OPTION].configPath = params.configPath;
81-
return getConfig();
82-
}
83-
84-
config.getConfig = getConfig;
85-
config.setOption = setOption;
86-
87-
module.exports = config;
88-
89-
67+
const config = new Config();
68+
global[CONFIG] = config;
69+
module.exports = config;

core/config/render.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { CONFIG_GET } = require('../const');
1+
const { CONFIG } = require('../const');
22
const { remote } = require('electron');
33
const util = require('../util');
4-
module.exports = Object.assign({ windowName: util.getThreadId() }, remote.getGlobal(CONFIG_GET)());
4+
module.exports = Object.assign({ windowName: util.getThreadId() }, remote.getGlobal(CONFIG));

core/const.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
exports.CONFIG_GET = 'ELECTRON_SUGAR:CONFIG_GET';
1+
exports.CONFIG = 'ELECTRON_SUGAR:CONFIG';
22
exports.SUGAR_OPTION = 'ELECTRON_SUGAR:SUGAR_OPTION';
33
exports.MAIN_PROCESS_NAME = 'main';

0 commit comments

Comments
 (0)