-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
AppUpdater.ts
246 lines (206 loc) · 7.55 KB
/
AppUpdater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { EventEmitter } from "events"
import * as path from "path"
import { gt as isVersionGreaterThan, valid as parseVersion } from "semver"
import { executorHolder } from "electron-builder-http"
import { Provider, UpdateCheckResult, FileInfo, UpdaterSignal } from "./api"
import { BintrayProvider } from "./BintrayProvider"
import BluebirdPromise from "bluebird-lst-c"
import { BintrayOptions, PublishConfiguration, GithubOptions, GenericServerOptions, VersionInfo } from "electron-builder-http/out/publishOptions"
import { readFile } from "fs-extra-p"
import { safeLoad } from "js-yaml"
import { GenericProvider } from "./GenericProvider"
import { GitHubProvider } from "./GitHubProvider"
import { ElectronHttpExecutor } from "./electronHttpExecutor"
import "source-map-support/register"
export interface Logger {
info(message?: any): void
warn(message?: any): void
error(message?: any): void
}
export abstract class AppUpdater extends EventEmitter {
/**
* Automatically download an update when it is found.
*/
public autoDownload = true
protected updateAvailable = false
private clientPromise: Promise<Provider<any>>
private readonly untilAppReady: Promise<boolean>
private checkForUpdatesPromise: Promise<UpdateCheckResult> | null
protected readonly app: any
protected versionInfo: VersionInfo | null
private fileInfo: FileInfo | null
public readonly signals = new UpdaterSignal(this)
/**
* The logger. You can pass [electron-log](https://github.com/megahertz/electron-log), [winston](https://github.com/winstonjs/winston) or another logger with the following interface: `{ info(), warn(), error() }`.
* Set it to `null` if you would like to disable a logging feature.
*/
public logger: Logger | null = console
constructor(options: PublishConfiguration | BintrayOptions | GithubOptions | null | undefined) {
super()
this.on("error", (error: Error) => {
if (this.logger != null) {
this.logger.error(`Error: ${error.stack || error.message}`)
}
})
if ((<any>global).__test_app != null) {
this.app = (<any>global).__test_app
this.untilAppReady = BluebirdPromise.resolve()
}
else {
this.app = require("electron").app
executorHolder.httpExecutor = new ElectronHttpExecutor()
this.untilAppReady = new BluebirdPromise(resolve => {
if (this.app.isReady()) {
if (this.logger != null) {
this.logger.info("Wait for app ready")
}
resolve()
}
else {
if (this.logger != null) {
this.logger.info("App is ready")
}
this.app.on("ready", resolve)
}
})
}
if (options != null) {
this.setFeedURL(options)
}
}
//noinspection JSMethodCanBeStatic,JSUnusedGlobalSymbols
getFeedURL(): string | null | undefined {
return "Deprecated. Do not use it."
}
setFeedURL(value: PublishConfiguration | BintrayOptions | GithubOptions | GenericServerOptions | string) {
// https://github.com/electron-userland/electron-builder/issues/1105
let client: Provider<any>
if (typeof value === "string") {
client = new GenericProvider({provider: "generic", url: value})
}
else {
client = createClient(value)
}
this.clientPromise = BluebirdPromise.resolve(client)
}
checkForUpdates(): Promise<UpdateCheckResult> {
let checkForUpdatesPromise = this.checkForUpdatesPromise
if (checkForUpdatesPromise != null) {
return checkForUpdatesPromise
}
checkForUpdatesPromise = this._checkForUpdates()
this.checkForUpdatesPromise = checkForUpdatesPromise
const nullizePromise = () => this.checkForUpdatesPromise = null
checkForUpdatesPromise
.then(nullizePromise)
.catch(nullizePromise)
return checkForUpdatesPromise
}
private async _checkForUpdates(): Promise<UpdateCheckResult> {
await this.untilAppReady
if (this.logger != null) {
this.logger.info("Checking for update")
}
this.emit("checking-for-update")
try {
if (this.clientPromise == null) {
this.clientPromise = loadUpdateConfig().then(it => createClient(it))
}
return await this.doCheckForUpdates()
}
catch (e) {
if (this.logger != null) {
this.logger.info("Cannot check for updates:")
}
this.emit("error", e, (e.stack || e).toString())
throw e
}
}
private async doCheckForUpdates(): Promise<UpdateCheckResult> {
const client = await this.clientPromise
const versionInfo = await client.getLatestVersion()
const latestVersion = parseVersion(versionInfo.version)
if (latestVersion == null) {
throw new Error(`Latest version (from update server) is not valid semver version: "${latestVersion}`)
}
const currentVersion = parseVersion(this.app.getVersion())
if (currentVersion == null) {
throw new Error(`App version is not valid semver version: "${currentVersion}`)
}
if (!isVersionGreaterThan(latestVersion, currentVersion)) {
this.updateAvailable = false
if (this.logger != null) {
this.logger.info(`Update for version ${versionInfo.version} is not available`)
}
this.emit("update-not-available")
return {
versionInfo: versionInfo,
}
}
const fileInfo = await client.getUpdateFile(versionInfo)
this.updateAvailable = true
this.versionInfo = versionInfo
this.fileInfo = fileInfo
this.onUpdateAvailable(versionInfo, fileInfo)
//noinspection ES6MissingAwait
return {
versionInfo: versionInfo,
fileInfo: fileInfo,
downloadPromise: this.autoDownload ? this.downloadUpdate() : null,
}
}
protected onUpdateAvailable(versionInfo: VersionInfo, fileInfo: FileInfo) {
if (this.logger != null) {
this.logger.info(`Found version ${versionInfo.version} (url: ${fileInfo.url})`)
}
this.emit("update-available")
}
/**
* Start downloading update manually. You can use this method if `autoDownload` option is set to `false`.
* @returns {Promise<string>} Path to downloaded file.
*/
async downloadUpdate(): Promise<any> {
const versionInfo = this.versionInfo
const fileInfo = this.fileInfo
if (versionInfo == null || fileInfo == null) {
const message = "Please check update first"
const error = new Error(message)
this.emit("error", error, message)
throw error
}
if (this.logger != null) {
this.logger.info(`Downloading update from ${fileInfo.url}`)
}
try {
return await this.doDownloadUpdate(versionInfo, fileInfo)
}
catch (e) {
this.dispatchError(e)
throw e
}
}
protected dispatchError(e: Error) {
this.emit("error", e, (e.stack || e).toString())
}
protected async abstract doDownloadUpdate(versionInfo: VersionInfo, fileInfo: FileInfo): Promise<any>
abstract quitAndInstall(): void
}
async function loadUpdateConfig() {
return safeLoad(await readFile(path.join((<any>global).__test_resourcesPath || (<any>process).resourcesPath, "app-update.yml"), "utf-8"))
}
function createClient(data: string | PublishConfiguration | BintrayOptions | GithubOptions) {
if (typeof data === "string") {
throw new Error("Please pass PublishConfiguration object")
}
const provider = (<PublishConfiguration>data).provider
switch (provider) {
case "github":
return new GitHubProvider(<GithubOptions>data)
case "generic":
return new GenericProvider(<GenericServerOptions>data)
case "bintray":
return new BintrayProvider(<BintrayOptions>data)
default:
throw new Error(`Unsupported provider: ${provider}`)
}
}