forked from megahertz/electron-simple-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
334 lines (292 loc) · 8.78 KB
/
Copy pathindex.js
File metadata and controls
334 lines (292 loc) · 8.78 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'use strict';
const { app, autoUpdater } = require('electron');
const events = require('events');
const getUpdatesMeta = require('./lib/get-updates-meta');
const linux = require('./lib/linux');
const normalizeOptions = require('./lib/normalize-options');
const win32 = require('./lib/win32');
class SimpleUpdater extends events.EventEmitter {
constructor() {
super();
// Just for better auto-complete
this.options = {
autoDownload: true,
build: '',
channel: 'prod',
checkUpdateOnStart: true,
disabled: false,
empty: true, // Mark that it's not initialized
logger: console,
version: '',
url: '',
};
this.meta = {
empty: true, // Mark that it's not initialized
version: '',
update: '',
};
autoUpdater.on('update-downloaded', () => {
const version = this.meta.version;
this.options.logger.info(`New version ${version} has been downloaded`);
/**
* @event SimpleUpdater#update-downloaded
* @param {object} meta Update metadata
*/
this.emit('update-downloaded', this.meta);
});
this.on('error', (e) => {
if (this.options.logger) {
this.options.logger.warn(e);
}
});
autoUpdater.on('error', e => this.emit('error', e));
}
/**
* Initialize a package.
* By default it finish the process if run by Squirrel.Windows installer
*
* @param {object|string} [options]
* @param {bool} [options.autoDownload=true] Automatically download an
* update when it is found in updates.json
* @param {string} [options.build] Build type, like 'linux-x64'
* or 'win32-ia32'
* @param {string} [options.channel=prod] An application which is built for
* channel like 'beta' will receive updates only from this channel
* @param {bool} [options.checkUpdateOnStart=true] Check for updates
* immediately when init() is called
* @param {bool} [options.disabled=false] Disable update feature.
* This option is set to true automatically for applications built for
* Mac AppStore or Windows Store
* @param {object} [options.logger=console] You can pass electron-log,
* winston or another logger with the following interface:
* { info(){}, warn(){} }. Set it to false if you would like to disable
* a logging feature
* @param {string} [options.version] Current app version. In most cases,
* you should not pass this options manually, it is read by electron
* from version at package.json
* @param {string} [options.url] URL to a file updates.json
*
* @fires SimpleUpdater#error
* @return {SimpleUpdater}
*/
init(options) {
if (!this.options.empty) {
/**
* @event SimpleUpdater#error
* @param {object|string} error
*/
this.emit(
'error',
'electron-simple updater has been initialized before'
);
return this;
}
// Return if we run not compiled application
if (app.isPackaged === false || app.getName() === 'Electron') {
this.options.disabled = true;
return this;
}
this.options = normalizeOptions(options);
const squirrelAction = win32.getSquirrelInstallerAction();
if (squirrelAction) {
const event = { squirrelAction, preventDefault: false };
/**
* @event SimpleUpdater#squirrel-win-installer
* @param {string} action one of:
* squirrel-install
* squirrel-updated
* squirrel-uninstall
* squirrel-obsolete
*/
this.emit('squirrel-win-installer', event);
if (!event.preventDefault) {
win32.processSquirrelInstaller(squirrelAction);
process.exit();
}
return this;
}
if (this.options.checkUpdateOnStart) {
this.checkForUpdates();
}
return this;
}
/**
* Asks the server whether there is an update. url must be set before
* this call
* @fires SimpleUpdater#error
* @fires SimpleUpdater#checking-for-update
* @fires SimpleUpdater#update-not-available
* @return {SimpleUpdater}
*/
checkForUpdates() {
const opt = this.options;
if (opt.disabled) {
opt.logger.warn('Update is disabled');
return this;
}
if (!opt.url) {
this.emit('error', 'You must set url before calling checkForUpdates()');
return this;
}
/**
* @event SimpleUpdater#checking-for-update
*/
this.emit('checking-for-update');
//noinspection JSUnresolvedFunction
getUpdatesMeta(opt.url, opt.build, opt.channel, opt.version)
.then((updateMeta) => {
if (updateMeta) {
this.onFoundUpdate(updateMeta);
} else {
opt.logger.debug && opt.logger.debug(
`Update for ${this.buildId} is not available`
);
/**
* @event SimpleUpdater#update-not-available
*/
this.emit('update-not-available');
}
})
.catch(e => this.emit('error', e));
return this;
}
/**
* Start downloading update manually.
* You can use this method if autoDownload option is set to false
* @fires SimpleUpdater#update-downloading
* @fires SimpleUpdater#update-downloaded
* @fires SimpleUpdater#error
* @return {SimpleUpdater}
*/
downloadUpdate() {
if (!this.meta.update) {
const msg = 'There is no metadata for update. Run checkForUpdates first.';
this.emit('error', msg);
return this;
}
let feedUrl = autoUpdater.getFeedURL();
/**
* @event SimpleUpdater#update-downloading
* @param {object} meta Update metadata
*/
this.emit('update-downloading', this.meta);
if (process.platform === 'linux') {
feedUrl = this.meta.update;
linux.downloadUpdate(feedUrl, this.options.logger, this.options.appName)
.then((appImagePath) => {
this.appImagePath = appImagePath;
const version = this.meta.version;
this.options.logger.info(
`New version ${version} has been downloaded`
);
this.emit('update-downloaded', this.meta);
})
.catch(e => this.emit('error', e));
} else {
autoUpdater.checkForUpdates();
}
this.options.logger.info(`Downloading updates from ${feedUrl}`);
return this;
}
/**
* Restarts the app and installs the update after it has been downloaded.
* It should only be called after update-downloaded has been emitted.
* @return {void}
*/
quitAndInstall() {
if (this.appImagePath) {
return linux.quitAndInstall();
}
return autoUpdater.quitAndInstall();
}
/**
* Set one or a few options
* @param {string|object} name
* @param {*} value
* @return {SimpleUpdater}
*/
setOptions(name, value = null) {
if (typeof name === 'object') {
Object.assign(this.options, name);
return this;
}
this.options[name] = value;
return this;
}
get build() {
if (!this.checkIsInitialized()) return;
return this.options.build;
}
/**
* Return a build name with a channel and version
* @return {string}
*/
get buildId() {
if (!this.checkIsInitialized()) return '';
return `${this.build}-${this.channel}-v${this.version}`;
}
get channel() {
if (!this.checkIsInitialized()) return;
return this.options.channel;
}
get version() {
if (!this.checkIsInitialized()) return;
return this.options.version;
}
/**
* Sets the url and initialize the auto updater.
* Instead of built-in auto-updater, it's a URL to updates.json
* @deprecated
* @param {string} url
*/
setFeedURL(url) {
if (this.options.empty) {
this.init(url);
} else {
this.options.url = url;
}
}
/**
* Return the current updates.json URL
* @return {string}
*/
getFeedURL() {
if (!this.checkIsInitialized()) return '';
return this.options.url;
}
/**
* Called when updates metadata has been downloaded
* @private
* @fires SimpleUpdater#update-available
* @param {object} meta
*/
onFoundUpdate(meta) {
this.meta = meta;
const opt = this.options;
if (opt.logger.debug) {
opt.logger.debug(`Found version ${meta.version} at ${meta.update}`);
}
autoUpdater.setFeedURL(meta.update);
/**
* @event SimpleUpdater#update-available
* @param {object} meta Update metadata
*/
this.emit('update-available', meta);
if (opt.autoDownload) {
this.downloadUpdate();
}
}
/**
* @private
* @fires SimpleUpdater#error
* @return {boolean}
*/
checkIsInitialized() {
if (this.options.empty) {
this.emit('error', 'electron-simple-updater is not initialized');
return false;
}
return true;
}
}
module.exports = new SimpleUpdater();