Skip to content

Commit

Permalink
feat(electron-updater): follow autoInstallOnAppQuit = false on macOS (
Browse files Browse the repository at this point in the history
#5271)

Make electron-updater on macOS behave more like other OS's by only exposing the downloaded update file to Squirrel.Mac on applying it. This circumvents Squirrel.Mac limitation of not being able to NOT apply an update after it (squirrel) had downloaded it.
  • Loading branch information
MortalKastor authored Feb 16, 2021
1 parent 0360cd1 commit 1643d56
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 0 additions & 2 deletions packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export abstract class AppUpdater extends EventEmitter {

/**
* Whether to automatically install a downloaded update on app quit (if `quitAndInstall` was not called before).
*
* Applicable only on Windows and Linux.
*/
autoInstallOnAppQuit: boolean = true

Expand Down
33 changes: 23 additions & 10 deletions packages/electron-updater/src/MacUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { createServer, IncomingMessage, ServerResponse } from "http"
import { AddressInfo } from "net"
import { AppAdapter } from "./AppAdapter"
import { AppUpdater, DownloadUpdateOptions } from "./AppUpdater"
import { ResolvedUpdateFileInfo, UpdateDownloadedEvent } from "./main"
import { ResolvedUpdateFileInfo } from "./main"
import { findFile } from "./providers/Provider"
import AutoUpdater = Electron.AutoUpdater

export class MacUpdater extends AppUpdater {
private readonly nativeUpdater: AutoUpdater = require("electron").autoUpdater

private updateInfoForPendingUpdateDownloadedEvent: UpdateDownloadedEvent | null = null
private squirrelDownloadedUpdate: boolean = false

constructor(options?: AllPublishOptions, app?: AppAdapter) {
super(options, app)
Expand All @@ -22,15 +22,11 @@ export class MacUpdater extends AppUpdater {
this.emit("error", it)
})
this.nativeUpdater.on("update-downloaded", () => {
const updateInfo = this.updateInfoForPendingUpdateDownloadedEvent
this.updateInfoForPendingUpdateDownloadedEvent = null
this.dispatchUpdateDownloaded(updateInfo!!)
this.squirrelDownloadedUpdate = true
})
}

protected doDownloadUpdate(downloadUpdateOptions: DownloadUpdateOptions): Promise<Array<string>> {
this.updateInfoForPendingUpdateDownloadedEvent = null

let files = downloadUpdateOptions.updateInfoAndProvider.provider.resolveFiles(downloadUpdateOptions.updateInfoAndProvider.info);

// Allow arm64 macs to install universal or rosetta2(x64) - https://github.com/electron-userland/electron-builder/pull/5524
Expand Down Expand Up @@ -63,7 +59,6 @@ export class MacUpdater extends AppUpdater {
},
done: async event => {
const downloadedFile = event.downloadedFile
this.updateInfoForPendingUpdateDownloadedEvent = event
let updateFileSize = zipFileInfo.info.size
if (updateFileSize == null) {
updateFileSize = (await stat(downloadedFile)).size
Expand Down Expand Up @@ -130,14 +125,32 @@ export class MacUpdater extends AppUpdater {
})

this.nativeUpdater.once("error", reject)
this.nativeUpdater.checkForUpdates()

// The update has been dowloaded and is ready to be served to Squirrel
this.dispatchUpdateDownloaded(event)

if (this.autoInstallOnAppQuit) {
// This will trigger fetching and installing the file on Squirrel side
this.nativeUpdater.checkForUpdates()
}
})
})
}
})
}

quitAndInstall(): void {
this.nativeUpdater.quitAndInstall()
if (this.squirrelDownloadedUpdate) {
// Update already fetched by Squirrel, it's ready to install
this.nativeUpdater.quitAndInstall()
} else {
// Quit and install as soon as Squirrel get the update
this.nativeUpdater.on("update-downloaded", () => {
this.nativeUpdater.quitAndInstall()
})

// And trigger the update
this.nativeUpdater.checkForUpdates()
}
}
}

0 comments on commit 1643d56

Please sign in to comment.