跳过内容

Electron Updater API 参考

GitHub

本页面记录了Electron Updater可用的所有方法、事件和配置选项。

notifyAppReady()

notifyAppReady()

每次应用启动时都必须调用。 确认 bundle 加载成功,防止自动回滚。

await updater.notifyAppReady();

从 URL 下载一个 bundle。

const bundle = await updater.download({
url: 'https://example.com/bundle.zip',
version: '1.0.1',
checksum: 'sha256-hash', // Optional but recommended
sessionKey: '...', // For encrypted bundles
});

参数:

选项类型必填描述
urlstring下载包的 URL
versionstring包的版本标识符
checksumstring没有用于验证的SHA256校验和
sessionKey字符串没有用于加密包的会话密钥

返回值: BundleInfo 包含 id, version, status

在下一次应用重启时加载包的队列。

await updater.next({ id: 'bundle-id' });

参数:

选项类型必填描述
idstring队列的 Bundle ID

立即切换到一个 Bundle 并重新加载应用程序。

await updater.set({ id: 'bundle-id' });

参数:

选项类型必填描述
idstring用于激活的 Bundle ID

手动使用当前 Bundle 重载应用

await updater.reload();

删除一个存储中的包。

await updater.delete({ id: 'bundle-id' });

参数:

选项类型必填描述
idstring要删除的包ID

reset(options)

__CAPGO_KEEP_0__

重置到内置版本或最后一次成功打包

// Reset to builtin
await updater.reset({ toLastSuccessful: false });
// Reset to last successful bundle
await updater.reset({ toLastSuccessful: true });

参数:

选项类型必填描述
toLastSuccessful布尔值如果为真,重置到最后一次成功打包而不是内置

获取当前 Bundle 和本机版本的信息。

const info = await updater.current();
// { bundle: { id, version, status }, native: '1.0.0' }

列出所有下载的 Bundle。

const bundles = await updater.list();
// [{ id, version, status, downloaded, checksum }, ...]

获取下一次重启时的 Bundle。

const next = await updater.getNextBundle();
// { id, version, status } or null

获取有关上一次失败的更新的信息(用于调试回滚)。

const failed = await updater.getFailedUpdate();
// { id, version, reason } or null

getBuiltinVersion()

标题:getBuiltinVersion()

获取与应用程序二进制文件一起发布的版本号。

const version = await updater.getBuiltinVersion();
// '1.0.0'

getLatest(options)

获取最新(options)

检查服务器获取最新可用版本

const latest = await updater.getLatest();
if (latest.url && !latest.error) {
// Update available
console.log('New version:', latest.version);
console.log('Download URL:', latest.url);
} else if (latest.error) {
console.error('Error checking updates:', latest.error);
}

返回:

属性类型描述
urlstring下载 URL(如果没有更新则为空)
versionstring可用版本
checksumstringSHA256 checksum
sessionKey字符串加密会话密钥
error字符串校验失败时的错误消息
message字符串服务器消息

频道管理

频道管理

设置设备到特定频道

设置设备到特定频道

将设备分配到特定频道。

await updater.setChannel({ channel: 'beta' });

unsetChannel(options)

标题:unsetChannel(options)

移除通道分配并使用默认值。

await updater.unsetChannel();

获取当前通道分配。

const channel = await updater.getChannel();
// { channel: 'production', status: 'set' }

列出此应用程序的所有可用通道。

const channels = await updater.listChannels();
// ['production', 'beta', 'staging']

控制下载的更新何时应用。

setMultiDelay(options)

标题:setMultiDelay(options)

设置必须满足的条件才能应用更新。

// Wait for app to be backgrounded
await updater.setMultiDelay({
delayConditions: [{ kind: 'background' }]
});
// Wait until specific date
await updater.setMultiDelay({
delayConditions: [{ kind: 'date', value: '2024-12-25T00:00:00Z' }]
});
// Wait for app to be killed and restarted
await updater.setMultiDelay({
delayConditions: [{ kind: 'kill' }]
});
// Multiple conditions (all must be met)
await updater.setMultiDelay({
delayConditions: [
{ kind: 'background' },
{ kind: 'date', value: '2024-12-25T00:00:00Z' }
]
});

延迟条件类型:

