Skip to content
On this page

electron-updater

一个用于 Electron 应用程序自动更新的解决方案

安装

bash
pnpm add electron-updater

基础配置

在主进程中配置:

ts
import { app } from 'electron'
import { autoUpdater } from 'electron-updater'

// 基本配置
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = true

// 检查更新
autoUpdater.checkForUpdates()

// 更新事件监听
autoUpdater.on('checking-for-update', () => {
  console.log('正在检查更新...')
})

autoUpdater.on('update-available', (info) => {
  console.log('有可用的更新:', info)
})

autoUpdater.on('update-not-available', (info) => {
  console.log('当前已是最新版本:', info)
})

autoUpdater.on('error', (err) => {
  console.error('更新出错:', err)
})

使用示例

基本更新流程

ts
import { app, dialog } from 'electron'
import { autoUpdater } from 'electron-updater'

// 检查更新
const checkForUpdates = async () => {
  try {
    const result = await autoUpdater.checkForUpdates()
    return result.updateInfo
  } catch (error) {
    console.error('检查更新失败:', error)
    return null
  }
}

// 下载更新
const downloadUpdate = async () => {
  try {
    await autoUpdater.downloadUpdate()
    return true
  } catch (error) {
    console.error('下载更新失败:', error)
    return false
  }
}

// 安装更新
const installUpdate = () => {
  autoUpdater.quitAndInstall()
}

// 更新流程示例
autoUpdater.on('update-available', async (info) => {
  const { response } = await dialog.showMessageBox({
    type: 'info',
    title: '发现新版本',
    message: `发现新版本 ${info.version},是否更新?`,
    buttons: ['更新', '取消'],
  })

  if (response === 0) {
    await downloadUpdate()
  }
})

autoUpdater.on('update-downloaded', async () => {
  const { response } = await dialog.showMessageBox({
    type: 'info',
    title: '更新就绪',
    message: '更新已下载,重启应用以安装更新?',
    buttons: ['重启', '稍后'],
  })

  if (response === 0) {
    installUpdate()
  }
})

高级配置

ts
import { app } from 'electron'
import { autoUpdater, UpdateInfo } from 'electron-updater'
import log from 'electron-log'

// 日志配置
autoUpdater.logger = log
log.transports.file.level = 'debug'

// 自定义更新配置
autoUpdater.setFeedURL({
  provider: 'github',
  owner: 'your-username',
  repo: 'your-repo',
  private: true,
  token: process.env.GH_TOKEN,
})

// 自定义下载配置
autoUpdater.requestHeaders = {
  'Cache-Control': 'no-cache',
}

autoUpdater.channel = 'beta' // 更新通道
autoUpdater.allowDowngrade = false // 禁止降级
autoUpdater.allowPrerelease = false // 禁止预发布版本

// 自定义更新检查间隔
let updateCheckInterval: NodeJS.Timeout

const startAutoUpdateCheck = () => {
  updateCheckInterval = setInterval(() => {
    autoUpdater.checkForUpdates()
  }, 1000 * 60 * 60) // 每小时检查一次
}

app.on('ready', () => {
  startAutoUpdateCheck()
})

app.on('window-all-closed', () => {
  if (updateCheckInterval) {
    clearInterval(updateCheckInterval)
  }
})

// 自定义更新进度处理
autoUpdater.on('download-progress', (progressObj) => {
  const { bytesPerSecond, percent, transferred, total } = progressObj
  log.info(`下载速度: ${bytesPerSecond}`)
  log.info(`当前进度: ${percent}%`)
  log.info(`已下载: ${transferred}/${total} bytes`)

  // 发送进度到渲染进程
  mainWindow?.webContents.send('update-progress', progressObj)
})

要保持清醒 永远不抱有意外的幻想 凭空的期待最要命