diff --git a/README.md b/README.md index 18e9b20..9040f11 100644 --- a/README.md +++ b/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 diff --git a/config/config.default.js b/config/config.default.js index debeb43..ec6f0df 100644 --- a/config/config.default.js +++ b/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); -// } diff --git a/electron/autoUpdater.js b/electron/autoUpdater.js new file mode 100644 index 0000000..96e6f2a --- /dev/null +++ b/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; \ No newline at end of file diff --git a/electron/config.js b/electron/config.js index a1c5a9d..7234a65 100644 --- a/electron/config.js +++ b/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 {}; }; diff --git a/electron/setup.js b/electron/setup.js index 1b3ae77..c2b7e3b 100644 --- a/electron/setup.js +++ b/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 () { diff --git a/electron/storage.js b/electron/storage.js index bdd2122..e174b90 100644 --- a/electron/storage.js +++ b/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() diff --git a/main.js b/main.js index 1f49e34..b4357fd 100644 --- a/main.js +++ b/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) diff --git a/package.json b/package.json index e38a50c..18a7dd0 100644 --- a/package.json +++ b/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": {