ee-core 1.0.0 → 1.1.0

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.
Files changed (2) hide show
  1. package/lib/eeApp.js +69 -35
  2. package/package.json +5 -4
package/lib/eeApp.js CHANGED
@@ -3,6 +3,8 @@ const getPort = require('get-port');
3
3
  const {app, BrowserWindow, BrowserView, Menu} = require('electron');
4
4
  const BaseApp = require('./baseApp');
5
5
  const is = require('is-type-of');
6
+ const Koa = require('koa');
7
+ const koaServe = require('koa-static');
6
8
 
7
9
  class EeApp extends BaseApp {
8
10
  constructor(options = {}) {
@@ -12,13 +14,6 @@ class EeApp extends BaseApp {
12
14
  mainWindow: null,
13
15
  tray: null
14
16
  };
15
-
16
- //this._mainWindow = null;
17
- //this.electron.mainWindow = null;
18
-
19
- //this.electron.tray = null;
20
- // global.APP_TRAY = null;
21
- //this.preferences = {};
22
17
  }
23
18
 
24
19
  /**
@@ -85,11 +80,6 @@ class EeApp extends BaseApp {
85
80
  async createWindow () {
86
81
  const winOptions = this.config.windowsOption;
87
82
  this.electron.mainWindow = new BrowserWindow(winOptions);
88
-
89
- // DevTools
90
- if (!app.isPackaged && this.config.openDevTools) {
91
- this.electron.mainWindow.webContents.openDevTools();
92
- }
93
83
 
94
84
  // 隐藏菜单
95
85
  if (this.config.openAppMenu) {
@@ -103,8 +93,15 @@ class EeApp extends BaseApp {
103
93
  await this.windowReady();
104
94
 
105
95
  await this.loderPreload();
96
+
97
+ this.loadLocalWeb();
106
98
 
107
- await this.startEggServer(this.config.egg);
99
+ await this.startEggServer();
100
+
101
+ // DevTools
102
+ if (!app.isPackaged && this.config.openDevTools) {
103
+ this.electron.mainWindow.webContents.openDevTools();
104
+ }
108
105
  }
109
106
 
110
107
  /**
@@ -113,10 +110,36 @@ class EeApp extends BaseApp {
113
110
  loadRemoreWeb () {
114
111
  const remoteConfig = this.config.remoteUrl;
115
112
  if (remoteConfig.enable) {
116
- this.loadMainUrl(remoteConfig.url);
113
+ this.loadMainUrl('remote_web', remoteConfig.url);
117
114
  }
118
115
  }
119
116
 
117
+ /**
118
+ * 加载本地前端资源
119
+ */
120
+ loadLocalWeb () {
121
+ // 如果加载了远程,则不能加载本地的
122
+ const remoteConfig = this.config.remoteUrl;
123
+ if (remoteConfig.enable) {
124
+ return;
125
+ }
126
+
127
+ // 如果egg服务开启,则不能加载本地的
128
+ if (this.config.egg.enable == true) {
129
+ return;
130
+ }
131
+ const self = this;
132
+ const staticDir = path.join(this.config.homeDir, 'public', 'dist');
133
+
134
+ const koaApp = new Koa();
135
+ koaApp.use(koaServe(staticDir))
136
+ const port = process.env.EE_EGG_PORT;
137
+ koaApp.listen(port, () => {
138
+ const url = 'http://127.0.0.1:' + port;
139
+ self.loadMainUrl('local_web', url);
140
+ });
141
+ }
142
+
120
143
  /**
121
144
  * 加载已经实现的功能
122
145
  */
@@ -128,38 +151,29 @@ class EeApp extends BaseApp {
128
151
  /**
129
152
  * 创建egg服务
130
153
  */
131
- async startEggServer (options) {
154
+ async startEggServer () {
132
155
  // egg服务是否开启
133
156
  if (this.config.egg.enable == false) {
134
157
  return;
135
158
  }
136
-
137
- this.coreLogger.info('[ee-core:EeApp] [startServer] options', options);
159
+ let eggConfig = this.config.egg;
138
160
  const protocol = 'http://';
139
161
  let startRes = null;
140
- let url = null;
141
-
142
- if (this.config.env === 'prod') {
143
- url = protocol + options.hostname + ':' + options.port
144
- } else {
145
- const developmentModeConfig = this.config.developmentMode;
146
- const selectMode = developmentModeConfig.default;
147
- const modeInfo = developmentModeConfig.mode[selectMode];
148
- url = protocol + modeInfo.hostname + ':' + modeInfo.port;
149
- }
150
- this.coreLogger.info('[ee-core:EeApp] frontend server :', url)
151
- startRes = await this.startEgg(options).then((res) => res, (err) => err);
152
- this.coreLogger.info('[ee-core:EeApp] [startServer] egg startRes:', startRes)
162
+ let url = protocol + eggConfig.hostname + ':' + eggConfig.port;
163
+
164
+ startRes = await this.startEgg(eggConfig).then((res) => res, (err) => err);
165
+ this.coreLogger.info('[ee-core:EeApp] [startEggServer] startRes:', startRes)
153
166
  if (startRes === 'success') {
154
- // 是否加载远程网址
167
+ // 如果加载远程网址,则不能重复load
155
168
  const remoteConfig = this.config.remoteUrl;
156
169
  if (remoteConfig.enable) {
157
170
  return;
158
171
  }
159
- this.loadMainUrl(url);
172
+ this.loadMainUrl('egg', url);
173
+ } else {
174
+ // 失败后重启
175
+ app.relaunch();
160
176
  }
161
-
162
- app.relaunch();
163
177
  }
164
178
 
165
179
  /**
@@ -170,6 +184,11 @@ class EeApp extends BaseApp {
170
184
  if (remoteConfig.enable) {
171
185
  return;
172
186
  }
187
+
188
+ if (!this.config.loadingPage) {
189
+ return;
190
+ }
191
+
173
192
  const self = this;
174
193
  const loadingBrowserView = new BrowserView();
175
194
  this.electron.mainWindow.setBrowserView(loadingBrowserView);
@@ -193,7 +212,21 @@ class EeApp extends BaseApp {
193
212
  /**
194
213
  * 加载主页面
195
214
  */
196
- loadMainUrl (url) {
215
+ loadMainUrl (type, url) {
216
+
217
+ // 环境模式 (远程模式,不加载dev)
218
+ const remoteConfig = this.config.remoteUrl;
219
+ if (this.config.env !== 'prod' && remoteConfig.enable == false) {
220
+ const protocol = 'http://';
221
+ const developmentModeConfig = this.config.developmentMode;
222
+ const selectMode = developmentModeConfig.default;
223
+ const modeInfo = developmentModeConfig.mode[selectMode];
224
+ url = protocol + modeInfo.hostname + ':' + modeInfo.port;
225
+
226
+ this.coreLogger.info('[ee-core:EeApp] frontend server :', url)
227
+ }
228
+
229
+ this.logger.info('main page is env: %s, type: %s, url: %s', this.config.env, type, url);
197
230
  this.electron.mainWindow.loadURL(url);
198
231
  }
199
232
 
@@ -222,6 +255,7 @@ class EeApp extends BaseApp {
222
255
  const ignoreKeys = [ '_', '$0', 'env', 'daemon', 'stdout', 'stderr', 'timeout', 'ignore-stderr', 'node' ];
223
256
  const clusterOptions = this.stringify(argv, ignoreKeys);
224
257
  const options = JSON.parse(clusterOptions);
258
+ this.coreLogger.info('[ee-core:EeApp] [startEgg] options', options);
225
259
 
226
260
  return new Promise((resolve, reject) => {
227
261
  startCluster(options, function(){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "electron-egg core",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,23 +17,24 @@
17
17
  "egg-logger": "^2.7.1",
18
18
  "electron-is": "^3.0.0",
19
19
  "electron-updater": "^4.6.1",
20
+ "fs-extra": "^10.0.0",
20
21
  "get-port": "^5.1.1",
21
22
  "globby": "^10.0.0",
22
23
  "humanize-ms": "^1.2.1",
23
24
  "is-type-of": "^1.2.1",
24
25
  "koa": "^2.13.4",
25
26
  "koa-convert": "^2.0.0",
27
+ "koa-static": "^5.0.0",
26
28
  "lodash": "^4.17.21",
27
29
  "lowdb": "^1.0.0",
28
30
  "socket.io": "^4.4.1",
29
31
  "socket.io-client": "^4.4.1",
32
+ "uglify-js": "^3.14.5",
30
33
  "unzip-crx-3": "^0.2.0",
31
34
  "urllib": "^2.38.0",
32
35
  "utility": "^1.17.0"
33
36
  },
34
37
  "devDependencies": {
35
- "debug": "^4.3.3",
36
- "fs-extra": "^10.0.0",
37
- "uglify-js": "^3.14.5"
38
+ "debug": "^4.3.3"
38
39
  }
39
40
  }