Skip to main content

Electron 实践

AuthUpdater 自动更新

Implementation 实现

import { autoUpdater } from "electron-updater";

class AppUpdater {
constructor() {
log.transports.file.level = "info";
autoUpdater.logger = log;
autoUpdater.setFeedURL({
provider: "github",
owner: "ower",
repo: "repo",
token: CONSTANTS_GPT_AI_FLOW_COMMON.GITHUB_TOKEN,
});
autoUpdater.checkForUpdatesAndNotify();
}
}

new AppUpdater();

autoUpdater.on("update-available", () => {
console.log("update_available");
});
autoUpdater.on("update-downloaded", (event) => {
console.log("update_downloaded");

const dialogOpts: MessageBoxOptions = {
type: "info",
buttons: ["Restart", "Later"],
title: "Application Update",
message: " A new version is ready",
detail:
"A new version has been downloaded. Restart the application to apply the updates.",
};

dialog
.showMessageBox(dialogOpts)
.then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall();
})
.catch((err) => {
console.log("err", err);
});
});
autoUpdater.on("error", (message) => {
console.error("There was a problem updating the application: ", message);
});

Test 测试

Object.defineProperty(app, "isPackaged", {
get() {
return true;
},
});

autoUpdater.on("update-available", () => {
console.log("update_available");
setTimeout(() => {
mainWindow?.webContents.send(
"ipc-trigger-shortcut-clipboardText-to-textarea-in-main-window",
"update_available"
);
}, 10000);
});
autoUpdater.on("update-downloaded", (event) => {
console.log("update_downloaded");
mainWindow?.webContents.send(
"ipc-trigger-shortcut-clipboardText-to-textarea-in-main-window",
"update_downloaded"
);
setTimeout(() => {
mainWindow?.webContents.send(
"ipc-trigger-shortcut-clipboardText-to-textarea-in-main-window",
event
);
const dialogOpts: MessageBoxOptions = {
type: "info",
buttons: ["Restart", "Later"],
title: "Application Update",
message: " A new version is ready",
detail:
"A new version has been downloaded. Restart the application to apply the updates.",
};

dialog
.showMessageBox(dialogOpts)
.then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall();
})
.catch((err) => {
mainWindow?.webContents.send(
"ipc-trigger-shortcut-clipboardText-to-textarea-in-main-window",
err
);
});
}, 10000);
});

autoUpdater.on("error", (message) => {
console.error("There was a problem updating the application");
console.error(message);
setTimeout(() => {
mainWindow?.webContents.send(
"ipc-trigger-shortcut-clipboardText-to-textarea-in-main-window",
message
);
}, 10000);
});

Build

electron-builder build --publish always