diff --git a/package.json b/package.json index 587a57e..ba0300f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wfrpc", - "version": "1.0.1", + "version": "1.0.2", "private": true, "description": "wfrpc图形客户端", "author": "Gavin ", @@ -60,6 +60,7 @@ "meow": "8.1.1" }, "dependencies": { + "electron-updater": "^5.3.0", "js-ini": "^1.5.1" } } diff --git a/src/app/index.html b/src/app/index.html index 95c18d0..b9bbfb4 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -71,10 +71,9 @@ -
+ diff --git a/src/main.js b/src/main.js index 2220b15..59ba976 100644 --- a/src/main.js +++ b/src/main.js @@ -8,25 +8,25 @@ const { Menu, nativeImage, } = require('electron'); - -let mainWindow; -const gotTheLock = app.requestSingleInstanceLock({ k: 'wrfpc_gui' }); - +const { autoUpdater } = require('electron-updater'); const { spawn } = require('child_process'); const path = require('path'); const ini = require('js-ini'); const fs = require('fs'); const homedir = require('os').homedir(); +const { homepage } = require('../package.json'); +let mainWindow; let runWfrpc; let configData = { common: {} }; + const sysData = { x64: 'amd64', ia32: '386', win32: 'windows' }; const sysPlatform = sysData[process.platform] || process.platform; - const wfrpcBasePath = path.join(homedir, '/.wfrpc'); const wfrpcConfigFile = path.join(wfrpcBasePath, 'wfrpc.ini'); const wfrpcBinName = sysPlatform === 'windows' ? 'wfrpc.exe' : 'wfrpc'; const wfrpcBin = app.isPackaged ? path.join(process.resourcesPath, wfrpcBinName) : path.join('wfrpc', sysPlatform, wfrpcBinName); +const gotTheLock = app.requestSingleInstanceLock({ k: 'wrfpc_gui' }); const handleStartWfrpc = (event) => { const webContents = event.sender; @@ -201,8 +201,46 @@ if (!gotTheLock) { fs.truncateSync(wfrpcConfigFile); dialog.showMessageBox(BrowserWindow.fromWebContents(event.sender), { message: '配置文件已清空', type: 'info' }); }); + ipcMain.handle('downloadUpdate', (event, version) => { + dialog.showMessageBox({ + title: '提示', + message: `发现新版本v${version}!`, + type: 'info', + buttons: ['更新', '查看'], + noLink: true, + cancelId: -1, + }).then(({ response }) => { + if (response === 0) autoUpdater.downloadUpdate(); + if (response === 1) shell.openExternal(`${homepage}/releases/v${version}`); + }); + }); mainWindow = createMainWindow(); + + // check update + autoUpdater.autoDownload = false; + autoUpdater.checkForUpdates(); + + autoUpdater.on('update-available', (info) => { + mainWindow.webContents.send('wfrpc:update:version', info.version); + }); + + autoUpdater.on('download-progress', (progressObj) => { + mainWindow.webContents.send('wfrpc:update:download:progress', progressObj.percent); + }); + + autoUpdater.on('update-downloaded', () => { + dialog.showMessageBox({ + title: '提示', + message: '下载完成,是否退出更新?', + type: 'info', + buttons: ['是', '否'], + noLink: true, + cancelId: -1, + }).then(({ response }) => { + if (response === 0) autoUpdater.quitAndInstall(); + }); + }); }); app.on('window-all-closed', () => { diff --git a/src/preload.js b/src/preload.js index db8423d..379e4a2 100644 --- a/src/preload.js +++ b/src/preload.js @@ -58,4 +58,29 @@ window.addEventListener('DOMContentLoaded', async () => { document.getElementById('server_port').value = config.server_port || ''; document.getElementById('token').value = config.token || ''; }); + + ipcRenderer.on('wfrpc:update:version', (_event, version) => { + const a = document.createElement('a'); + a.className = 'text-danger ms-1 text-blink'; + a.href = '#'; + a.innerText = `新版本: v${version}`; + a.onclick = () => { + ipcRenderer.invoke('downloadUpdate', version); + }; + + document.getElementById('showtext').before(a); + }); + + ipcRenderer.on('wfrpc:update:download:progress', (_event, progress) => { + const div = document.createElement('div'); + div.className = 'progress'; + div.style.height = '3px'; + div.innerHTML = '
'; + + if (!document.getElementById('progress')) { + document.body.prepend(div); + } + + document.getElementById('progress').style.width = `${progress}%`; + }); }); diff --git a/yarn.lock b/yarn.lock index 9f46b24..5a5f037 100644 --- a/yarn.lock +++ b/yarn.lock @@ -252,6 +252,11 @@ dependencies: "@types/node" "*" +"@types/semver@^7.3.6": + version "7.3.13" + resolved "https://registry.npmmirror.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91" + integrity sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw== + "@types/verror@^1.10.3": version "1.10.6" resolved "https://registry.npmmirror.com/@types/verror/-/verror-1.10.6.tgz#3e600c62d210c5826460858f84bcbb65805460bb" @@ -1075,6 +1080,21 @@ electron-publish@23.6.0: lazy-val "^1.0.5" mime "^2.5.2" +electron-updater@^5.3.0: + version "5.3.0" + resolved "https://registry.npmmirror.com/electron-updater/-/electron-updater-5.3.0.tgz#3ba0d20407911a2edc5a68bee45c5aa2023e9ff8" + integrity sha512-iKEr7yQBcvnQUPnSDYGSWC9t0eF2YbZWeYYYZzYxdl+HiRejXFENjYMnYjoOm2zxyD6Cr2JTHZhp9pqxiXuCOw== + dependencies: + "@types/semver" "^7.3.6" + builder-util-runtime "9.1.1" + fs-extra "^10.0.0" + js-yaml "^4.1.0" + lazy-val "^1.0.5" + lodash.escaperegexp "^4.1.2" + lodash.isequal "^4.5.0" + semver "^7.3.5" + typed-emitter "^2.1.0" + electron@^22.0.0: version "22.0.0" resolved "https://registry.npmmirror.com/electron/-/electron-22.0.0.tgz#ef84ab9cf23aa3f8c2f42a1e8e000ad7fd941058" @@ -2220,6 +2240,16 @@ locate-path@^6.0.0: dependencies: p-locate "^5.0.0" +lodash.escaperegexp@^4.1.2: + version "4.1.2" + resolved "https://registry.npmmirror.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347" + integrity sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw== + +lodash.isequal@^4.5.0: + version "4.5.0" + resolved "https://registry.npmmirror.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0" + integrity sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ== + lodash.merge@^4.6.2: version "4.6.2" resolved "https://registry.npmmirror.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" @@ -2868,6 +2898,13 @@ run-parallel@^1.1.9: dependencies: queue-microtask "^1.2.2" +rxjs@^7.5.2: + version "7.8.0" + resolved "https://registry.npmmirror.com/rxjs/-/rxjs-7.8.0.tgz#90a938862a82888ff4c7359811a595e14e1e09a4" + integrity sha512-F2+gxDshqmIub1KdvZkaEfGDwLNpPvk9Fs6LD/MyQxNgMds/WH9OdDDXOmxUZpME+iSK3rQCctkL0DYyytUqMg== + dependencies: + tslib "^2.1.0" + safe-buffer@5.2.1, safe-buffer@^5.1.1: version "5.2.1" resolved "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" @@ -2924,6 +2961,13 @@ semver@^7.3.2, semver@^7.3.4, semver@^7.3.7: dependencies: lru-cache "^6.0.0" +semver@^7.3.5: + version "7.3.8" + resolved "https://registry.npmmirror.com/semver/-/semver-7.3.8.tgz#07a78feafb3f7b32347d725e33de7e2a2df67798" + integrity sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A== + dependencies: + lru-cache "^6.0.0" + semver@~7.0.0: version "7.0.0" resolved "https://registry.npmmirror.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e" @@ -3259,6 +3303,11 @@ tsconfig-paths@^3.14.1: minimist "^1.2.6" strip-bom "^3.0.0" +tslib@^2.1.0: + version "2.5.0" + resolved "https://registry.npmmirror.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf" + integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg== + type-check@^0.4.0, type-check@~0.4.0: version "0.4.0" resolved "https://registry.npmmirror.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1" @@ -3291,6 +3340,13 @@ type-fest@^0.8.1: resolved "https://registry.npmmirror.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d" integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA== +typed-emitter@^2.1.0: + version "2.1.0" + resolved "https://registry.npmmirror.com/typed-emitter/-/typed-emitter-2.1.0.tgz#ca78e3d8ef1476f228f548d62e04e3d4d3fd77fb" + integrity sha512-g/KzbYKbH5C2vPkaXGu8DJlHrGKHLsM25Zg9WuC9pMGfuvT+X25tZQWo5fK1BjBm8+UrVE9LDCvaY0CQk+fXDA== + optionalDependencies: + rxjs "^7.5.2" + unbox-primitive@^1.0.2: version "1.0.2" resolved "https://registry.npmmirror.com/unbox-primitive/-/unbox-primitive-1.0.2.tgz#29032021057d5e6cdbd08c5129c226dff8ed6f9e"