-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
227 lines (184 loc) · 6.64 KB
/
index.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
/**
* Hi there,
* Thanks for using this module, and good luck with your Electron app.
*
* ~ Zain
* */
const Application = require('electron').app;
const FileSystem = require('fs');
const Utils = require('util');
const Zip = require('adm-zip');
const HTTP = require('restler');
const AppPath = Application.getAppPath() + '/';
const errors = [
'version_not_specified',
'cannot_connect_to_api',
'no_update_available',
'api_response_not_valid',
'update_file_not_found',
'failed_to_download_update',
'failed_to_apply_update'
];
/**
* */
var Updater = {
/**
* The setup
* */
'setup': {
'api': null,
'logFile': 'updater-log.txt',
'requestOptions': {},
'callback': false
},
/**
* The new update information
* */
'update': {
'last': null,
'source': null,
'file': null
},
/**
* Init the module
* */
'init': function(setup){
this.setup = Utils._extend(this.setup, setup);
},
/**
* Logging
* */
'log': function(line){
// Log it
console.log('Updater: ', line);
// Put it into a file
if(this.setup.logFile){
FileSystem.appendFileSync(AppPath + this.setup.logFile, line + "\n");
}
},
/**
* Triggers the callback you set to receive the result of the update
* */
'end': function(error){
if(typeof this.setup.callback != 'function') return false;
this.setup.callback.call(this,
( error != 'undefined' ?errors[error] :false ),
this.update.last);
},
/**
* Make the check for the update
* */
'check': function(callback){
if(callback){
this.setup.callback = callback;
}
// Get the current version
var packageInfo = require(AppPath + 'package.json');
// If the version property not specified
if(!packageInfo.version){
this.log('The "version" property not specified inside the application package.json');
this.end(0);
return false;
}
var requestOptions = Utils._extend({}, this.setup.requestOptions);
if(!requestOptions.data){
requestOptions.data = {};
}
// Send the current version along with the request
requestOptions.data.current = packageInfo.version;
// Check for updates
HTTP.post(this.setup.api, requestOptions)
.on('complete', function(result){
// If the request failed
if(result instanceof Error){
Updater.log('Could not connect, ' + result.message);
Updater.end(1);
return false;
}
// Connected!
Updater.log('Connected to ' + Updater.setup.api);
// Handle the response
try{
if(!result){
throw false;
}
// Parse the response
var response = typeof result === 'object' ? result : JSON.parse(result);
// If the "last" property is not defined
if(!response.last){
throw false;
}
// Update available
if(response.source){
Updater.log('Update available: ' + response.last);
// Store the response
Updater.update = response;
// Download the update
Updater.download();
}else{
Updater.log('No updates available');
Updater.end(2);
return false;
}
}catch(error){
Updater.log('API response is not valid');
Updater.end(3);
}
});
},
/**
* Download the update file
* */
'download': function(){
var url = this.update.source,
fileName = 'update.zip';
this.log('Downloading ' + url);
var requestOptions = Utils._extend({}, this.setup.requestOptions);
requestOptions.decoding = 'buffer';
// Download the file
HTTP.get(url, requestOptions)
.on('complete', function(data){
// The request failed
if(data instanceof Error){
Updater.log('Could not find the update file.');
Updater.end(4);
return false;
}
// The file full path
var updateFile = AppPath + fileName;
// Create the file
FileSystem.writeFile(updateFile, data, null, function(error){
if(error){
Updater.log('Failed to download the update to a local file.');
Updater.end(5);
return false;
}
// Store the update file path
Updater.update.file = updateFile;
// Success
Updater.log('Update downloaded: ' + updateFile);
// Apply the update
Updater.apply();
});
});
},
/**
* Apply the update, it simply overwrites the current files!
* */
'apply': function(){
try{
this.log('Extracting the new update files.');
var zip = new Zip(this.update.file);
zip.extractAllTo(AppPath, true);
this.log('New update files were extracted.');
this.log('End of update.');
// Success
this.end();
}catch(error){
this.log('Extraction error: ' + error);
// Failure
this.end(6);
}
}
};
module.exports = Updater;