Skip to content

Electron Updaterのチュートリアル

GitHub

このガイドでは、ElectronアプリケーションにライブのJavaScript/HTML/CSS更新を有効にするために設定方法を説明します。 @capgo/electron-updater Electronアプリケーションで使用するように設定します。

前提条件

前提条件
  • Electron 20.0.0 またはそれ以上
  • Node.js 18 またはそれ以上
  • Capgoアカウント ( capgo.app)

インストール

インストール

AI-Assisted セットアップを使用してプラグインをインストールすることができます。次のコマンドを使用して、AI ツールに Capgo スキルを追加します。

ターミナル画面
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.

Manual Setup を使用する場合は、以下のコマンドを実行してプラグインをインストールし、以下のプラットフォーム固有の指示に従ってください。

  1. パッケージをインストール:

    ターミナル画面
    bun add @capgo/electron-updater
  2. App ID を Capgo ダッシュボードから取得します。アプリを作成していない場合は、以下のコマンドを実行してください。

    ターミナル画面
    npx @capgo/cli@latest init

セットアップ

セットアップ

Electronのアップデートには、main process、preload script、renderer processの3つの場所でセットアップが必要です。

メインプロセス

メインプロセス
main.ts
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 ID
const 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();
}
});

プリロードスクリプト

プリロードスクリプト
preload.ts
import { exposeUpdaterAPI } from '@capgo/electron-updater/preload';
// Expose the updater API to the renderer process
exposeUpdaterAPI();

レンダラー プロセス

レンダラー プロセス
// 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 rollback
await updater.notifyAppReady();
console.log('App ready, current bundle:', await updater.current());

更新を確認する

更新を確認する

With autoUpdate: true, the updater automatically checks for updates. You can also trigger manual checks:

// Check for updates manually
const 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 progress
updater.addListener('download', (event) => {
console.log(`Download progress: ${event.percent}%`);
});
// Update available
updater.addListener('updateAvailable', (event) => {
console.log('New version available:', event.bundle.version);
});
// Download completed
updater.addListener('downloadComplete', (event) => {
console.log('Download finished:', event.bundle.id);
});
// Update failed
updater.addListener('updateFailed', (event) => {
console.error('Update failed:', event.bundle.version);
});

クリップボードにコピー

更新をデプロイ

Use the Capgo CLI to upload updates:

__CAPGO_KEEP_0__ __CAPGO_KEEP_1__ を使用して、更新をアップロードします。
# Build your app
npm run build
# Upload to Capgo
npx @capgo/cli@latest bundle upload --channel=production

Your Electron app will automatically detect and download the new bundle on next check.

開発中はデバッグ メニューを有効にします:

const updater = new ElectronUpdater({
appId: 'YOUR_CAPGO_APP_ID',
debugMenu: true, // Enable debug menu
});

Press Ctrl+Shift+D (または Cmd+Shift+D Macの場合)デバッグ メニューを開き、以下の操作を行います:

  • 現在のバンドル情報を表示
  • 利用可能なバンドル間で切り替え
  • バイトインバウンドバージョンにリセット
  • デバイスとチャンネル情報を表示

[__CAPGO_KEEP_0__] 設定オプション

セクション:設定オプション
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のGetting Startedから続けてください

「Electron Updater」で始めてから続ける

Capgoを使用している場合 Electron Updaterで始める ネイティブプラグインの作業計画に使用する場合、Capgoと接続する ネイティブ機能の「@capgo/electron-updater」 「@capgo/electron-updater」で使用するネイティブ機能 Capgo プラグインディレクトリ Capgo プラグインディレクトリの製品ワークフロー Capacitor Plugins by Capgo for the implementation detail in Capacitor Plugins by Capgo, プラグインの追加または更新 プラグインの追加または更新の実装詳細 イオニック エンタープライズ プラグイン代替 イオニック エンタープライズ プラグインの製品ワークフロー用途.