跳过主要内容

电子更新器 电子插件

OTA live updates for Electron apps with the same API surface as capacitor-updater

安装

bun add @capgo/electron-updater
npm、pnpm 或 yarn
  • npm install @capgo/electron-updater
  • pnpm add @capgo/electron-updater
  • yarn add @capgo/electron-updater

指南

如何在 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 排列的实现细节, 添加或更新插件 添加或更新插件的实现细节。

常见问题

Electron Updater 插件常见问题

如何在 Electron 应用程序中安装 Electron Updater 插件?

运行 "bun add @capgo/electron-updater" (或 "npm install @capgo/electron-updater"), 然后按照 Electron 设置步骤在指南中并阅读 GitHub README。

@capgo/electron-updater 与 React、Vue 和 Angular 是否兼容?

是的。 Electron Updater 是一个 Electron 包,因此它与任何运行在 Electron 内的 web 框架,包括 React、Vue、Angular、Svelte 和普通 JavaScript,兼容。

@capgo/electron-updater 是否免费和开源?

是的。 code 的源代码在 GitHub 上公开,https://github.com/Cap-go/electron-updater/,并且包是从 npm 免费安装的。 欢迎 bug 报告和 pull 请求。

不等待应用审核即将 Electron 更新器的更改

一旦插件在您的商店构建中,Capgo即刻更新将推送您的JavaScript、HTML和CSS更改到用户中。

从 Capgo开始