This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
forked from juampi92/auto-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-updater.js
549 lines (483 loc) · 12.5 KB
/
auto-updater.js
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
var fs = require('fs');
var util = require('util');
var path = require('path').posix;
const { http, https } = require('follow-redirects');
var EventEmitter = require('events').EventEmitter;
var _ = require('underscore'),
Defer = require('node-promise').defer;
/**
* @class AutoUpdater
* @extends event-emitter
*/
var AutoUpdater = function (config) {
EventEmitter.apply(this);
this.attrs = _.extend({}, AutoUpdater.defaults, config);
this.update_dest = 'update';
this.cache = {};
this.jsons = {};
};
// Proto inheritance
util.inherits(AutoUpdater, EventEmitter);
AutoUpdater.prototype.emit = null;
/**
* The user has a git clone. Recommend use the "git pull" command
* @event git-clone
*/
/**
* Versions match
* @event check.up-to-date
* @param {String} version Version number
*/
/**
* Versions don't match
* @event check.out-dated
* @param {String} v_old Old (local) version number
* @param {String} v New version number
*/
/**
* Update downloaded in the machine
* @event update.downloaded
*/
/**
* Update was already in the dir, so it wasnt installed
* @event update.not-installed
*/
/**
* The update has been extracted correctly.
* @event update.extracted
*/
/**
* The download has started
* @event download.start
* @param {String} name Name of the update
*/
/**
* The download has been updated. New percentage
* @event download.progress
* @param {String} name Name of the update
* @param {Number} percent Percent of completion
*/
/**
* The download has ended
* @event download.end
* @param {String} name Name of the update
*/
/**
* Something happened to the download
* @event download.error
* @param {Error} e
*/
/**
* Called when all is over
* ( along with 'check.up-to-date' if there are no updates, or with 'update.extracted' if it was installed )
* @event end
*/
AutoUpdater.defaults = {
/**
* @attribute pathToJson
* @type {String}
* @default ''
*/
pathToJson: '',
/**
* @attribute autoupdate
* @type {Boolean}
* @default false
*/
autoupdate: false,
/**
* @attribute checkgit
* @type {Boolean}
* @default false
*/
checkgit: false,
/**
* @attribute jsonhost
* @type {String}
* @default 'raw.githubusercontent.com'
*/
jsonhost: 'raw.githubusercontent.com',
/**
* @attribute contenthost
* @type {String}
* @default 'codeload.github.com'
*/
contenthost: 'codeload.github.com',
/**
* @attribute devmode
* @type {Boolean}
* @default false
*/
devmode: false,
/**
* If greater than 0, download progress gets debounced using this time (in ms)
* @attribute progressDebounce
* @type {Number}
* @default 0
*/
progressDebounce: 0,
};
/**
* Extra config
* @method use
* @param {Object} options Custom options
* @chainable
*/
AutoUpdater.prototype.use = function (options) {
_.extend(this.attrs, options);
return this;
};
/**
* Fire commands
* @method fire
* @param {String} command Name of command
*/
AutoUpdater.prototype.fire = function (command) {
return commands[command].apply(this, _.toArray(arguments).slice(1));
};
/**
* Error message handling
* (if debug mode, triggers console error)
* @method error
* @param {String} message Description of the error
* @param {String} code Error code
* @param {Exception} e Error object
* @private
*/
AutoUpdater.prototype.error = function (message, code, e) {
if (this.attrs.devmode) {
console.error(message);
}
emit(this, 'error', code, e || {});
};
/**
* Emitts events
* @method emit
* @param {Context} context Context of AutoUpdater
* @private
*/
function emit(context) {
EventEmitter.prototype.emit.apply(context, _.toArray(arguments).slice(1));
}
var commands = {
/**
* Checks packages versions (local and remote)
* @method check
* @chainable
*/
'check': function () {
// first check git if needed
if (this.attrs.checkgit && checkGit.call(this)) {
return;
}
loadClientJson.call(this).then(loadRemoteJson.bind(this)).then(loaded.bind(this));
return this;
},
/**
* Downloads the latest zip
* Fires:
* 'update.not-installed' if the update exists but it wasn't installed
* 'update.downloaded' if the update was successfully downloaded
* @method download-update
* @chainable
*/
'download-update': function () {
// Validation
if (!this.jsons.client) {
return loadClientJson.call(this).then(loadRemoteJson.bind(this)).then(commands['download-update'].bind(this));
}
var self = this,
jsoninfo = this.jsons.client['auto-updater'];
remoteDownloadUpdate
.call(this, this.updateName, {
host: this.attrs.contenthost,
path: '/' + path.join(jsoninfo.repo, 'zip', jsoninfo.branch),
})
.then(function (existed) {
if (existed === true) emit(self, 'update.not-installed');
else emit(self, 'update.downloaded');
if (self.attrs.autoupdate) {
self.fire('extract');
}
});
return this;
},
/**
* Extracts the zip, replacing everything.
* Fires:
* 'update.extracted' when the extraction was successful
* 'end' when the extraction was successful
* @method extract
* @chainable
*/
'extract': function (subfolder) {
var self = this;
extract.call(this, this.updateName, subfolder).then(function () {
emit(self, 'update.extracted');
emit(self, 'end');
});
return this;
},
/**
* Checks against cache, and if not, calculates
* @method diff-dependencies
* @return {Array}
*/
'diff-dependencies': function () {
if (!this.cache.dependencies) {
checkDependencies.call(this);
}
return this.cache.dependencies;
},
};
/**
* Parses and filters package.json
* @method parsePackageJson
* @param {String || Object} data Parsed or raw package.json
* @return {Object} Object containing only 'auto-updater', 'version' and 'dependencies'
* @private
*/
var parsePackageJson = function (data) {
if (!_.isObject(data)) {
data = JSON.parse(data);
}
// Validation
if (!data['auto-updater']) {
this.error('Invalid package.json. No auto-updater field', 'json.error');
throw 'error';
}
var filtered = _.pick(data, 'auto-updater', 'version', 'dependencies');
return filtered;
};
/**
* Fires:
* 'git-clone' if it has a .git folder
* @method _checkGit
* @return {Boolean} Has git folder
* @private
*/
var checkGit = function () {
if (this.cache.git === undefined) {
this.cache.git = fs.existsSync('.git');
if (this.cache.git === true) {
emit(this, 'git-clone');
}
}
return this.cache.git;
};
/**
* Reads the package.json
* @method loadClientJson
* @return {Promise}
* @private
*/
var loadClientJson = function () {
var jsonPath = path.join('.', this.attrs.pathToJson, 'package.json'),
self = this,
deferred = Defer();
fs.readFile(jsonPath, 'utf-8', function (err, data) {
if (err) {
deferred.reject();
return;
}
self.jsons.client = parsePackageJson.call(self, data);
deferred.resolve();
});
return deferred;
};
/**
* Fetches and reads the remote package.json
* @method loadRemoteJson
* @return {Promise}
* @private
*/
var loadRemoteJson = function () {
var self = this,
jsoninfo = self.jsons.client['auto-updater'],
repo = jsoninfo.repo,
branch = jsoninfo.branch,
jsonPath = path.join(repo, branch, this.attrs.pathToJson, 'package.json'),
deferred = Defer();
remoteDownloader
.call(this, {
host: this.attrs.jsonhost,
path: '/' + jsonPath,
})
.then(function (data) {
self.jsons.remote = parsePackageJson.call(self, data);
self.updateName = self.update_dest + '-' + self.jsons.remote.version + '.zip';
deferred.resolve();
}, deferred.reject.bind(deferred));
return deferred;
};
/**
*
* Fires:
* 'check.up-to-date' if local version and remote version match
* 'end' cause it finished checking
* 'check.out-dated' if the versions don't match
* @method loaded
* @private
*/
var loaded = function () {
var clientVersion = this.jsons.client.version,
remoteVersion = this.jsons.remote.version;
if (clientVersion === remoteVersion) {
emit(this, 'check.up-to-date', remoteVersion);
emit(this, 'end');
} else {
emit(this, 'check.out-dated', clientVersion, remoteVersion);
if (this.attrs.autoupdate) {
this.fire('download-update');
}
}
};
/**
* Fires:
* 'download.error' In case the download fails
* @method remoteDownloader
* @param {[type]} opc Object containing host, path and method of the download
* @return {Promise}
* @private
*/
var remoteDownloader = function (opc, callback) {
if (!opc.host || !opc.path) return;
var self = this,
deferred = Defer();
var request = https.request(opc, function (res) {
var data = '';
res.on('data', function (d) {
data = data + d;
});
res.on('end', function () {
var error = null;
try {
data = JSON.parse(data);
} catch (e) {
self.error('Error reading the dowloaded JSON: ' + data, 'download.error', {
e: e,
response: data,
path: opc.path,
host: opc.host,
});
deferred.reject(e);
return;
}
deferred.resolve(data);
});
});
request.on('error', function (e) {
self.error('Error downloaing the remote JSON', 'download.error', e);
deferred.reject();
});
request.end();
return deferred;
};
/**
* Fires:
* 'download.start'
* 'download.progress'
* 'download.end'
* 'download.error'
* @method remoteDownloadUpdate
* @param {String} name Name of the update
* @param {Object} opc Download request options
* @return {Promise}
* @private
*/
var remoteDownloadUpdate = function (name, opc) {
var self = this,
deferred = Defer();
// Ya tengo el update. Falta instalarlo.
if (fs.existsSync(name)) {
deferred.resolve(true);
return deferred;
}
// No tengo el archivo! Descargando!!
var protocol;
if (opc.ssh === false) protocol = http;
else protocol = https;
// download request
var request = protocol.get(opc, function (res) {
// Check if the file already exists and remove it if it does
if (fs.existsSync('_' + name)) fs.unlinkSync('_' + name);
// Download started
emit(self, 'download.start', name);
// Writestream for the binary file
var file = fs.createWriteStream('_' + name),
len = parseInt(res.headers['content-length'], 10),
current = 0;
// Pipe any new block to the stream
res.pipe(file);
var dataRecieve = function (chunk) {
current += chunk.length;
perc = (100.0 * (current / len)).toFixed(2);
emit(self, 'download.progress', name, perc);
};
if (self.attrs.progressDebounce) {
res.on('data', _.debounce(dataRecieve, self.attrs.progressDebounce));
} else {
res.on('data', dataRecieve);
}
res.on('end', function () {
file.end();
});
file.on('finish', function () {
fs.renameSync('_' + name, name);
emit(self, 'download.end', name);
deferred.resolve();
});
});
request.end();
request.on('error', function (e) {
deferred.reject();
emit(self, 'download.error', e);
});
return deferred;
};
/**
*
* @method extract
* @param {String} name Path of zip
* @param {Boolean} subfolder If subfolder. (check Adm-zip)
* @return {Promise}
* @private
*/
var extract = function (name, subfolder) {
var admzip = require('adm-zip');
var zip = new admzip(name);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
var deferred = Defer();
if (subfolder) {
zip.extractAllTo('./', true);
} else {
zip.extractEntryTo(zipEntries[0], './', false, true);
}
fs.unlink(name, deferred.resolve.bind(deferred));
return deferred;
};
/**
* Iterates over the local and remote dependencies to check if they have changed
* @method checkDependencies
* @return {Boolean} If they have changed
* @private
*/
var checkDependencies = function () {
var client = this.jsons.client.dependencies,
remote = this.jsons.remote.dependencies,
cache = this.cache;
if (!client || !remote) {
this.error('Error: you need to check the jsons before checking dependencies', 'dependencies.error');
return;
}
cache.dependencies = [];
_.each(remote, function (value, key) {
// Check that the client has the key, or that the versions are the same
if (!client.hasOwnProperty(key) || value !== client[key]) {
// Log the diff
cache.dependencies.push(key);
}
});
return cache.dependencies.length > 0;
};
module.exports = AutoUpdater;