gaoshuaixing 5 роки тому
батько
коміт
0a6ef89538
8 змінених файлів з 87 додано та 24 видалено
  1. 6 5
      README.md
  2. 1 7
      config/config.default.js
  3. 50 0
      electron/autoUpdater.js
  4. 14 5
      electron/config.js
  5. 2 0
      electron/setup.js
  6. 2 2
      electron/storage.js
  7. 11 4
      main.js
  8. 1 1
      package.json

+ 6 - 5
README.md

@@ -6,11 +6,12 @@
 [文档迁移到了羽雀](https://www.yuque.com/u34495/mivcfg/xnhmms)
 
 ## 特性
-1. 直接打包成windows版、Mac版、Linux版或者以web网站运行
-2. 可以用服务端的开发思维,来编写桌面软件
-3. 也可以用前端来开发,数据服务请求外部api即可
-4. 服务端的技术场景几乎都可以使用,如:路由、中间件、控制器、服务、定时任务、队列、插件等
+1. 直接打包成windows版、Mac版、Linux版或者以web网站运行
+2. 可以用服务端的开发思维,来编写桌面软件
+3. 也可以用前端来开发,数据服务请求外部api即可
+4. 服务端的技术场景几乎都可以使用,如:路由、中间件、控制器、服务、定时任务、队列、插件等
 5. 桌面软件常见功能,后续逐步集成并完善或提供demo。
+6. 软件自动更新。
 
 ## 开始使用
 
@@ -62,7 +63,7 @@
 [体验web版本和客户端版](http://b.kaka996.com/)
 
 ## 进行中功能
-1. 软件自动更新
+1. 软件自动更新(已完成)
 
 ## 欢迎star
 

+ 1 - 7
config/config.default.js

@@ -2,7 +2,7 @@
 
 'use strict';
 const path = require('path');
-const electronEggConfig = require('../electron/config').get('web-egg');
+const electronEggConfig = require('../electron/config').get('webEgg');
 
 /**
  * @param {Egg.EggAppInfo} appInfo app info
@@ -91,14 +91,8 @@ module.exports = appInfo => {
   };
 
   config.ejs = {};
-  //getPort();
   return {
     ...config,
     ...userConfig,
   };
 };
-
-// function getPort () {
-//   const dbFile = path.normalize('./storage/db.json');
-//   console.log('dbFile:', dbFile);
-// }

+ 50 - 0
electron/autoUpdater.js

@@ -0,0 +1,50 @@
+'use strict';
+
+const updater = require("electron-updater");
+const autoUpdater = updater.autoUpdater;
+const config = require('./config');
+const path = require('path');
+const {app} = require('electron');
+
+exports.setup = function () {
+  const pkgInfo = require(path.join(app.getAppPath(), 'package.json'));
+  ELog.info('[autoUpdater] [setup] current version: ', pkgInfo.version);
+  const updateConfig = config.get('autoUpdate');
+  autoUpdater.setFeedURL(updateConfig.options);
+
+  autoUpdater.on('checking-for-update', () => {
+    sendStatusToWindow('Checking for update...');
+  })
+  autoUpdater.on('update-available', (info) => {
+    sendStatusToWindow('Update available.');
+  })
+  autoUpdater.on('update-not-available', (info) => {
+    sendStatusToWindow('Update not available.');
+  })
+  autoUpdater.on('error', (err) => {
+    sendStatusToWindow('Error in auto-updater. ' + err);
+  })
+  autoUpdater.on('download-progress', (progressObj) => {
+    let log_message = "Download speed: " + progressObj.bytesPerSecond;
+    log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
+    log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
+    sendStatusToWindow(log_message);
+  })
+  autoUpdater.on('update-downloaded', (info) => {
+    sendStatusToWindow('Update downloaded');
+    // quit and update
+    autoUpdater.quitAndInstall();
+  });
+
+};
+
+exports.checkUpdate = function () {
+  autoUpdater.checkForUpdatesAndNotify();
+}
+
+function sendStatusToWindow(text) {
+  ELog.info(text);
+  MAIN_WINDOW.webContents.send('message', text);
+}
+
+exports = module.exports;

+ 14 - 5
electron/config.js

@@ -32,11 +32,18 @@ const config = {
     port: 7068,
     hostname: '0.0.0.0',
     workers: 1
+  },
+  autoUpdate: {
+    enable: true,
+    options: {
+      provider: 'generic', // or github, s3, bintray
+      url: '' // resource dir
+    }
   }
 }
 
 exports.get = function (flag = '') {
-  console.log('config flag:', flag);
+  console.log('[config] [get] flag:', flag);
   if (flag === 'log') {
     return config.log;
   }
@@ -45,19 +52,21 @@ exports.get = function (flag = '') {
     return config.windowsOption;
   }
 
-  if (flag === 'web-egg') {
+  if (flag === 'webEgg') {
     return config.egg;
   }
-
+  
   if (flag === 'egg') {
     const eggConfig = storage.getEggConfig();
-    console.log('eggConfig:', eggConfig);
     if (eggConfig.port) {
-      console.log('eggConfig.port:', eggConfig.port);
       config.egg.port = eggConfig.port;
     }
     return config.egg;
   }
+
+  if (flag === 'autoUpdate') {
+    return config.autoUpdate;
+  }
   
   return {};
 };

+ 2 - 0
electron/setup.js

@@ -3,10 +3,12 @@
 global.ELog = require('electron-log');
 const storage = require('./storage');
 const config = require('./config');
+const autoUpdater = require('./autoUpdater');
 
 module.exports = () => {
   storage.setup();
   logger();
+  autoUpdater.setup();
 }
 
 function logger () {

+ 2 - 2
electron/storage.js

@@ -50,8 +50,8 @@ exports.getEggConfig = function () {
 };
 
 exports.setDynamicPort = async function () {
-  const eggConfig = config.get('egg');
-  console.log('setDynamicPort eggConfig:', eggConfig);
+  // const eggConfig = config.get('egg');
+  // console.log('setDynamicPort eggConfig:', eggConfig);
   // const dynamicPort = await getPort({port: eggConfig.port})
   const dynamicPort = await getPort();
   const res = this.instance()

+ 11 - 4
main.js

@@ -4,14 +4,15 @@ const eggLauncher = require('./electron/lanucher')
 const setup = require('./electron/setup')
 const electronConfig = require('./electron/config')
 const storage = require('./electron/storage')
+const autoUpdater = require('./electron/autoUpdater')
+
+// main window
+global.MAIN_WINDOW = null
 
 // Initialize 
 setup()
 // return
 
-// main window
-global.MAIN_WINDOW = null
-
 if (process.mas) app.setName('electron-egg')
 
 // Open url with the default browser
@@ -73,6 +74,12 @@ async function createWindow () {
     startServer(eggConfig)
   }, 100)
 
+  // check update
+  const updateConfig = electronConfig.get('autoUpdate')
+  if (updateConfig.enable) {
+    autoUpdater.checkUpdate()
+  }
+
   return MAIN_WINDOW
 }
 
@@ -80,7 +87,7 @@ async function startServer (options) {
   let startRes = null
   ELog.info('[main] [startServer] options', options)
   startRes = await eggLauncher.start(options).then((res) => res, (err) => err)
-  ELog.info('startRes:', startRes)
+  ELog.info('[main] [startServer] startRes:', startRes)
   if (startRes === 'success') {
     let url = 'http://localhost:' + options.port
     MAIN_WINDOW.loadURL(url)

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "electron-egg",
-  "version": "1.1.0",
+  "version": "1.1.2",
   "description": "A fast, desktop software development framework",
   "main": "main.js",
   "scripts": {