-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdater.js
264 lines (230 loc) · 8.75 KB
/
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
const {app,BrowserWindow}=require('electron');
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 Store=require('electron-store');
const store=new Store();
const dialog=require('electron').dialog;
var splash;
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',
'skip_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] 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;
requestOptions.data.platform = process.platform;
requestOptions.data.arch = process.arch;
console.log("[Updater] Platform:"+process.platform);
console.log("[Updater] Arch:"+process.arch);
console.log("[Updater] Version:"+packageInfo.version);
// Check for updates
HTTP.post(this.setup.api, requestOptions)
.on('complete', function(result){
// If the request failed
if(result instanceof Error){
console.log('[Updater] Could not connect, ' + result.message);
Updater.end(1);
return false;
}
// Connected!
console.log('[Updater] 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)
{
if (store.get('update_preference')=="1")
{
splash=new BrowserWindow({width: 640, height: 480});
splash.loadURL(`file://${__dirname}/dist/static/update.html`);
splash.webContents.on('did-finish-load', ()=>{splash.webContents.executeJavaScript(`swal({onOpen: () => {swal.showLoading()},allowOutsideClick:false,text: 'Updating to ` + response.last + `...'});`);});
console.log('[Updater] Update available: ' + response.last);
Updater.update=response;
Updater.download();
}
if (store.get('update_preference')=="2")
{
const dialogOptions = {type: 'question', buttons: ['OK', 'Cancel'], title:"NEXT",message: 'An update available for NEXT.\r\n\r\nWould you like to download and update now?'}
dialog.showMessageBox(dialogOptions, i => {
if(i=="0")
{
splash=new BrowserWindow({width: 640, height: 480});
splash.loadURL(`file://${__dirname}/dist/static/update.html`);
splash.webContents.on('did-finish-load', ()=>{splash.webContents.executeJavaScript(`swal({onOpen: () => {swal.showLoading()},allowOutsideClick:false,text: 'Updating to ` + response.last + `...'});`);});
console.log('[Updater] Update available: ' + response.last);
Updater.update=response;
Updater.download();
}
else
{
Updater.end(7);
}
});
}
}
else
{
console.log('[Updater] No updates available');
Updater.end(2);
return false;
}
}catch(error){
console.log('[Updater] API response is not valid'+error);
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){
console.log('[Updater] 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){
console.log('[Updater] Failed to download the update to a local file.');
Updater.end(5);
return false;
}
// Store the update file path
Updater.update.file = updateFile;
// Success
console.log('[Updater] Update downloaded: ' + updateFile);
// Apply the update
Updater.apply();
});
});
},
/**
* Apply the update, it simply overwrites the current files!
* */
'apply': function(){
splash.webContents.executeJavaScript(`swal({onOpen: () => {swal.showLoading()},allowOutsideClick:false,text: 'Extracting...'});`);
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.');
splash.webContents.executeJavaScript(`swal({
title: 'Update completed',
text: "Please restart the application",
type: 'success',
showCancelButton: false,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'OK'
}).then((result) => {
if (result.value) {
window.close();
}
})`);
// Success
this.end();
}catch(error){
this.log('Extraction error: ' + error);
splash.webContents.executeJavaScript(`swal({onOpen: () => {swal.showLoading()},allowOutsideClick:false,text: 'Extraction error'});`);
// Failure
this.end(6);
}
}
};
module.exports = Updater;