electron-builder自动更新
创建时间:2025-05-12 13:09
长度:1457
浏览:0
评论:0
官方文档: https://www.electron.build/publish
在package.json buildn属性下添加publist
{
"build": {
"appId": "xxx",
...,
"publish": ["github"],
},
"scripts": {
...
"release": "cross-env GH_TOKEN=你的github token electron-builder",
"prerelease": "npm run build"
}
}
程序内增加自动代码
安装
npm i electron-updater
在main.js修改
const { autoUpdater } = require('electron-updater');
app.on('ready', () => {
autoUpdater.autoDownload = false; // 取消自动下载
autoUpdater.checkForUpdatesAndNotify(); //
autoUpdater.on('error', err => {
dialog.showErrorBox(Error, error === null ? '未知错误' : JSON.stringify(err));
})
// 有更新时
autoUpdater.on('update-available', () => {
dialog.showMessageBox({
type: 'info',
title: '应用有新的版本',
message: '发现有新版本,是否现在更新?',
buttons: ['是', '否']
}, buttonIndex => {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate();
}
})
})
// 没有新版本
autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({
title: '没有新版本',
message: '当前已经是最新版本'
})
})
// ....
})
开发环境中测试自动更新
根目录创建一个文件 dev-app-update.yml
main.js增加代码
if (isDev) {
// 本地调试自动更新
autoUpdater.updateConfigPath = path.join(__dirname, './dev-app-update.yml')
}
if (isDev) {
autoUpdater.checkForUpdates(); // 本地的检查更新
} else {
autoUpdater.checkForUpdatesAndNotify(); // 检查更新
}