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

电子更新器

electron 应用程序的 OTA 实时更新功能,具有相同的 API 表面和 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 通道和滚动模型。

继续使用 @capgo/electron-updater

如果您正在使用 使用 @capgo/electron-updater 为native插件工作做好准备,连接它与 @capgo/electron-updater 在 @capgo/electron-updater 中了解实现细节 Electron Updater 入门 在 Electron Updater 入门 中了解实现细节 Capgo 插件目录 在 Capgo 插件目录 中了解产品工作流程 Capacitor 由 Capgo 提供的插件 在 Capacitor 由 Capgo 提供的插件 中了解实现细节 添加或更新插件 为添加或更新插件提供实现细节。