diff --git a/electron/addon/autoUpdater/index.js b/electron/addon/autoUpdater/index.js index b445f66..b435a24 100644 --- a/electron/addon/autoUpdater/index.js +++ b/electron/addon/autoUpdater/index.js @@ -2,6 +2,9 @@ const { app } = require('electron'); const { autoUpdater } = require("electron-updater"); const is = require('electron-is'); const Log = require('ee-core/log'); +const Conf = require('ee-core/config'); +const Electron = require('ee-core/electron'); +const EE = require('ee-core/ee'); /** * 自动升级插件 @@ -11,19 +14,18 @@ class AutoUpdaterAddon { constructor(app) { this.app = app; - this.cfg = app.config.addons.autoUpdater; - this.mainWindow = app.electron.mainWindow; } /** * 创建 */ create () { - this.app.console.info('[addon:autoUpdater] load'); + Log.info('[addon:autoUpdater] load'); - if ((is.windows() && this.cfg.windows) - || (is.macOS() && this.cfg.macOS) - || (is.linux() && this.cfg.linux)) + const cfg = Conf.getValue('addons.autoUpdater'); + if ((is.windows() && cfg.windows) + || (is.macOS() && cfg.macOS) + || (is.linux() && cfg.linux)) { // continue } else { @@ -31,7 +33,7 @@ class AutoUpdaterAddon { } // 是否检查更新 - if (this.cfg.force) { + if (cfg.force) { this.checkUpdate(); } @@ -43,22 +45,21 @@ class AutoUpdaterAddon { downloaded: 4, } - const updateConfig = this.cfg; const version = app.getVersion(); Log.info('[addon:autoUpdater] current version: ', version); // 设置下载服务器地址 - let server = updateConfig.options.url; + let server = cfg.options.url; let lastChar = server.substring(server.length - 1); server = lastChar === '/' ? server : server + "/"; //Log.info('[addon:autoUpdater] server: ', server); - updateConfig.options.url = server; + cfg.options.url = server; // 是否后台自动下载 - autoUpdater.autoDownload = updateConfig.force ? true : false; + autoUpdater.autoDownload = cfg.force ? true : false; try { - autoUpdater.setFeedURL(updateConfig.options); + autoUpdater.setFeedURL(cfg.options); } catch (error) { Log.error('[addon:autoUpdater] setFeedURL error : ', error); } @@ -105,7 +106,7 @@ class AutoUpdaterAddon { info.desc = '下载完成'; this.sendStatusToWindow(info); // quit and update - this.app.appQuit(); + EE.app.appQuit(); autoUpdater.quitAndInstall(); }); } @@ -130,7 +131,7 @@ class AutoUpdaterAddon { sendStatusToWindow(content = {}) { const textJson = JSON.stringify(content); const channel = 'app.updater'; - this.mainWindow.webContents.send(channel, textJson); + Electron.mainWindow.webContents.send(channel, textJson); } /** diff --git a/electron/addon/awaken/index.js b/electron/addon/awaken/index.js index 742f279..3a05f5b 100644 --- a/electron/addon/awaken/index.js +++ b/electron/addon/awaken/index.js @@ -1,5 +1,6 @@ -const electronApp = require('electron').app; +const { app: electronApp } = require('electron'); const Log = require('ee-core/log'); +const Conf = require('ee-core/config'); /** * 唤醒插件 @@ -9,7 +10,6 @@ class AwakenAddon { constructor(app) { this.app = app; - this.cfg = app.config.addons.awaken; this.protocol = ''; } @@ -17,9 +17,10 @@ class AwakenAddon { * 创建 */ create () { - this.app.console.info('[addon:awaken] load'); + Log.info('[addon:awaken] load'); - this.protocol = this.cfg.protocol; + const cfg = Conf.getValue('addons.awaken'); + this.protocol = cfg.protocol; electronApp.setAsDefaultProtocolClient(this.protocol); diff --git a/electron/addon/chromeExtension/index.js b/electron/addon/chromeExtension/index.js index 911421b..3f88dc6 100644 --- a/electron/addon/chromeExtension/index.js +++ b/electron/addon/chromeExtension/index.js @@ -2,6 +2,7 @@ const { app, session } = require('electron'); const _ = require('lodash'); const fs = require('fs'); const path = require('path'); +const Log = require('ee-core/log'); /** * 安全插件 @@ -17,7 +18,7 @@ class ChromeExtensionAddon { * 创建 */ async create () { - this.app.console.info('[addon:chromeExtension] load'); + Log.info('[addon:chromeExtension] load'); const extensionIds = this.getAllIds(); for (let i = 0; i < extensionIds.length; i++) { @@ -59,10 +60,10 @@ class ChromeExtensionAddon { try { const extensionPath = path.join(this.getDirectory(), extensionId); - console.log('[addon:chromeExtension] extensionPath:', extensionPath); + Log.info('[addon:chromeExtension] extensionPath:', extensionPath); await session.defaultSession.loadExtension(extensionPath, { allowFileAccess: true }); } catch (e) { - console.log('[addon:chromeExtension] load extension error extensionId:%s, errorInfo:%s', extensionId, e.toString()); + Log.info('[addon:chromeExtension] load extension error extensionId:%s, errorInfo:%s', extensionId, e.toString()); return false } diff --git a/electron/addon/javaServer/index.js b/electron/addon/javaServer/index.js index e56e34b..eaacc96 100644 --- a/electron/addon/javaServer/index.js +++ b/electron/addon/javaServer/index.js @@ -1,7 +1,8 @@ const getPort = require('get-port'); const server = require("./server"); -const electronApp = require('electron').app; +const { app: electronApp } = require('electron'); const Log = require('ee-core/log'); +const Conf = require('ee-core/config'); /** * java server插件 @@ -11,8 +12,8 @@ class JavaServerAddon { constructor(app) { this.app = app; - this.cfg = app.config.addons.javaServer; - this.javaServer = null; + this.cfg; + this.javaServer; } /** @@ -22,10 +23,12 @@ class JavaServerAddon { * @since 1.0.0 */ async createServer () { + + this.cfg = Conf.getValue('addons.javaServer'); await this.createJavaPorts(); - this.javaServer = new server(this.app); - await this.javaServer.create(); + this.javaServer = new server(); + await this.javaServer.create(this.cfg); // kill electronApp.on("before-quit", async () => { @@ -61,7 +64,7 @@ class JavaServerAddon { this.cfg.port = javaPort; // 更新config配置 - this.app.getCoreDB().setItem("config", this.app.config); + Conf.setValue('addons.javaServer', this.cfg); } /** diff --git a/electron/addon/javaServer/server.js b/electron/addon/javaServer/server.js index 834c4a0..74b0838 100644 --- a/electron/addon/javaServer/server.js +++ b/electron/addon/javaServer/server.js @@ -4,24 +4,25 @@ const fs = require("fs"); const is = require('electron-is'); const path = require("path"); const { exec, execSync } = require("child_process"); -const Utils = require("ee-core").Utils; const ps = require("./ps"); const Log = require('ee-core/log'); +const Conf = require('ee-core/config'); +const UtilsPs = require('ee-core/ps'); /** * java server */ class JavaServer { - constructor (app) { - this.app = app; - this.options = app.config.addons.javaServer; + constructor () { + this.options; } /** * 创建服务 */ - async create () { - if (!this.options.enable) { + async create (cfg) { + this.options = cfg; + if (this.options.enable == false) { return; } @@ -30,17 +31,17 @@ class JavaServer { try { const jarName = this.options.name; - let softwarePath = path.join(Utils.getExtraResourcesDir(), jarName); + let softwarePath = path.join(UtilsPs.getExtraResourcesDir(), jarName); let javaOptStr = this.options.opt; - let jrePath = path.join(Utils.getExtraResourcesDir(), this.options.jreVersion); + let jrePath = path.join(UtilsPs.getExtraResourcesDir(), this.options.jreVersion); let cmdStr = ''; - this.app.console.info("[addon:javaServer] jar file path:", softwarePath); + Log.info("[addon:javaServer] jar file path:", softwarePath); if (!fs.existsSync(softwarePath)) throw new Error('java program does not exist'); // 替换opt参数 javaOptStr = _.replace(javaOptStr, "${port}", port); - javaOptStr = _.replace(javaOptStr, "${path}", Utils.getLogDir()); + javaOptStr = _.replace(javaOptStr, "${path}", UtilsPs.getLogDir()); if (is.windows()) { jrePath = path.join(jrePath, "bin", "javaw.exe"); @@ -80,7 +81,7 @@ class JavaServer { if (err) { throw new Error(err); } - console.info("[addon:javaServer] java程序退出 pid: ", item.pid); + Log.info("[addon:javaServer] java程序退出 pid: ", item.pid); }); }); diff --git a/electron/addon/security/index.js b/electron/addon/security/index.js index 5bfddbc..d19e670 100644 --- a/electron/addon/security/index.js +++ b/electron/addon/security/index.js @@ -14,7 +14,7 @@ class SecurityAddon { * 创建 */ create () { - this.app.console.info('[addon:security] load'); + Log.info('[addon:security] load'); const runWithDebug = process.argv.find(function(e){ let isHasDebug = e.includes("--inspect") || e.includes("--inspect-brk") || e.includes("--remote-debugging-port"); return isHasDebug; diff --git a/electron/addon/tray/index.js b/electron/addon/tray/index.js index d8f4565..27c6c4a 100644 --- a/electron/addon/tray/index.js +++ b/electron/addon/tray/index.js @@ -1,5 +1,9 @@ const {Tray, Menu} = require('electron'); const path = require('path'); +const Ps = require('ee-core/ps'); +const Log = require('ee-core/log'); +const Electron = require('ee-core/electron'); +const Conf = require('ee-core/config'); /** * 托盘插件 @@ -9,7 +13,6 @@ class TrayAddon { constructor(app) { this.app = app; - this.cfg = app.config.addons.tray; this.tray = null; } @@ -18,13 +21,15 @@ class TrayAddon { */ create () { // 开发环境,代码热更新开启时,会导致托盘中有残影 - if (process.env.EE_SERVER_ENV == 'local' && process.env.HOT_RELOAD == 'true') return; + if (Ps.isDev() && Ps.isHotReload()) return; - this.app.console.info('[addon:tray] load'); - const mainWindow = this.app.electron.mainWindow; + Log.info('[addon:tray] load'); + + const cfg = Conf.getValue('addons.tray'); + const mainWindow = Electron.mainWindow; // 托盘图标 - let iconPath = path.join(this.app.config.homeDir, this.cfg.icon); + let iconPath = path.join(Ps.getHomeDir(), cfg.icon); // 托盘菜单功能列表 const self = this; diff --git a/electron/config/config.default.js b/electron/config/config.default.js index 6067c5f..41972e5 100644 --- a/electron/config/config.default.js +++ b/electron/config/config.default.js @@ -81,7 +81,7 @@ module.exports = (appInfo) => { * 内置socket服务 */ config.socketServer = { - enable: false, + enable: true, port: 7070, path: "/socket.io/", connectTimeout: 45000, @@ -98,7 +98,7 @@ module.exports = (appInfo) => { * 内置http服务 */ config.httpServer = { - enable: false, + enable: true, https: { enable: false, key: '/public/ssl/localhost+1.key', @@ -176,7 +176,7 @@ module.exports = (appInfo) => { name: 'java-app.jar' }, example: { - enable: true, + enable: false, }, }; diff --git a/electron/controller/example.js b/electron/controller/example.js index 65456a6..2f3ca4e 100644 --- a/electron/controller/example.js +++ b/electron/controller/example.js @@ -11,7 +11,7 @@ const { powerMonitor, screen, nativeTheme } = require('electron'); const dayjs = require('dayjs'); -const ChildJob = require('ee-core/jobs/child'); +const { ChildJob } = require('ee-core/jobs'); const Ps = require('ee-core/ps'); const Log = require('ee-core/log'); @@ -195,6 +195,7 @@ class ExampleController extends Controller { content = args.content; } + // electron实验性功能,慎用 browserViewObj = new BrowserView(); this.app.electron.mainWindow.setBrowserView(browserViewObj) browserViewObj.setBounds({ @@ -211,6 +212,7 @@ class ExampleController extends Controller { * 移除视图内容 */ removeViewContent () { + // removeBrowserView移除视图后,进程依然存在,估计是electron bug this.app.electron.mainWindow.removeBrowserView(browserViewObj); return true } diff --git a/electron/jobs/example/timer.js b/electron/jobs/example/timer.js index f01f0eb..4c24cf8 100644 --- a/electron/jobs/example/timer.js +++ b/electron/jobs/example/timer.js @@ -38,7 +38,7 @@ class TimerJob extends Job { // 任务完成后,必须调用 Ps.exit() 方法,让进程退出,否则会常驻内存 setTimeout(function(){ Ps.exit(1); - }, 15 * 1000) + }, 10 * 1000) } } diff --git a/electron/preload/index.js b/electron/preload/index.js index 2708e47..a4af709 100644 --- a/electron/preload/index.js +++ b/electron/preload/index.js @@ -4,7 +4,6 @@ /** * 预加载模块入口 - * @param {Object} app - 全局app对象 */ module.exports = async (app) => { diff --git a/main.js b/main.js index 380dc03..c47ec6a 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,5 @@ const { Appliaction } = require('ee-core'); +const EE = require('ee-core/ee'); class Main extends Appliaction { @@ -45,5 +46,6 @@ class Main extends Appliaction { } } -new Main(); +// Instantiate an app object +EE.app = new Main();