跳过主要内容
返回插件
@capgo/electron-updater
教程
由 github.com/Cap-go

Electron Updater

API 为 Electron 应用程序提供 OTA 实时更新,具有相同的 capacitor-updater 表面

指南

Electron 更新器教程

使用 @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

实用建议

  • 始终在渲染器中调用 notifyAppReady() 早期调用以确保回滚保护正常工作。
  • 保持内置路径稳定,让更新器决定是否加载已打包的包或下载的包。
  • 在 Electron 应用程序共享后端发布管道时,重用相同的 Capgo 通道和发布模型。