类型描述
background可选持续时间 (ms)等待应用程序进入后台
kill-等待应用程序被杀死并重启
dateISO 日期字符串等待特定日期/时间
nativeVersion版本字符串等待原生应用程序更新

cancelDelay()

取消延迟()

清除所有延迟条件并在下一次检查时立即应用更新。

await updater.cancelDelay();

获取设备唯一标识符。

const deviceId = await updater.getDeviceId();
// 'uuid-xxxx-xxxx-xxxx'

为设备设置自定义标识符(用于分析)。

await updater.setCustomId({ customId: 'user-123' });

在运行时更改更新服务器URL。

await updater.setUpdateUrl({ url: 'https://my-server.com/updates' });

Section titled “setStatsUrl(options)”

在运行时更改统计报告URL。

复制到剪贴板

await updater.setStatsUrl({ url: 'https://my-server.com/stats' });

修改频道管理 URL

await updater.setChannelUrl({ url: 'https://my-server.com/channel' });

在运行时更改 App ID

await updater.setAppId({ appId: 'com.example.newapp' });

获取当前 App ID。

const appId = await updater.getAppId();

启用或禁用调试菜单。

await updater.setDebugMenu({ enabled: true });

检查是否启用了调试菜单。

const enabled = await updater.isDebugMenuEnabled();

使用 addListener:

updater.addListener('eventName', (event) => {
// Handle event
});
事件载荷描述
download{ percent, status }下载进度更新
updateAvailable{ bundle }新更新可用
noNeedUpdate{ message }已经是最新版本
downloadComplete{ bundle }下载成功
downloadFailed{ bundle, error }下载失败
breakingAvailable{ bundle }不兼容的更新可用(需要原生更新)
updateFailed{ bundle, reason }更新安装失败
appReloaded{}应用已重新加载
appReady{}notifyAppReady() 被调用

示例:全事件处理

标题:示例:全事件处理
// Progress tracking
updater.addListener('download', (event) => {
updateProgressBar(event.percent);
});
// Update available notification
updater.addListener('updateAvailable', (event) => {
showNotification(`Update ${event.bundle.version} available!`);
});
// Handle completion
updater.addListener('downloadComplete', async (event) => {
// Queue for next restart
await updater.next({ id: event.bundle.id });
showNotification('Update will apply on next restart');
});
// Handle failures
updater.addListener('updateFailed', (event) => {
console.error('Update failed:', event.reason);
reportError(event);
});

构造函数选项

标题:构造函数选项

全配置选项 ElectronUpdater:

const updater = new ElectronUpdater({
// Required
appId: 'com.example.app',
// Version override
version: '1.0.0', // Override builtin version detection
// Server URLs
updateUrl: 'https://plugin.capgo.app/updates',
channelUrl: 'https://plugin.capgo.app/channel_self',
statsUrl: 'https://plugin.capgo.app/stats',
// Behavior
autoUpdate: true, // Enable automatic update checks
appReadyTimeout: 10000, // Milliseconds before rollback (default: 10000)
autoDeleteFailed: true, // Auto-delete failed bundles
autoDeletePrevious: true, // Auto-delete old bundles
resetWhenUpdate: true, // Reset to builtin on native update
// Channels
defaultChannel: 'production',
// Direct Update Mode
directUpdate: false, // 'atInstall' | 'onLaunch' | 'always' | false
// Security
publicKey: '...', // RSA public key for E2E encryption
// Dynamic Configuration
allowModifyUrl: false, // Allow runtime URL changes
allowModifyAppId: false, // Allow runtime App ID changes
persistCustomId: false, // Persist custom ID across updates
persistModifyUrl: false, // Persist URL changes
// Debug
debugMenu: false, // Enable debug menu (Ctrl+Shift+D)
disableJSLogging: false, // Disable console logs
// Periodic Updates
periodCheckDelay: 0, // Seconds between auto-checks (0 = disabled, min 600)
});

从 Electron Updater API 开始继续

标题:从 Electron Updater API 开始继续

如果您正在使用 Electron Updater API 开始继续 来规划仪表板和 API 操作,连接它到 使用 @capgo/electron-updater 为使用 @capgo/electron-updater 的本机能力 API 概述 为 API 概述 的实现细节 简介 在介绍中实现细节 API 键 在 API 键中实现细节,以及 设备 在设备中实现细节。