指南
如何在 Electron 中使用 Electron Updater
使用@capgo/electron-updater
@capgo/electron-updater 为 Electron 应用提供相同的 Capgo 实时更新模型 @capgo/capacitor-updater。您在主进程中初始化它,通过预加载暴露渲染器桥梁,并在每次启动时调用 notifyAppReady() 安装
主进程设置
bun add @capgo/electron-updater
预加载桥梁
import { app, BrowserWindow } from 'electron';
import path from 'node:path';
import { ElectronUpdater, setupEventForwarding, setupIPCHandlers } from '@capgo/electron-updater';
const updater = new ElectronUpdater({
appId: 'com.example.desktop',
autoUpdate: true,
});
app.whenReady().then(async () => {
const window = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
},
});
const builtinPath = path.join(__dirname, 'www/index.html');
await updater.initialize(window, builtinPath);
setupIPCHandlers(updater);
setupEventForwarding(updater, window);
await window.loadFile(updater.getCurrentBundlePath());
});
渲染器使用
import { exposeUpdaterAPI } from '@capgo/electron-updater/preload';
exposeUpdaterAPI();
监听更新事件
import { requireUpdater } from '@capgo/electron-updater/renderer';
const updater = requireUpdater();
await updater.notifyAppReady();
const latest = await updater.getLatest();
if (latest.url && !latest.error) {
const bundle = await updater.download({
url: latest.url,
version: latest.version,
checksum: latest.checksum,
});
await updater.next({ id: bundle.id });
}
部署新包
updater.addListener('download', ({ percent }) => {
console.log('Download progress', percent);
});
updater.addListener('updateFailed', ({ bundle }) => {
console.error('Update failed', bundle.version);
});
实用建议
bun run build
bunx @capgo/cli@latest bundle upload --channel=production
始终调用
- rollback保护知道包是健康的
notifyAppReady()尽早在渲染器中执行以确保回滚保护正常工作。 - 保持内置路径稳定,让更新器决定是否加载预置的包或下载的包。
- 在 Electron 应用程序共享后端发布管道时,重用相同的 Capgo 通道和发布模型。
继续使用 @capgo/electron-updater
如果您正在使用 使用 @capgo/electron-updater 计划原生插件工作时,连接它与 @__CAPGO_KEEP_0__/electron-updater 在 @capgo/electron-updater 中了解实现细节 为 @capgo/electron-updater 的实现细节 在 Electron Updater 中了解实现细节 __CAPGO_KEEP_0__ 插件目录 Capgo 插件目录 为 Capgo 产品工作流程在插件目录中 Capacitor 插件按 Capgo 排列 为 Capacitor 插件按 Capgo 排列的实现细节, 添加或更新插件 添加或更新插件的实现细节。