全量更新
和 增量更新
当前为全量更新:
update.electron.org
实现自动更新,以及 electron-builder
打包方案。AutoUpdate
功能。publish
选项,指定 provider
和 url
,其中 url
是存放升级更新包的服务器。 // @see https://www.electron.build/configuration/configuration
{ "appId": "com.test.app", "asar": true, "directories": { "output": "dist/", }, "mac": { "target": ["dmg", "zip"] }, "win": { "target": [ { "target": "nsis", "arch": ["x64"], }, ], }, "publish": { "provider": "generic", "url": "https://www.test.com/download/", // 资源服务器 "channel": "latest", }, }
latest.yml
和app-0.0.2.exe
放到资源服务器,给应用提供下载。 ├── dist ├── win-unpacked ├── app-0.0.2.exe ├── app-0.0.2.exe.blockmap ├── builder-effective-config.yaml ├── latest.yml
autoUpdater.checkForUpdates()
的检测逻辑,读取资源服务器 latest.yml
文件,对比文件摘要 import { app, BrowserWindow, shell, ipcMain, session, Menu, Tray, safeStorage } from 'electron' import { autoUpdater } from 'electron-updater' // 执行自动更新检查 ipcMain.on('checkForUpdate', () => { isRobot = false autoUpdater.checkForUpdates() }) // 主进程推送消息到渲染进程 function sendUpdateMessage(obj: any) { win?.webContents.send('updateMessage', obj) } // 执行下载 ipcMain.on('downloadUpdate', () => { autoUpdater.downloadUpdate() }) // 是否自动下载更新,设置为 false 时将通过 API 触发更新下载 autoUpdater.autoDownload = false; // 是否允许版本降级,也就是服务器版本低于本地版本时,依旧以服务器版本为主 autoUpdater.allowDowngrade = true; // 更新错误 autoUpdater.on('error', function (err) { sendUpdateMessage({ action: 'update-error', errorInfo: err }) }) // 检查中 autoUpdater.on('checking-for-update', function () { sendUpdateMessage({ action: 'checking' }) }) // 发现新版本,通知渲染进程吊起确认下载更新按钮 autoUpdater.on('update-available', function (info) { sendUpdateMessage({ action: 'updateAva', updateInfo: info }) }) // 当前版本为最新版本 autoUpdater.on('update-not-available', function (info) { setTimeout(function () { // 项目中做了增量更新,所以加了增量更新逻辑 //hotUpdate() // 增量更新检查 }, 1000) }) // 更新下载进度事件 autoUpdater.on('download-progress', function (progressObj) { console.log('触发下载。。。') console.log(progressObj) win?.webContents.send('downloadProgress', progressObj) }) // 安装包下载完成 autoUpdater.on('update-downloaded', function () { // 安装包下载进度 win?.webContents.send('downloadProgress', { percent: 100 }) // ipcMain.on('isUpdateNow', (e, arg) => { // 可以手动选择是否立即退出并更新 ipcMain.on('isUpdateNow', (e, arg) => { // some code here to handle event autoUpdater.quitAndInstall() }) })
import { ipcRenderer } from 'electron' // 添加自动更新事件的监听 ipcRenderer.on('updateMessage', (event, obj) => { if (obj.action === 'updateAva') { // 发现新版本 } else if (obj.action === 'update-error') { // 更新错误 } else if (obj.action === 'updateNotAva') { // 没有新版本 } else { } })
electron-updater
将会请求该链接的服务地址,这个地址需要返回一个更新配置信息文件,这个文件就是 electron-builder
打包出来的 YML 文件。electron-updater
会校验配置中 version
的格式,需要符合为 semver version 规范,但是一般 Windows 应用是四位版本号,所以采用像 *.*.*-*
这种形式能通过检验electron-updater
会校验配置中的 sha512
值与更新下载下来的文件的 sha512
值,不匹配会抛出更新错误,所以不能随意修改安装程序(只要文件 MD5 值不变即可)path
值即是 electron-updater
执行下载更新的目标地址,是新版本程序安装包的文件地址publish
即可autoUpdater
即可