Skip to content

Commit

Permalink
feat: 新增自动更新功能
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinTan committed Feb 10, 2023
1 parent ab91131 commit c1016c5
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wfrpc",
"version": "1.0.1",
"version": "1.0.2",
"private": true,
"description": "wfrpc图形客户端",
"author": "Gavin <[email protected]>",
Expand Down Expand Up @@ -60,6 +60,7 @@
"meow": "8.1.1"
},
"dependencies": {
"electron-updater": "^5.3.0",
"js-ini": "^1.5.1"
}
}
5 changes: 2 additions & 3 deletions src/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@
</div>
</div>

<div class="fixed-bottom border-top" style="font-size:14px;">
<div class="fixed-bottom border-top d-flex justify-content-between" style="font-size:14px;">
<span id="showtext" class="text-secondary ms-1" spellcheck="false"></span>
<!-- <span class="d-none float-end text-success me-2 text-blink" id="showrunning">running</span> -->
<a class="float-end me-2" href="#" id="showlog">运行日志</a>
<a class="me-2" href="#" id="showlog">运行日志</a>
</div>


Expand Down
48 changes: 43 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', () => {
Expand Down
25 changes: 25 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div class="progress-bar bg-success" id="progress"></div>';

if (!document.getElementById('progress')) {
document.body.prepend(div);
}

document.getElementById('progress').style.width = `${progress}%`;
});
});
56 changes: 56 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1075,6 +1080,21 @@ [email protected]:
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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

[email protected], safe-buffer@^5.1.1:
version "5.2.1"
resolved "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit c1016c5

Please sign in to comment.