安装electron-updater
npm install electron-updater
在渲染进程中点击立即更新时发消息到主进程,这个立即更新是在渲染进程中写的一个更新的弹窗
this.$ipcApi.send('download-client')
在主进程中,会检测到新版本自动安装
import {app, BrowserWindow, ipcMain} from 'electron'
import pkg from '../../../package.json'
import io from './io'
import {autoUpdater} from "electron-updater"
let mainWindow
function handleUpdate(win) {
mainWindow = win
const returnData = {
error: {status: -1, msg: '检测更新查询异常'},
checking: {status: 0, msg: '正在检查应用程序更新'},
updateAva: {status: 1, msg: '检测到新版本,正在下载,请稍后'},
updateNotAva: {status: -1, msg: '您现在使用的版本为最新版本,无需更新!'},
};
mainWindow.webContents.send('downloadInfo', '点击立即更新是否触发')
updataPath = '服务器的地址'
autoUpdater.setFeedURL({
provider: 'generic',
url:updataPath,
updaterCacheDirName:'tfstudent-updater'
});
autoUpdater.on('error', function (error) {
sendUpdateMessage(returnData.error)
});
autoUpdater.on('checking-for-update', function () {
sendUpdateMessage(win, returnData.checking)
});
autoUpdater.on('update-available', function (info) {
sendUpdateMessage(win, returnData.updateAva)
});
autoUpdater.on('update-not-available', function (info) {
setTimeout(function () {
sendUpdateMessage(returnData.updateNotAva)
}, 1000);
});
autoUpdater.on('download-progress', function (progressObj) {
mainWindow.webContents.send('autodownloadprogress', progressObj)
});
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
mainWindow.webContents.send('isUpdateNow')
autoUpdater.quitAndInstall();
});
autoUpdater.checkForUpdates();
}
function sendUpdateMessage(win, text) {
mainWindow.webContents.send('messageupdate', text)
}
export default handleUpdate
主进程的updata.js
执行完成以后会直接进行打包安装,更换的地址就是打最新的压缩包放到服务器上latest.yml
和同等级的.exe
的压缩包
latest.yml的内容
version: 9.1.10
files:
- url: 打包的名称
sha512: 版本检测的一个秘钥
size: 309004610
isAdminRightsRequired: true
path: 打包的名称
sha512: 版本检测的一个秘钥
releaseDate: '2021-05-20T10:06:20.027Z'
本地执行的一些报错信息
- 这个错是因为
utoUpdater.setFeedURL()
里面的放的服务地址是错的,应该放打包以后在build
文件夹里的latest.yml
并且文件夹里有最新的.exe的压缩包,放latest.yml
服务器上的地址时,把最后的latest.yml
后缀给去掉,因为electron-update
会把latest.yml
拼接上
- 这个错直接把打包以后的
latest.yml
copy到D:\tifang\student-desktop\dist\electron\dev-app-update.yml
这个目录下,这个目录是没有dev-app-update.yml
的,需要新建的一个dev-app-update.yml
然后把latest.yml
的内容放到dev-app-update.yml
,然后再点击立即更新时就没问题了