| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | 'use strict' | 
					
						
							| 
									
										
										
										
											2016-01-15 14:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | const app = require('electron').app | 
					
						
							|  |  |  | const EventEmitter = require('events').EventEmitter | 
					
						
							|  |  |  | const squirrelUpdate = require('./squirrel-update-win') | 
					
						
							|  |  |  | const util = require('util') | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | function AutoUpdater () { | 
					
						
							|  |  |  |   EventEmitter.call(this) | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | util.inherits(AutoUpdater, EventEmitter) | 
					
						
							| 
									
										
										
										
											2016-01-15 14:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | AutoUpdater.prototype.quitAndInstall = function () { | 
					
						
							| 
									
										
										
										
											2016-04-26 01:35:34 +10:00
										 |  |  |   if (!this.updateAvailable) { | 
					
						
							|  |  |  |     return this.emitError('No update available, can\'t quit and install') | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |   squirrelUpdate.processStart() | 
					
						
							|  |  |  |   return app.quit() | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | AutoUpdater.prototype.setFeedURL = function (updateURL) { | 
					
						
							|  |  |  |   this.updateURL = updateURL | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-15 14:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | AutoUpdater.prototype.checkForUpdates = function () { | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  |   if (!this.updateURL) { | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |     return this.emitError('Update URL is not set') | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (!squirrelUpdate.supported()) { | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |     return this.emitError('Can not find Squirrel') | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |   this.emit('checking-for-update') | 
					
						
							| 
									
										
										
										
											2016-03-10 11:54:17 -08:00
										 |  |  |   squirrelUpdate.download(this.updateURL, (error, update) => { | 
					
						
							|  |  |  |     if (error != null) { | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |       return this.emitError(error) | 
					
						
							| 
									
										
										
										
											2016-03-10 11:54:17 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (update == null) { | 
					
						
							| 
									
										
										
										
											2016-04-26 01:35:34 +10:00
										 |  |  |       this.updateAvailable = false | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |       return this.emit('update-not-available') | 
					
						
							| 
									
										
										
										
											2016-03-10 11:54:17 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-26 01:35:34 +10:00
										 |  |  |     this.updateAvailable = true | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |     this.emit('update-available') | 
					
						
							| 
									
										
										
										
											2016-03-10 11:54:17 -08:00
										 |  |  |     squirrelUpdate.update(this.updateURL, (error) => { | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |       var date, releaseNotes, version | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  |       if (error != null) { | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |         return this.emitError(error) | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |       releaseNotes = update.releaseNotes | 
					
						
							|  |  |  |       version = update.version | 
					
						
							| 
									
										
										
										
											2016-03-10 11:54:17 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Following information is not available on Windows, so fake them.
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |       date = new Date() | 
					
						
							| 
									
										
										
										
											2016-03-10 11:54:17 -08:00
										 |  |  |       this.emit('update-downloaded', {}, releaseNotes, version, date, this.updateURL, () => { | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  |         this.quitAndInstall() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-15 14:12:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-03 09:52:04 -08:00
										 |  |  | // Private: Emit both error object and message, this is to keep compatibility
 | 
					
						
							|  |  |  | // with Old APIs.
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | AutoUpdater.prototype.emitError = function (message) { | 
					
						
							|  |  |  |   return this.emit('error', new Error(message), message) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 13:15:04 -07:00
										 |  |  | module.exports = new AutoUpdater() |