const { autoUpdater } = require("electron-updater");
const log = require("electron-log");
// 配置日志
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = "info";
// 手动检查更新
function checkForUpdates() {
autoUpdater.checkForUpdates();
}
// 监听更新事件
autoUpdater.on("checking-for-update", () => {
log.info("Checking for update...");
// 可以在 UI 中显示“正在检查更新”的提示
});
autoUpdater.on("update-available", (info) => {
log.info("Update available:", info);
// 可以在 UI 中提示用户有新版本,并询问是否下载
});
autoUpdater.on("update-not-available", (info) => {
log.info("Update not available:", info);
// 可以在 UI 中提示用户当前已是最新版本
});
autoUpdater.on("download-progress", (progressObj) => {
log.info("Download progress:", progressObj);
// 可以在 UI 中显示下载进度
});
autoUpdater.on("update-downloaded", (info) => {
log.info("Update downloaded; ready to install");
// 可以在 UI 中提示用户更新已下载完成,并询问是否立即安装
// 例如:
// dialog.showMessageBox({
// type: 'info',
// title: 'Update Ready',
// message: 'A new version has been downloaded. Restart the application to apply the updates.',
// buttons: ['Restart', 'Later']
// }).then((result) => {
// if (result.response === 0) {
// autoUpdater.quitAndInstall();
// }
// });
});
autoUpdater.on("error", (err) => {
log.error("Error in auto-updater:", err);
// 可以在 UI 中提示用户更新失败
});
// 在应用启动时检查更新
app.on("ready", () => {
checkForUpdates();
});