ee-core 2.1.1 → 2.2.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/addon/index.js CHANGED
@@ -1,30 +1,35 @@
1
- const EEAddon = Symbol('Ee#Addon');
1
+ const EE = require('../ee');
2
2
 
3
- /**
4
- * todo 插件模块
5
- */
6
3
  const Addon = {
7
4
 
8
5
  /**
9
- * 设置插件对象
6
+ * 获取 all addon instances
10
7
  */
11
- setAddonObject(name, obj) {
12
- if (!this[EEAddon]) {
13
- this[EEAddon] = new Map();
8
+ all() {
9
+ const { CoreApp } = EE;
10
+ if (!CoreApp) {
11
+ throw new Error('An unknown error or Addons cannot be used by the jobs!');
14
12
  }
15
13
 
16
- if (!this[EEAddon].has(name)) {
17
- this[EEAddon].set(name, obj);
18
- }
14
+ const instances = CoreApp.addon || null;
15
+ if (!instances) {
16
+ throw new Error('Addons not exists or do not call directly at the top!');
17
+ };
18
+ return instances;
19
19
  },
20
20
 
21
21
  /**
22
- * 获取插件对象
22
+ * 获取 addon instance
23
23
  */
24
24
  get(name) {
25
- let addon = this[EEAddon].has(name) ? this[EEAddon].get(name): null;
26
- return addon;
27
- },
28
- }
25
+ const instances = this.all();
26
+ const instance = instances[name] || null;
27
+ if (!instance) {
28
+ throw new Error(`Addon class '${name}' not exists or do not call directly at the top!`);
29
+ };
30
+ return instance;
31
+ },
32
+
33
+ };
29
34
 
30
35
  module.exports = Addon;
package/ee/application.js CHANGED
@@ -1,10 +1,11 @@
1
1
  const Exception = require('../exception');
2
- const {app} = require('electron');
2
+ const { app } = require('electron');
3
3
  const path = require('path');
4
4
  const debug = require('debug')('ee-core:Appliaction');
5
5
  const EeApp = require('./eeApp');
6
6
  const Utils = require('../utils');
7
7
  const Ps = require('../ps');
8
+ const EE = require('./index');
8
9
 
