preface

We’re working on a New Electron desktop app, and we hope that as soon as we release the latest version, users will get an update notification for a new pack. There is autoUpdater in ELECTRON, and this time we choose the third party plug-in of electron- Updater. Firstly, the disadvantage of autoUpdater installed with ELECTRON is that there is no way to control when to download it (I see no such function in the official document at present, please correct me if there is any), and I don’t know the download progress. However, the electron updater has this function. The biggest advantage of this function is that we can have multiple versions in the formal environment and monitor the download progress constantly.

electron-updaterInstructions for use of

1, the introduction of
$ yarn add electron-updater electron-log electron-is-dev
Copy the code
  • electron-updater Automatically update the required dependencies
  • electron-logUpdated logs can be stored locally
  • electron-is-devCheck whether electron is the DEV environment

2,electron-updaterA description of each event in
Event: error

Triggered when an update error occurs

Returns:

  • error Error
Event: checking-for-update

Triggered when checking for updates begins

Returns:

  • No return value
Event: update-available

Triggered when an update package is available

Returns:

 {
     "version": "0.1.0 from".// Latest version number
     "files": [{
         "url": Exe "text Setup 0.1.0 from.".// The package name
         "sha512": "xxx"."size": 899999562.// Package size}]."path": Exe "text Setup 0.1.0 from.".// The package name
     "sha512": "xxx"."relaseDate": "The 2019-11-07 T10: he. 286 z".// New package release time
     "relaseNotes": "Repair XXX".// New package release content
 }
Copy the code
Event: the update - not available

Triggered when no updates are detected

Returns:

  • No return value
Event: download-progress

Monitoring download progress

Returns:

{
    "delta":"22108084"./ / delta
    "bytesPerSecond": "20243532".// Download speed
    "percent": '73.32491979950667', // Percentage of downloads
    "total":"82879166".// Update the package size
    "transferred": "82879166" // The size of the downloaded package
}
Copy the code
Event: update-downloaded

Triggered when the update package is downloaded

The return value:

  {
      "version": "0.1.0 from".// Latest version number
      "files": [{
          "url": Exe "text Setup 0.1.0 from.".// The package name
          "sha512": "xxx"."size": 899999562.// Package size}]."path": Exe "text Setup 0.1.0 from.".// The package name
      "sha512": "xxx"."relaseDate": "The 2019-11-07 T10: he. 286 z".// New package release time
      "relaseNotes": "Repair XXX".// New package release content
  }
Copy the code

Methods:

autoUpdaterThere are the following methods

  • autoUpdater.checkForUpdates() => Promise<UpdateCheckResult>

    Asks the server if any updates are available

  • AutoUpdater. CheckForUpdatesAndNotify () ⇒ Promise < | UpdateCheckResult >

    Asks the server if there is an update, downloads it, and notifies the server if the update is available.

  • AutoUpdater. DownloadUpdate (cancellationToken) ⇒ Promise < any >

    Start manually downloading updates. This method false can be used if autoDownloadoption is set to

    Return: Promise

    – the path to download the file.

    • cancelToken CancellationToken
  • AutoUpdater. GetFeedURL () ⇒ undefined | null | String autoUpdater. SetFeedURL (options)

    Configure the update provider. If value is string, the value of GenericServerOptions is set to the URL.

    Option PublishConfiguration | String | GithubOptions | S3Options | SpacesOptions | GenericServerOptions | BintrayOptions- If you want to override the configuration app-update.yml.

  • AutoUpdater. Channel (getter and setter

    Use to define the channel that the auto-updater will follow (see using channels for auto-updater. channel = ‘beta’ or use to get the currentChannel currentChannel = autoupder.channel.

  • autoUpdater.quitAndInstall(isSilent, isForceRunAfter)

    After downloading the update, restart the application and install the update. You should call update-downloaded only after it has been issued.

    Note: autoUpdater. QuitAndInstall () will be the first to close all applications window, app sent before – the quit after event. This is different from the normal sequence of exit events.

    IsSilent Boolean- Only Windows runs setup in silent mode. The default is false.

    IsForceRunAfter – Boolean Run the application after completion even without prompting for installation. Does not apply to macOS. Ignore whether isSilent is set to false.


API

  • AutoDownload = true Boolean value – Whether updates are automatically downloaded after they are discovered.

  • AutoInstallOnAppQuit = true Boolean – Whether downloaded updates are automatically installed when the application exits (if quitAndInstall has not been called before).

    Windows and Linux only.

  • AllowPrerelease = false Boolean – * Only GitHub vendors. * Whether updates to pre-release versions are allowed. The default is true if the version of the application contains a pre-released component (for example, 0.12.1-alpha.1, where alpha is a pre-released component), or false otherwise.

    If true, a downgrade is allowed (allowDowngrade will be set to true).

  • FullChangelog = false Boolean – * Only GitHub vendors. * Get all release notes (from the current version to the latest version), not just the latest version.

  • AllowDowngrade = false Boolean – Whether to allow a version downgrade (when a user from a Beta channel wants to return to a stable channel).

    Only if the channel is different (in the case of semantic versions, pre-release components).

  • CurrentVersion SemVer- The current application version.

  • Channel string – Gets the update channel. Does not apply to GitHub. A channel is not returned from the update configuration, only if it is previously set.

  • RequestHeaders [key: string] : string – Request header.

  • Logger Logs You can pass electronic-log, Winston, or other logs through the following interface: {info(), WARN (), error()}. If you want to disable logging, set it to NULL.

  • Signals = new UpdaterSignal(this) UpdaterSignal- For type safety, you can use signals, For example autoUpdater. Signals. UpdateDownloaded (() = > {}) instead of autoUpdater. On (‘ update – available ‘, () = > {})


Automatic update example

  • inpackage.jsonThe configuration in
{..."build": {
    	"releaseInfo": {
          "releaseNotes": "Optimized my order, information base, role functions, added refund, refund order review" // The updated content
        },
		"publish": [{"provider": "generic"."url": "http://127.0.0.1:8080/download/".// Push the address
            "channel": "latest" // Check the channels for updates}]}... }Copy the code
  • Add in the following directoryApp - updater. Yml and dev - app - updater. YmlFile is used to check the configuration of the update file
Yml = app-update.yml = app-update.yml = app-update.yml = app-update.yml
provider: 'generic'
url: 'http://127.0.0.1:8080/download/'
channel: 'latest'
Yml = dev-app-update.yml = dev-app-update.yml
provider: 'generic'
url: 'http://127.0.0.1:8080/test-download/'
channel: 'latest'
Copy the code
  • inAPPAutoUpdater.jsIn the
const { autoUpdater } = require('electron-updater')
const Store = require('electron-store')
const path = require('path')
global.store = new Store()
autoUpdater.logger = require('electron-log')
// Listen for output logs
autoUpdater.logger.transports.file.level = 'info'
// Set the current version to the automatically updated version
global.currentVersion = autoUpdater.currentVersion
autoUpdater.autoDownload = false // Set the automatic download package to false. The product requirement is that the user click update to download the package
// Listen for several events that update automatically
// Listening will stop installation if automatic update fails
autoUpdater.on('error'.() = > {
  // todo something
})
// Check if the update has started when issued
autoUpdater.on('checking-for-update'.() = > {})
// Check that there are updatable application packages
autoUpdater.on('update-available'.info= > {
  // todo something
})
// Emitted when no updates are available
autoUpdater.on('update-not-available'.info= > {
  // todo something
})
// Download the updatable installation package
autoUpdater.on('update-downloaded'.info= > {
 // todo something
})
// Monitor the download progress
autoUpdater.on('download-progress'.info= > {
  // todo something
})
module.exports = {
  checkVersion() {
    if (global.isDev) {
      autoUpdater.updateConfigPath = path.join(__dirname, '.. /.. /dev-app-update.yml')
    }
    autoUpdater.checkForUpdates()
  }
}
Copy the code
  • In the main process of electron index.js

// Check for updates as soon as the main page is loaded
mainWindow.webContents.on('did-finish-load'.() = > {
    AppUpdater.checkVersion()
})
Copy the code