更新应用程序

有多种方法可以更新Electron应用. 最简单并且获得官方支持的方法是利用内置的Squirrel框架和Electron的autoUpdater模块。

使用 update.electronjs.org

The Electron team maintains update.electronjs.org, a free and open-source webservice that Electron apps can use to self-update. 这个服务是设计给那些满足以下标准的 Electron 应用:

  • 应用运行在 macOS 或者 Windows
  • 应用有公开的 GitHub 仓库
  • 编译的版本发布在 GitHub Releases
  • 编译的版本已代码签名

使用这个服务最简单的方法是安装 update-electron-app,一个预配置好的 Node.js 模块来使用 update.electronjs.org。

安装模块

  1. npm install update-electron-app

从你的应用的 main process 文件调用这个更新:

  1. require('update-electron-app')()

默认情况下,这个模块会在应用启动的时候检查更新,然后每隔十分钟再检查一次。 当发现了一个更新,它会自动在后台下载。 当下载完成后,会显示一个对话框以允许用户重启应用。

如果你需要定制化你的配置,你可以 将配置设置传递给 update-electron-app 或者 直接使用更新服务

部署更新服务器

如果你开发的是一个私有的 Electron 应用程序,或者你没有在 GitHub Releases 中公开发布,你可能需要运行自己的更新服务器。

根据你的需要,你可以从下方选择:

  • Hazel – 用于私人或开源应用的更新服务器,可以在 Now 上免费部署。 它从GitHub Releases中拉取更新文件,并且利用 GitHub CDN 的强大性能。
  • Nuts-同样使用GitHub Releases, 但得在磁盘上缓存应用程序更新并支持私有存储库.
  • electron-release-server – 提供一个用于处理发布的仪表板,并且不需要在GitHub上发布发布。
  • Nucleus – 一个由Atlassian维护的 Electron 应用程序的完整更新服务器。 支持多种应用程序和渠道; 使用静态文件存储来降低服务器成本.

在你的应用中实施更新

一旦你部署了更新服务器, 继续导入你所需要的代码模块. 下列代码可能因不同的服务器软件而变化, but it works like described when using Hazel.

重要: 请确保下面的代码只在打包的应用程序, 而不是开发中. 你可以使用electron-is-dev检查当前环境.

  1. const { app, autoUpdater, dialog } = require('electron')

下一步, 构建更新服务器的URL并且通知autoUpdater:

  1. const server = 'https://your-deployment-url.com'
  2. const url = `${server}/update/${process.platform}/${app.getVersion()}`
  3. autoUpdater.setFeedURL({ url })

As the final step, check for updates. The example below will check every minute:

  1. setInterval(() => {
  2. autoUpdater.checkForUpdates()
  3. }, 60000)

应用程序被packaged后, 它将接收你每次发布在GitHub Release上的的更新。

应用更新

现在您已经为应用程序配置了基本的更新机制, 您需要确保在更新时通知用户. 这可以使用autoUpdater API events来实现:

  1. autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
  2. const dialogOpts = {
  3. type: 'info',
  4. buttons: ['Restart', 'Later'],
  5. title: 'Application Update',
  6. message: process.platform === 'win32' ? releaseNotes : releaseName,
  7. detail: 'A new version has been downloaded. Restart the application to apply the updates.'
  8. }
  9. dialog.showMessageBox(dialogOpts).then((returnValue) => {
  10. if (returnValue.response === 0) autoUpdater.quitAndInstall()
  11. })
  12. })

Also make sure that errors are being handled. Here’s an example for logging them to stderr:

  1. autoUpdater.on('error', message => {
  2. console.error('There was a problem updating the application')
  3. console.error(message)
  4. })

Handing Updates Manually

Because the requests made by Auto Update aren’t under your direct control, you may find situations that are difficult to handle (such as if the update server is behind authentication). The url field does support files, which means that with some effort, you can sidestep the server-communication aspect of the process. Here’s an example of how this could work.