9
10
  class Appliaction extends EeApp {
10
11
  constructor() {
@@ -52,7 +53,7 @@ class Appliaction extends EeApp {
52
53
  }
53
54
 
54
55
  // normalize env
55
- env.NODE_ENV = options.env;
56
+ //env.NODE_ENV = options.env;
56
57
  env.EE_HOME = options.homeDir;
57
58
  env.EE_BASE_DIR = options.baseDir;
58
59
  env.EE_SERVER_ENV = options.env;
@@ -71,6 +72,10 @@ class Appliaction extends EeApp {
71
72
  debug('options:%j', options)
72
73
 
73
74
  super(options);
75
+
76
+ // 设置全局this
77
+ EE.CoreApp = this;
78
+
74
79
  this.initialize();
75
80
  }
76
81
 
package/ee/baseApp.js CHANGED
@@ -22,9 +22,6 @@ class BaseApp extends EeAppCore {
22
22
  this.loader.load();
23
23
 
24
24
  this.HttpClient = HttpClient;
25
-
26
- // TODO 这个不行,要么每次new对象,要么所有地方都用同一个实例,否则会出现数据无法刷新的情况
27
- //this.coreDB = this.getCoreDB();
28
25
  }
29
26
 
30
27
  get [EE_PATH]() {
package/ee/eeApp.js CHANGED
@@ -1,14 +1,15 @@
1
1
  const path = require('path');
2
2
  const fs = require('fs');
3
3
  const assert = require('assert');
4
- const { app } = require('electron');
5
4
  const is = require('is-type-of');
6
5
  const Koa = require('koa');
7
6
  const koaServe = require('koa-static');
8
7
  const https = require('https');
9
8
  const BaseApp = require('./baseApp');
10
9
  const Log = require('../log');
11
- const Electron = require('../electron');
10
+ const CoreElectron = require('../electron');
11
+ const CoreElectronApp = require('../electron/app');
12
+ const CoreElectronWindow = require('../electron/window');
12
13
  const Conf = require('../config');
13
14
  const Ps = require('../ps');
14
15
  const Socket = require('../socket');
@@ -18,14 +19,15 @@ class EeApp extends BaseApp {
18
19
  constructor(options = {}) {
19
20
  super(options);
20
21
 
21
- this.electron = Electron;
22
+ // 兼容旧的api
23
+ this.electron = CoreElectron;
22
24
  this.mainWindow;
23
25
  }
24
26
 
25
27
  /**
26
28
  * 生成端口
27
29
  */
28
- async createPorts () {
30
+ async createPorts() {
29
31
  const mainPort = await GetPort({port: this.config.mainServer.port});
30
32
  process.env.EE_MAIN_PORT = mainPort;
31
33
  this.config.mainServer.port = mainPort;
@@ -49,46 +51,17 @@ class EeApp extends BaseApp {
49
51
  /**
50
52
  * 启动通信模块
51
53
  */
52
- async startSocket () {
54
+ async startSocket() {
53
55
  Socket.startAll(this);
54
56
  }
55
57
 
56
58
  /**
57
59
  * 创建electron应用
58
60
  */
59
- async createElectronApp () {
60
- const self = this;
61
-
62
- const gotTheLock = app.requestSingleInstanceLock();
63
- if (!gotTheLock) {
64
- await this.appQuit();
65
- return;
66
- }
67
-
68
- app.on('second-instance', (event) => {
69
- self.restoreMainWindow();
70
- })
71
-
72
- app.whenReady().then(() => {
73
- self.createWindow();
74
- app.on('activate', () => {
75
- self.restoreMainWindow();
76
- })
77
- })
78
-
79
- app.on('window-all-closed', () => {
80
- if (process.platform !== 'darwin') {
81
- Log.coreLogger.info('[ee-core] [lib/eeApp] window-all-closed quit');
82
- self.appQuit();
83
- }
84
- })
85
-
86
- app.on('before-quit', () => {
87
- Electron.extra.closeWindow = true;
88
- })
89
-
90
- if (this.config.hardGpu.enable == false) {
91
- app.disableHardwareAcceleration();
61
+ async createElectronApp() {
62
+ const newApp = CoreElectronApp.create();
63
+ if (!newApp) {
64
+ return
92
65
  }
93
66
 
94
67
  await this.electronAppReady();
@@ -97,10 +70,10 @@ class EeApp extends BaseApp {
97
70
  /**
98
71
  * 创建应用主窗口
99
72
  */
100
- async createWindow () {
73
+ async createWindow() {
101
74
 
102
75
  // 初始化一个主窗口
103
- this.mainWindow = Electron.getMainWindow();
76
+ this.mainWindow = CoreElectronWindow.getMainWindow();
104
77
 
105
78
  await this.windowReady();
106
79
 
@@ -111,23 +84,10 @@ class EeApp extends BaseApp {
111
84
  this.selectAppType();
112
85
  }
113
86
 
114
- /**
115
- * 还原窗口
116
- */
117
- restoreMainWindow () {
118
- if (this.mainWindow) {
119
- if (this.mainWindow.isMinimized()) {
120
- this.mainWindow.restore();
121
- }
122
- this.mainWindow.show();
123
- this.mainWindow.focus();
124
- }
125
- }
126
-
127
87
  /**
128
88
  * 应用类型 (远程、html、单页应用)
129
89
  */
130
- selectAppType () {
90
+ selectAppType() {
131
91
  let type = '';
132
92
  let url = '';
133
93
 
@@ -167,7 +127,7 @@ class EeApp extends BaseApp {
167
127
  /**
168
128
  * 加载本地前端资源
169
129
  */
170
- loadLocalWeb (mode, staticDir, hostInfo) {
130
+ loadLocalWeb(mode, staticDir, hostInfo) {
171
131
  const self = this;
172
132
  if (!staticDir) {
173
133
  staticDir = path.join(this.config.homeDir, 'public', 'dist')
@@ -210,25 +170,29 @@ class EeApp extends BaseApp {
210
170
  /**
211
171
  * 主页面
212
172
  */
213
- loadMainUrl (type, url) {
173
+ loadMainUrl(type, url) {
214
174
  const mainServer = this.config.mainServer;
215
- Log.coreLogger.info('[ee-core] [main] Env: %s, Type: %s', this.config.env, type);
216
- Log.coreLogger.info('[ee-core] [main] App running at: %s', url);
217
- this.mainWindow.loadURL(url, mainServer.options);
175
+ Log.coreLogger.info('[ee-core] Env: %s, Type: %s', this.config.env, type);
176
+ Log.coreLogger.info('[ee-core] App running at: %s', url);
177
+ this.mainWindow.loadURL(url, mainServer.options)
178
+ .then()
179
+ .catch((err)=>{
180
+ Log.coreLogger.error(`[ee-core] Please check the ${url} are running OR modify config file !`);
181
+ });
218
182
  }
219
183
 
220
184
  /**
221
185
  * electron app退出
222
186
  */
223
- async appQuit () {
187
+ async appQuit() {
224
188
  await this.beforeClose();
225
- app.quit();
189
+ CoreElectronApp.quit();
226
190
  }
227
191
 
228
192
  /**
229
193
  * 加载插件
230
194
  */
231
- async _loderAddons () {
195
+ async _loderAddons() {
232
196
  this.loader.loadAddons();
233
197
 
234
198
  // 注册主窗口Contents id
@@ -243,7 +207,7 @@ class EeApp extends BaseApp {
243
207
  /**
244
208
  * 预加载模块
245
209
  */
246
- async _loderPreload () {
210
+ async _loderPreload() {
247
211
  let filepath = this.loader.resolveModule(path.join(this.config.baseDir, 'preload', 'index'));
248
212
  if (!filepath) return;
249
213
  const fileObj = this.loader.loadFile(filepath);
@@ -254,46 +218,24 @@ class EeApp extends BaseApp {
254
218
  }
255
219
  }
256
220
 
257
- /**
258
- * 序列化参数
259
- */
260
- stringify(obj, ignore) {
261
- const result = {};
262
- Object.keys(obj).forEach(key => {
263
- if (!ignore.includes(key)) {
264
- result[key] = obj[key];
265
- }
266
- });
267
- return JSON.stringify(result);
268
- }
269
-
270
- /**
271
- * 捕获异常(废弃)
272
- */
273
- async catchLog () {
274
- process.on('uncaughtException', function(err) {
275
- Log.coreLogger.error(err);
276
- });
277
- }
278
-
279
221
  /**
280
222
  * electron app已经准备好,主窗口还未创建
281
223
  */
282
- async electronAppReady () {
224
+ async electronAppReady() {
283
225
  // do some things
284
226
  }
285
227
 
286
228
  /**
287
229
  * 主应用窗口已经创建
288
230
  */
289
- async windowReady () {
231
+ async windowReady() {
290
232
  // do some things
291
233
  }
292
234
 
293
235
  /**
294
236
  * app关闭之前
295
237
  */
296
- async beforeClose () {
238
+ async beforeClose() {
297
239
  // do some things
298
240
  }
299
241
  }
package/ee/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  const Utils = require('../utils');
2
2
  const EEApplication = Symbol('Ee#Application');
3
+ const BuiltInApp = Symbol('Ee#BuiltInApp');
3
4
 
5
+ /**
6
+ * EE
7
+ */
4
8
  const EE = {
5
9
 
6
10
  /**
@@ -12,7 +16,7 @@ const EE = {
12
16
  },
13
17
 
14
18
  /**
15
- * 设置app对象
19
+ * 设置实例化app对象
16
20
  */
17
21
  set app(appObject) {
18
22
  if (!this[EEApplication]) {
@@ -21,12 +25,28 @@ const EE = {
21
25
  },
22
26
 
23
27
  /**
24
- * 获取app对象
28
+ * 获取实例化app对象
25
29
  */
26
30
  get app() {
27
31
  return this[EEApplication] || null;
28
32
  },
29
33
 
34
+ /**
35
+ * 设置全局this到CoreApp (eeApp)
36
+ */
37
+ set CoreApp(obj) {
38
+ if (!this[BuiltInApp]) {
39
+ this[BuiltInApp] = obj;
40
+ }
41
+ },
42
+
43
+ /**
44
+ * 获取 CoreApp (eeApp)
45
+ */
46
+ get CoreApp() {
47
+ return this[BuiltInApp] || null;
48
+ },
49
+
30
50
  /**
31
51
  * 是否加密
32
52
  */
@@ -0,0 +1,62 @@
1
+ const { app } = require('electron');
2
+ const Window = require('../window');
3
+ const EE = require('../../ee');
4
+ const Log = require('../../log');
5
+ const Electron = require('../index');
6
+ const UtilsIs = require('../../utils/is');
7
+
8
+ /**
9
+ * CoreElectronApp (框架封装的electron app对象)
10
+ */
11
+ const CoreElectronApp = {
12
+
13
+ /**
14
+ * 创建electron应用
15
+ */
16
+ async create() {
17
+ const { CoreApp } = EE;
18
+
19
+ const gotTheLock = app.requestSingleInstanceLock();
20
+ if (!gotTheLock) {
21
+ app.quit();
22
+ return;
23
+ }
24
+
25
+ app.on('second-instance', (event) => {
26
+ Window.restoreMainWindow();
27
+ })
28
+
29
+ app.whenReady().then(() => {
30
+ CoreApp.createWindow();
31
+ app.on('activate', () => {
32
+ Window.restoreMainWindow();
33
+ })
34
+ })
35
+
36
+ app.on('window-all-closed', () => {
37
+ if (!UtilsIs.macOS) {
38
+ Log.coreLogger.info('[ee-core] [lib/eeApp] window-all-closed quit');
39
+ CoreApp.appQuit();
40
+ }
41
+ })
42
+
43
+ app.on('before-quit', () => {
44
+ Electron.extra.closeWindow = true;
45
+ })
46
+
47
+ if (CoreApp.config.hardGpu.enable == false) {
48
+ app.disableHardwareAcceleration();
49
+ }
50
+
51
+ return app;
52
+ },
53
+
54
+ /**
55
+ * 退出app
56
+ */
57
+ quit() {
58
+ app.quit();
59
+ }
60
+ }
61
+
62
+ module.exports = CoreElectronApp;
package/electron/index.js CHANGED
@@ -1,24 +1,12 @@
1
1
  const Window = require('./window');
2
- const EEMainWindow = Symbol('Ee#electron#mainWindow');
3
2
 
4
3
  const Electron = {
5
4
 
6
- /**
7
- * 获取 mainWindow
8
- */
9
- getMainWindow() {
10
- if (!this[EEMainWindow]) {
11
- this[EEMainWindow] = Window.createWindow();
12
- }
13
-
14
- return this[EEMainWindow] || null;
15
- },
16
-
17
5
  /**
18
6
  * 兼容1.x版本api
19
7
  */
20
8
  get mainWindow() {
21
- return this.getMainWindow();
9
+ return Window.getMainWindow();
22
10
  },
23
11
 
24
12
  /**
@@ -26,7 +14,7 @@ const Electron = {
26
14
  */
27
15
  extra: {
28
16
  closeWindow: false,
29
- },
17
+ },
30
18
  };
31
19
 
32
20
  module.exports = Electron;
@@ -1,13 +1,25 @@
1
- const {app, BrowserWindow, Menu} = require('electron');
2
- const Conf = require('../config');
3
- const Ps = require('../ps');
1
+ const { app, BrowserWindow, Menu } = require('electron');
2
+ const Conf = require('../../config');
3
+ const Ps = require('../../ps');
4
+ const EEMainWindow = Symbol('Ee#electron#mainWindow');
4
5
 
5
6
  const Window = {
6
7
 
8
+ /**
9
+ * 获取 mainWindow
10
+ */
11
+ getMainWindow() {
12
+ if (!this[EEMainWindow]) {
13
+ this[EEMainWindow] = this.createMainWindow();
14
+ }
15
+
16
+ return this[EEMainWindow] || null;
17
+ },
18
+
7
19
  /**
8
20
  * 创建应用主窗口
9
21
  */
10
- createWindow () {
22
+ createMainWindow() {
11
23
 
12
24
  // todo
13
25
  // const protocolName = 'eefile';
@@ -18,7 +30,8 @@ const Window = {
18
30
  // });
19
31
 
20
32
  const config = Conf.all();
21
- let win = new BrowserWindow(config.windowsOption);
33
+ const win = new BrowserWindow(config.windowsOption);
34
+ this[EEMainWindow] = win;
22
35
 
23
36
  // 菜单显示/隐藏
24
37
  if (config.openAppMenu === 'dev-show' && Ps.isProd()) {
@@ -37,9 +50,20 @@ const Window = {
37
50
  }
38
51
 
39
52
  return win;
40
- }
41
-
53
+ },
42
54
 
55
+ /**
56
+ * 还原窗口
57
+ */
58
+ restoreMainWindow() {
59
+ if (this[EEMainWindow]) {
60
+ if (this[EEMainWindow].isMinimized()) {
61
+ this[EEMainWindow].restore();
62
+ }
63
+ this[EEMainWindow].show();
64
+ this[EEMainWindow].focus();
65
+ }
66
+ }
43
67
  };
44
68
 
45
69
  module.exports = Window;
package/index.js CHANGED
@@ -2,6 +2,12 @@
2
2
  * @namespace EeCore
3
3
  */
4
4
 
5
+ /**
6
+ * @member {ElectronEgg} EeCore#Index
7
+ * @since 1.0.0
8
+ */
9
+ const ElectronEgg = require('./main');
10
+
5
11
  /**
6
12
  * @member {app} EeCore#app
7
13
  * @since 1.0.0
@@ -12,13 +18,13 @@ const EE = require('./ee');
12
18
  * @member {Controller} EeCore#Controller
13
19
  * @since 1.0.0
14
20
  */
15
- const Controller = require('./controller');
21
+ const Controller = require('./controller/baseContextClass');
16
22
 
17
23
  /**
18
24
  * @member {Service} EeCore#Service
19
25
  * @since 1.0.0
20
26
  */
21
- const Service = require('./service');
27
+ const Service = require('./services/baseContextClass');
22
28
 
23
29
  /**
24
30
  * @member {Storage}
@@ -39,6 +45,7 @@ const Utils = require('./old-utils');
39
45
  const Socket = require('./socket');
40
46
 
41
47
  module.exports = {
48
+ ElectronEgg,
42
49
  Application: EE.Application,
43
50
  Controller,
44
51
  Service,
package/main/index.js ADDED
@@ -0,0 +1,42 @@
1
+ const { app } = require('electron');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+ const Utils = require('../utils');
5
+ const Ps = require('../ps');
6
+ const EE = require('../ee');
7
+ const UtilsCore = require('../core/lib/utils');
8
+ const Loader = require('../loader');
9
+
10
+ class ElectronEgg {
11
+
12
+ constructor(mode) {
13
+ this.mode = mode || 'framework';
14
+ this._create();
15
+ }
16
+
17
+ /**
18
+ * create ElectronEgg app
19
+ */
20
+ _create() {
21
+ if (!Ps.verifyMode(this.mode)) {
22
+ throw new Error(`The mode supports only (framework | module) !`);
23
+ }
24
+ process.env.EE_MODE = this.mode;
25
+
26
+ let baseDir = path.join(app.getAppPath(), 'electron');
27
+ if (Utils.isEncrypt(app.getAppPath())) {
28
+ baseDir = Ps.getEncryptDir(app.getAppPath());
29
+ }
30
+
31
+ let indexFile = path.join(baseDir, 'index');
32
+ indexFile = Loader.resolveModule(indexFile);
33
+ if (!fs.existsSync(indexFile)) {
34
+ throw new Error(`The ${indexFile} file does not exist`);
35
+ }
36
+
37
+ const EEApp = UtilsCore.loadFile(indexFile);
38
+ EE.app = new EEApp(this.env);
39
+ }
40
+ }
41
+
42
+ module.exports = ElectronEgg;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.1.1",
3
+ "version": "2.2.0-beta.2",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/ps/index.js CHANGED
@@ -1,6 +1,23 @@
1
1
  const path = require('path');
2
2
  const eis = require('../utils/is');
3
3
 
4
+ /**
5
+ * 当前模式 - framework | module
6
+ */
7
+ exports.mode = function() {
8
+ return process.env.EE_MODE;
9
+ }
10
+
11
+ /**
12
+ * 校验模式
13
+ */
14
+ exports.verifyMode = function(mode) {
15
+ if (['framework', 'module'].includes(mode)) {
16
+ return true;
17
+ }
18
+ return false;
19
+ }
20
+
4
21
  /**
5
22
  * 当前进程的所有env
6
23
  */
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * BaseContextClass is a base class that can be extended,
5
+ * it's instantiated in context level,
6
+ * {@link Helper}, {@link Service} is extending it.
7
+ */
8
+ class BaseContextClass {
9
+
10
+ /**
11
+ * @class
12
+ * @param {Context} ctx - context instance
13
+ * @since 1.0.0
14
+ */
15
+ constructor(ctx) {
16
+ /**
17
+ * @member {Application} BaseContextClass#app
18
+ * @since 1.0.0
19
+ */
20
+ this.app = ctx;
21
+ /**
22
+ * @member {Config} BaseContextClass#config
23
+ * @since 1.0.0
24
+ */
25
+ this.config = ctx.config;
26
+ /**
27
+ * @member {Service} BaseContextClass#service
28
+ * @since 1.0.0
29
+ */
30
+ this.service = ctx.service;
31
+ }
32
+ }
33
+
34
+ module.exports = BaseContextClass;
@@ -0,0 +1,35 @@
1
+ const EE = require('../ee');
2
+
3
+ const Services = {
4
+
5
+ /**
6
+ * 获取 all addon instances
7
+ */
8
+ all() {
9
+ const { CoreApp } = EE;
10
+ if (!CoreApp) {
11
+ throw new Error('An unknown error or Services cannot be used by the jobs!');
12
+ }
13
+
14
+ const instances = CoreApp.service || null;
15
+ if (!instances) {
16
+ throw new Error('Services not exists or do not call directly at the top!');
17
+ };
18
+ return instances;
19
+ },
20
+
21
+ /**
22
+ * 获取 addon instance
23
+ */
24
+ get(name) {
25
+ const instances = this.all();
26
+ const instance = instances[name] || null;
27
+ if (!instance) {
28
+ throw new Error(`Service class '${name}' not exists or do not call directly at the top!`);
29
+ };
30
+ return instance;
31
+ },
32
+
33
+ };
34
+
35
+ module.exports = Services;
File without changes