当前位置: 首页 > 文档资料 > Electron 中文文档 >

更新应用程序

优质
小牛编辑
162浏览
2023-12-01

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

Updating Applications

There are several ways to update an Electron application. The easiest and officially supported one is taking advantage of the built-in Squirrel framework and Electron's autoUpdater module.

使用 update.electronjs.org

GitHub 的 Electron 团队维护 update.electronjs.org,一个免费开源的网络服务,可以让 Electron 应用使用自动更新。 这个服务是设计给那些满足以下标准的 Electron 应用:

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

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

安装模块

npm install update-electron-app

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

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

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

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

Using update.electronjs.org

GitHub's Electron team maintains update.electronjs.org, a free and open-source webservice that Electron apps can use to self-update. The service is designed for Electron apps that meet the following criteria:

  • App runs on macOS or Windows
  • App has a public GitHub repository
  • Builds are published to GitHub Releases
  • Builds are code-signed

The easiest way to use this service is by installing update-electron-app, a Node.js module preconfigured for use with update.electronjs.org.

Install the module:

npm install update-electron-app

Invoke the updater from your app's main process file:

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

By default, this module will check for updates at app startup, then every ten minutes. When an update is found, it will automatically be downloaded in the background. When the download completes, a dialog is displayed allowing the user to restart the app.

If you need to customize your configuration, you can pass options to update-electron-app or use the update service directly.

使用 electron-builder

如果你的app是通过electron-builder打包那么你可以使用electron-updater模块, 它不依赖任何服务器并且可以从S3, GitHub或者任何其它静态文件存储更新. 这避开了 Electron 内置的更新机制,这意味着本文档的其余部分不适用于此 electron-builder的更新程序。

Using electron-builder

If your app is packaged with electron-builder you can use the electron-updater module, which does not require a server and allows for updates from S3, GitHub or any other static file host. This sidesteps Electron's built-in update mechanism, meaning that the rest of this documentation will not apply to electron-builder's updater.

部署更新服务器

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

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

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

Deploying an Update Server

If you're developing a private Electron application, or if you're not publishing releases to GitHub Releases, it may be necessary to run your own update server.

Depending on your needs, you can choose from one of these:

  • Hazel – Update server for private or open-source apps which can be deployed for free on Now. It pulls from GitHub Releases and leverages the power of GitHub's CDN.
  • Nuts – Also uses GitHub Releases, but caches app updates on disk and supports private repositories.
  • electron-release-server – Provides a dashboard for handling releases and does not require releases to originate on GitHub.
  • Nucleus – A complete update server for Electron apps maintained by Atlassian. Supports multiple applications and channels; uses a static file store to minify server cost.

在你的应用中实施更新

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

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

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

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

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

最后一步, 检查更新. 下面的例子将每分钟检查一次:

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

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

Implementing Updates in Your App

Once you've deployed your update server, continue with importing the required modules in your code. The following code might vary for different server software, but it works like described when using Hazel.

Important: Please ensure that the code below will only be executed in your packaged app, and not in development. You can use electron-is-dev to check for the environment.

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

Next, construct the URL of the update server and tell autoUpdater about it:

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

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

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

Once your application is packaged, it will receive an update for each new GitHub Release that you publish.

应用更新

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

autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
  const dialogOpts = {
    type: 'info',
    buttons: ['Restart', 'Later'],
    title: 'Application Update',
    message: process.platform === 'win32' ? releaseNotes : releaseName,
    detail: 'A new version has been downloaded. Restart the application to apply the updates.'
  }
  dialog.showMessageBox(dialogOpts, (response) => {
    if (response === 0) autoUpdater.quitAndInstall()
  })
})

同时要确保错误被being handled. 这是一个例子它将记录到stderr:

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

Applying Updates

Now that you've configured the basic update mechanism for your application, you need to ensure that the user will get notified when there's an update. This can be achieved using the autoUpdater API events:

autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
  const dialogOpts = {
    type: 'info',
    buttons: ['Restart', 'Later'],
    title: 'Application Update',
    message: process.platform === 'win32' ? releaseNotes : releaseName,
    detail: 'A new version has been downloaded. Restart the application to apply the updates.'
  }
  dialog.showMessageBox(dialogOpts, (response) => {
    if (response === 0) autoUpdater.quitAndInstall()
  })
})

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

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