electron-auto-updater-enhanced
Version:
Enhanced auto-updater for Electron applications
69 lines (62 loc) • 2.17 kB
JavaScript
const { autoUpdater } = require('electron-updater');
const { EventEmitter } = require('events');
// 初始化 autoUpdater
function initAutoUpdater(mainWindow) {
const emitter = new EventEmitter();
// 监听更新错误
autoUpdater.on("error", (error) => {
emitter.emit('auto-update-log', {
type: "error",
message:"Update error: " + error
});
mainWindow.webContents.send("message", { cmd: "error", message: error });
});
autoUpdater.on("checking-for-update", function (message) {
if (message) {
emitter.emit('auto-update-log', {
type: "info",
message:"checking-for-update" + message
});
mainWindow.webContents.send("message", { cmd: "checking-for-update", message: message });
}
});
autoUpdater.on("update-not-available", function (message) {
if (message) {
emitter.emit('auto-update-log', {
type: "info",
message:"update-not-available" + message
});
mainWindow.webContents.send("message", { cmd: "update-not-available", message: message });
}
});
autoUpdater.on("download-progress", function (message) {
if (message) {
mainWindow.webContents.send("message", { cmd: "download-progress", message: message });
}
});
// 监听更新可用
autoUpdater.on("update-available", (info) => {
emitter.emit('auto-update-log', {
type: "info",
message:"Update available: " + info
});
mainWindow.webContents.send("message", { cmd: "update-available", message: "有可用更新" });
});
// 监听更新下载完成
autoUpdater.on("update-downloaded", (info) => {
emitter.emit('auto-update-log', {
type: "info",
message:"Update downloaded: " + info
});
mainWindow.webContents.send("message", { cmd: "update-downloaded", message: "下载完成" });
autoUpdater.quitAndInstall(); // 退出并安装更新
});
// 检查更新
function checkForUpdates() {
autoUpdater.checkForUpdates();
}
return {
checkForUpdates,
};
}
module.exports = { initAutoUpdater };