Skip to content

Commit 7c2973d

Browse files
committed
feat(electron-updater): cannot use updater without administrator privileges
Close #1133
1 parent 93b4d59 commit 7c2973d

File tree

5 files changed

+80
-26
lines changed

5 files changed

+80
-26
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ and other distributable formats.
160160

161161
[Donate with PayPal.](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W6V79R2RGCCHL)
162162

163+
## Community
164+
165+
[electron-builder](https://electron-builder.slack.com/shared_invite/MTMzMTc5NTcyNTAzLTE0ODUzNzE4MTctYzBhNGY1NjljYg) on Slack. Please use [threads](https://get.slack.help/hc/en-us/articles/115000769927-Message-threads).
166+
163167
## Further Reading
164168
See the [Wiki](https://github.com/electron-userland/electron-builder/wiki) for more documentation.
165169

packages/electron-builder/src/targets/nsis.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import { unlink, readFile } from "fs-extra-p"
1111
import { NsisOptions } from "../options/winOptions"
1212
import { Target, Arch } from "electron-builder-core"
1313
import sanitizeFileName from "sanitize-filename"
14+
import { copyFile } from "electron-builder-util/out/fs"
1415

15-
const NSIS_VERSION = "3.0.4"
16+
const NSIS_VERSION = "3.0.1.5"
1617
//noinspection SpellCheckingInspection
17-
const NSIS_SHA2 = "c29883cb9a04733489590420b910ea7a91ba0f9b776fe4c647d9801f23175225"
18+
const NSIS_SHA2 = "cf996b4209f302c1f6b379a6b2090ad0d51a360daf24d1828eeceafd1617a976"
1819

1920
//noinspection SpellCheckingInspection
2021
const ELECTRON_BUILDER_NS_UUID = "50e065bc-3134-11e6-9bab-38c9862bdaf3"
@@ -45,6 +46,8 @@ export default class NsisTarget extends Target {
4546
private async doBuild(appOutDir: string, arch: Arch) {
4647
log(`Packaging NSIS installer for arch ${Arch[arch]}`)
4748

49+
await copyFile(path.join(await nsisPathPromise, "elevate.exe"), path.join(appOutDir, "resources", "elevate.exe"))
50+
4851
const packager = this.packager
4952
const archiveFile = path.join(this.outDir, `${packager.appInfo.name}-${packager.appInfo.version}-${Arch[arch]}.nsis.7z`)
5053
return await archive(packager.config.compression, "7z", archiveFile, appOutDir, true)

packages/electron-updater/src/AppUpdater.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,18 @@ export abstract class AppUpdater extends EventEmitter {
204204
if (this.logger != null) {
205205
this.logger.info(`Downloading update from ${fileInfo.url}`)
206206
}
207-
return await this.doDownloadUpdate(versionInfo, fileInfo)
207+
208+
try {
209+
return await this.doDownloadUpdate(versionInfo, fileInfo)
210+
}
211+
catch (e) {
212+
this.dispatchError(e)
213+
throw e
214+
}
215+
}
216+
217+
protected dispatchError(e: Error) {
218+
this.emit("error", e, (e.stack || e).toString())
208219
}
209220

210221
protected async abstract doDownloadUpdate(versionInfo: VersionInfo, fileInfo: FileInfo): Promise<any>

packages/electron-updater/src/NsisUpdater.ts

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { tmpdir } from "os"
44
import { download, DownloadOptions } from "electron-builder-http"
55
import { DOWNLOAD_PROGRESS, FileInfo } from "./api"
66
import { BintrayOptions, PublishConfiguration, GithubOptions, VersionInfo } from "electron-builder-http/out/publishOptions"
7-
import { mkdtemp } from "fs-extra-p"
7+
import { mkdtemp, remove } from "fs-extra-p"
88
import "source-map-support/register"
99
import { AppUpdater } from "./AppUpdater"
1010

@@ -34,24 +34,32 @@ export class NsisUpdater extends AppUpdater {
3434
downloadOptions.sha2 = fileInfo.sha2
3535
}
3636

37-
return mkdtemp(`${path.join(tmpdir(), "up")}-`)
38-
.then(it => download(fileInfo.url, path.join(it, fileInfo.name), downloadOptions))
39-
.then(it => {
40-
this.setupPath = it
41-
this.addQuitHandler()
42-
const version = this.versionInfo!.version
43-
if (this.logger != null) {
44-
this.logger.info(`New version ${version} has been downloaded`)
45-
}
46-
this.emit("update-downloaded", this.versionInfo, null, version, null, null, () => {
47-
this.quitAndInstall()
48-
})
49-
return it
50-
})
51-
.catch(e => {
52-
this.emit("error", e, (e.stack || e).toString())
53-
throw e
54-
})
37+
const logger = this.logger
38+
const tempDir = await mkdtemp(`${path.join(tmpdir(), "up")}-`)
39+
const tempFile = path.join(tempDir, fileInfo.name)
40+
try {
41+
await download(fileInfo.url, tempFile, downloadOptions)
42+
}
43+
catch (e) {
44+
try {
45+
await remove(tempDir)
46+
}
47+
catch (ignored) {
48+
// ignored
49+
}
50+
51+
throw e
52+
}
53+
54+
const version = this.versionInfo!.version
55+
if (logger != null) {
56+
logger.info(`New version ${version} has been downloaded to ${tempFile}`)
57+
}
58+
59+
this.setupPath = tempFile
60+
this.addQuitHandler()
61+
this.emit("update-downloaded", this.versionInfo, null, version)
62+
return tempFile
5563
}
5664

5765
private addQuitHandler() {
@@ -62,6 +70,9 @@ export class NsisUpdater extends AppUpdater {
6270
this.quitHandlerAdded = true
6371

6472
this.app.on("quit", () => {
73+
if (this.logger != null) {
74+
this.logger.info("Auto install update on quit")
75+
}
6576
this.install(true)
6677
})
6778
}
@@ -91,10 +102,35 @@ export class NsisUpdater extends AppUpdater {
91102
if (isSilent) {
92103
args.push("/S")
93104
}
94-
spawn(setupPath, args, {
105+
const spawnOptions = {
95106
detached: true,
96107
stdio: "ignore",
97-
}).unref()
108+
}
109+
110+
try {
111+
spawn(setupPath, args, spawnOptions)
112+
.unref()
113+
}
114+
catch (e) {
115+
// yes, such errors dispatched not as error event
116+
// https://github.com/electron-userland/electron-builder/issues/1129
117+
if ((<any>e).code === "UNKNOWN") {
118+
if (this.logger != null) {
119+
this.logger.info("UNKNOWN error code on spawn, will be executed again using elevate")
120+
}
121+
122+
try {
123+
spawn(path.join(process.resourcesPath, "elevate.exe"), [setupPath].concat(args), spawnOptions)
124+
.unref()
125+
}
126+
catch (e) {
127+
this.dispatchError(e)
128+
}
129+
}
130+
else {
131+
this.dispatchError(e)
132+
}
133+
}
98134

99135
return true
100136
}

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.5.29.tgz#29f4dd9314fbccb080d8bd84b9c23811ec5090c2"
4242

4343
"@types/node@*":
44-
version "7.0.3"
45-
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.3.tgz#6bc1d23929bd426eabd409b5898537076bbbdeff"
44+
version "7.0.4"
45+
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.4.tgz#9aabc135979ded383325749f508894c662948c8b"
4646

4747
"@types/source-map-support@^0.2.28":
4848
version "0.2.28"

0 commit comments

Comments
 (0)