Merge pull request #194 from rubickCenter/feature/update

 add windows auto-update feature
This commit is contained in:
璃白 2023-06-24 23:03:39 +08:00 committed by GitHub
commit 867a7b1fec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 138 additions and 18 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "rubick", "name": "rubick",
"version": "2.2.8", "version": "2.2.9",
"author": "muwoo <2424880409@qq.com>", "author": "muwoo <2424880409@qq.com>",
"private": true, "private": true,
"scripts": { "scripts": {
@ -62,6 +62,5 @@
"typescript": "~4.1.5", "typescript": "~4.1.5",
"vue-cli-plugin-electron-builder": "~2.1.1", "vue-cli-plugin-electron-builder": "~2.1.1",
"worker-plugin": "^5.0.1" "worker-plugin": "^5.0.1"
}, }
"__npminstall_done": false
} }

3
release/releaseNotes.md Normal file
View File

@ -0,0 +1,3 @@
- 增加windows自动更新逻辑
- 修复一些已知问题
- 发布时间 2023-06-24

View File

@ -1,6 +1,7 @@
import { app, BrowserWindow, protocol } from "electron"; import { app, BrowserWindow, protocol } from 'electron';
import path from "path"; import path from 'path';
import { createProtocol } from "vue-cli-plugin-electron-builder/lib"; import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import versonHandler from '../common/versionHandler';
export default () => { export default () => {
let win: any; let win: any;
@ -15,7 +16,7 @@ export default () => {
resizable: true, resizable: true,
width: 800, width: 800,
frame: false, frame: false,
title: "拉比克", title: '拉比克',
show: false, show: false,
skipTaskbar: true, skipTaskbar: true,
webPreferences: { webPreferences: {
@ -25,40 +26,41 @@ export default () => {
contextIsolation: false, contextIsolation: false,
webviewTag: true, webviewTag: true,
nodeIntegration: true, nodeIntegration: true,
preload: path.join(__static, "preload.js") preload: path.join(__static, 'preload.js'),
} },
}); });
if (process.env.WEBPACK_DEV_SERVER_URL) { if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode // Load the url of the dev server if in development mode
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string); win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
} else { } else {
createProtocol("app"); createProtocol('app');
// Load the index.html when not in development // Load the index.html when not in development
win.loadURL("app://./index.html"); win.loadURL('app://./index.html');
} }
protocol.interceptFileProtocol("image", (req, callback) => { protocol.interceptFileProtocol('image', (req, callback) => {
const url = req.url.substr(8); const url = req.url.substr(8);
callback(decodeURI(url)); callback(decodeURI(url));
}); });
win.on("closed", () => { win.on('closed', () => {
win = undefined; win = undefined;
}); });
win.on("show", () => { win.on('show', () => {
win.webContents.executeJavaScript( win.webContents.executeJavaScript(
`window.rubick && window.rubick.hooks && typeof window.rubick.hooks.onShow === "function" && window.rubick.hooks.onShow()` `window.rubick && window.rubick.hooks && typeof window.rubick.hooks.onShow === "function" && window.rubick.hooks.onShow()`
); );
versonHandler.checkUpdate();
// win.webContents.openDevTools(); // win.webContents.openDevTools();
}); });
win.on("hide", () => { win.on('hide', () => {
win.webContents.executeJavaScript( win.webContents.executeJavaScript(
`window.rubick && window.rubick.hooks && typeof window.rubick.hooks.onHide === "function" && window.rubick.hooks.onHide()` `window.rubick && window.rubick.hooks && typeof window.rubick.hooks.onHide === "function" && window.rubick.hooks.onHide()`
); );
}); });
// 判断失焦是否隐藏 // 判断失焦是否隐藏
win.on("blur", () => { win.on('blur', () => {
const config = { ...global.OP_CONFIG.get() }; const config = { ...global.OP_CONFIG.get() };
if (config.perf.common.hideOnBlur) { if (config.perf.common.hideOnBlur) {
win.hide(); win.hide();
@ -70,6 +72,6 @@ export default () => {
return { return {
init, init,
getWindow getWindow,
}; };
}; };

View File

@ -69,7 +69,7 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
) )
); );
mainWindow.setAlwaysOnTop(true); mainWindow.setAlwaysOnTop(false);
mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true }); mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
mainWindow.focus(); mainWindow.focus();
mainWindow.setVisibleOnAllWorkspaces(false, { mainWindow.setVisibleOnAllWorkspaces(false, {

View File

@ -0,0 +1,105 @@
import { dialog } from 'electron';
import { autoUpdater } from 'electron-updater';
import pkg from '../../../package.json';
import API from './api';
import commonConst from '@/common/utils/commonConst';
import { main } from '../browsers';
import { app } from 'electron';
// Object.defineProperty(app, 'isPackaged', {
// get() {
// return true;
// },
// });
class VersionHandler {
private lastestVersion: string;
private currentVersion: string;
private releaseNotes: string;
private isUpdate: boolean;
constructor() {
this.lastestVersion = '';
this.currentVersion = pkg.version;
this.releaseNotes = '';
this.isUpdate = false;
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false;
}
checkForMacAndWindows() {
let sendUpdateMsg = false;
autoUpdater.removeAllListeners();
// update-available 会触发多次,限制只通知一次
autoUpdater.checkForUpdates();
autoUpdater.on('download-progress', ({ percent }) => {
console.log('下载进度', percent);
// if (percent < 50) {
// }
this.isUpdate = true;
});
autoUpdater.on('update-available', (info) => {
if (sendUpdateMsg) return;
const { version, releaseName = 'normal', releaseNotes } = info;
this.lastestVersion = version;
sendUpdateMsg = true;
autoUpdater.on('update-downloaded', () => {
console.log('下载完成');
this.isUpdate = false;
if (releaseName === 'major') {
autoUpdater.quitAndInstall(true, true);
}
const mainWindow = main().getWindow()
dialog
.showMessageBox(mainWindow, {
title: '版本更新',
message: `发现新版本${this.lastestVersion},是否更新\n\n${releaseNotes}`,
type: 'info',
buttons: ['稍后提示', '立即更新'],
})
.then(({ response }) => {
console.log(response);
if (response === 1) {
this.update();
}
});
});
// 自动下载安装包
if (!this.isUpdate) {
autoUpdater.downloadUpdate();
console.log('download');
}
});
autoUpdater.on('update-not-available', (info) => {
if (sendUpdateMsg) return;
sendUpdateMsg = true;
});
autoUpdater.on('error', () => {
this.isUpdate = false;
});
}
checkUpdate(): void {
this.checkForMacAndWindows();
}
update() {
let sendUpdateMsg = false;
this.checkUpdate();
autoUpdater.on('update-downloaded', () => {
if (sendUpdateMsg) return;
sendUpdateMsg = true;
this.isUpdate = false;
autoUpdater.quitAndInstall(true, true);
// App.quit();
});
}
}
export default new VersionHandler();

View File

@ -33,6 +33,17 @@ module.exports = {
directories: { directories: {
output: 'build', output: 'build',
}, },
releaseInfo: {
releaseName: 'normal', // normal 弹窗 / major 强制更新
releaseNotesFile: './release/releaseNotes.md',
},
publish: [
{
provider: 'github',
owner: 'rubickCenter',
repo: 'rubick',
},
],
// files: ["dist_electron/**/*"], // files: ["dist_electron/**/*"],
dmg: { dmg: {
contents: [ contents: [