Skip to content

Commit

Permalink
feat(nsis-auto-updater): auto install on quit
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Oct 29, 2016
1 parent 772b297 commit a3bba92
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion nsis-auto-updater/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-auto-updater",
"version": "0.2.0",
"version": "0.3.1",
"description": "NSIS Auto Updater",
"main": "out/nsis-auto-updater/src/main.js",
"author": "Vladimir Krivosheev",
Expand Down
29 changes: 25 additions & 4 deletions nsis-auto-updater/src/NsisUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class NsisUpdater extends EventEmitter {

private readonly app: any

private quitHandlerAdded = false

constructor(options?: PublishConfiguration | BintrayOptions | GithubOptions) {
super()

Expand Down Expand Up @@ -68,7 +70,7 @@ export class NsisUpdater extends EventEmitter {
throw new Error(`App version is not valid semver version: "${currentVersion}`)
}

if (isVersionGreaterThan(currentVersion, latestVersion)) {
if (!isVersionGreaterThan(latestVersion, currentVersion)) {
this.updateAvailable = false
this.emit("update-not-available")
return {
Expand All @@ -89,6 +91,7 @@ export class NsisUpdater extends EventEmitter {
.then(it => download(fileInfo.url, path.join(it, fileInfo.name)))
.then(it => {
this.setupPath = it
this.addQuitHandler()
this.emit("update-downloaded", {}, null, versionInfo.version, null, null, () => {
this.quitAndInstall()
})
Expand All @@ -98,15 +101,33 @@ export class NsisUpdater extends EventEmitter {
}
}

private addQuitHandler() {
if (this.quitHandlerAdded) {
return
}

this.quitHandlerAdded = true

this.app.on("quit", () => {
this.install()
})
}

quitAndInstall(): void {
if (this.install()) {
this.app.quit()
}
}

private install(): boolean {
if (this.quitAndInstallCalled) {
return
return false
}

const setupPath = this.setupPath
if (!this.updateAvailable || setupPath == null) {
this.emitError("No update available, can't quit and install")
return
return false
}

// prevent calling several times
Expand All @@ -117,7 +138,7 @@ export class NsisUpdater extends EventEmitter {
stdio: "ignore",
}).unref()

this.app.quit()
return true
}

// emit both error object and message, this is to keep compatibility with old APIs
Expand Down

0 comments on commit a3bba92

Please sign in to comment.