Electron 更新器入门
复制一个包含安装步骤和本插件的完整Markdown指南的设置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/electron-updater`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/electron-updater/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
本指南将指导您设置 @capgo/electron-updater 在 Electron 应用程序中启用实时 JavaScript/HTML/CSS 更新。
先决条件
标题:先决条件- Electron 20.0.0 或更高版本
- Node.js 18 或更高版本
- 一个 Capgo 账户(注册在 capgo.app)
安装
标题:安装您可以使用我们的 AI-Assisted Setup 安装插件。将 Capgo 技能添加到您的 AI 工具中,使用以下命令:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/electron-updater` plugin in my project.如果您prefer Manual Setup,安装插件,运行以下命令并遵循以下平台特定的说明:
-
安装包:
终端窗口 bun add @capgo/electron-updater -
从Capgo控制台获取您的App ID。如果您还没有创建应用程序,请运行:
终端窗口 npx @capgo/cli@latest init
设置
标题:设置Electron更新器需要在三个地方进行设置:主进程、预加载脚本和渲染器进程。
主进程
标题:主进程import { app, BrowserWindow } from 'electron';import * as path from 'path';import { ElectronUpdater, setupIPCHandlers, setupEventForwarding,} from '@capgo/electron-updater';
// Create updater instance with your Capgo App IDconst updater = new ElectronUpdater({ appId: 'YOUR_CAPGO_APP_ID', // e.g., 'com.example.myapp' autoUpdate: true,});
app.whenReady().then(async () => { const mainWindow = new BrowserWindow({ width: 1200, height: 800, webPreferences: { preload: path.join(__dirname, 'preload.js'), contextIsolation: true, }, });
// Initialize updater with window and builtin path const builtinPath = path.join(__dirname, 'www/index.html'); await updater.initialize(mainWindow, builtinPath);
// Setup IPC communication between main and renderer setupIPCHandlers(updater); setupEventForwarding(updater, mainWindow);
// Load the current bundle (either builtin or downloaded update) await mainWindow.loadFile(updater.getCurrentBundlePath());});
app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); }});预加载脚本
预加载脚本import { exposeUpdaterAPI } from '@capgo/electron-updater/preload';
// Expose the updater API to the renderer processexposeUpdaterAPI();渲染进程
预加载脚本// renderer.ts (or in your app's entry point)import { requireUpdater } from '@capgo/electron-updater/renderer';
const updater = requireUpdater();
// CRITICAL: Call this on every app launch!// This confirms the bundle loaded successfully and prevents rollbackawait updater.notifyAppReady();
console.log('App ready, current bundle:', await updater.current());检查更新
标题:检查更新自动检查更新, autoUpdate: true复制到剪贴板
// Check for updates manuallyconst latest = await updater.getLatest();
if (latest.url && !latest.error) { console.log('Update available:', latest.version);
// Download the update const bundle = await updater.download({ url: latest.url, version: latest.version, checksum: latest.checksum, });
console.log('Downloaded bundle:', bundle.id);
// Option 1: Queue for next restart await updater.next({ id: bundle.id });
// Option 2: Apply immediately and reload // await updater.set({ id: bundle.id });}标题:监听事件
通过事件跟踪更新进度和状态:复制到剪贴板
// Download progressupdater.addListener('download', (event) => { console.log(`Download progress: ${event.percent}%`);});
// Update availableupdater.addListener('updateAvailable', (event) => { console.log('New version available:', event.bundle.version);});
// Download completedupdater.addListener('downloadComplete', (event) => { console.log('Download finished:', event.bundle.id);});
// Update failedupdater.addListener('updateFailed', (event) => { console.error('Update failed:', event.bundle.version);});使用 Capgo CLI 上传更新:
# Build your appnpm run build
# Upload to Capgonpx @capgo/cli@latest bundle upload --channel=production您的 Electron 应用程序将自动检测并下载下一个检查时的新捆绑包。
调试菜单
标题为“调试菜单”的部分在开发期间启用调试菜单:
const updater = new ElectronUpdater({ appId: 'YOUR_CAPGO_APP_ID', debugMenu: true, // Enable debug menu});按 Ctrl+Shift+D (或 Cmd+Shift+D 在 Mac 上)打开调试菜单并:
- 查看当前捆绑包信息
- 切换可用包
- 重置为内置版本
- 查看设备和频道信息
配置选项
配置选项const updater = new ElectronUpdater({ // Required appId: 'com.example.app',
// Server URLs (defaults to Capgo Cloud) updateUrl: 'https://plugin.capgo.app/updates', channelUrl: 'https://plugin.capgo.app/channel_self', statsUrl: 'https://plugin.capgo.app/stats',
// Behavior autoUpdate: true, // Enable auto-updates appReadyTimeout: 10000, // MS before rollback (default: 10s) autoDeleteFailed: true, // Delete failed bundles autoDeletePrevious: true, // Delete old bundles after successful update
// Channels defaultChannel: 'production',
// Security publicKey: '...', // For end-to-end encryption
// Debug debugMenu: false, // Enable debug menu disableJSLogging: false, // Disable console logs
// Periodic Updates periodCheckDelay: 0, // Seconds between checks (0 = disabled, min 600)});下一步
下一步从 Electron Updater 入门继续
标题:从 Electron Updater 入门继续如果您正在使用 Electron Updater 入门 来规划原生插件工作,连接它 使用 @capgo/electron-updater 在使用 @capgo/electron-updater 的原生能力 中Capgo 插件目录 中Capgo 插件目录中的产品工作流 由Capgo 创建的Capacitor 插件 for the implementation detail in Capacitor Plugins by Capgo, 添加或更新插件 对于在添加或更新插件中实现的细节 Ionic 企业插件替代品 对于 Ionic 企业插件替代品中的产品工作流程