forked from jaygooby/electron-asar-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
304 lines (246 loc) · 10.6 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
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
const Application = require('electron').app;
const FileSystem = require('original-fs');
const Utils = require('util');
const HTTP = require('restler');
// Yes, it's weird, but we need the trailing slash after the .asar
// so we can read paths "inside" it, e.g. the package.json, where we look
// for our current version
const AppPath = Application.getAppPath() + '/';
const AppPathFolder = AppPath.slice(0,AppPath.indexOf("app.asar"));
const AppAsar = AppPath.slice(0,-1);
const WindowsUpdater = AppPath.slice(0,AppPath.indexOf("resources")) + "updater.exe";
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);
this.log("AppPath: " + AppPath);
this.log("AppPathFolder: " + AppPathFolder);
},
/**
* Logging
* */
'log': function(line){
// Log it
console.log('Updater: ', line);
// Put it into a file
if(this.setup.logFile){
console.log("%s + %s + %s", AppPathFolder, this.setup.logFile, line);
FileSystem.appendFileSync(AppPathFolder + 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');
this.log(packageInfo.version);
// 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.json(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 = 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;
// Ask user for confirmation
Updater.end();
}else{
Updater.log('No updates available');
Updater.end(2);
return false;
}
}catch(error){
Updater.log(result + 'API response is not valid');
Updater.end(3);
}
});
},
/**
* Download the update file
* */
'download': function(callback){
if(callback){
this.setup.callback = callback;
}
var url = this.update.source,
fileName = 'update.asar';
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 = AppPathFolder + fileName;
Updater.log("updateFile: " + updateFile);
// Create the file
FileSystem.writeFile(updateFile, data, null, function(error){
if(error){
Updater.log(error + '\n Failed to download the update to a local file.');
Updater.end(5);
return false;
}
// Store the update file path
Updater.update.file = updateFile;
Updater.log("Updater.update.file: " + updateFile);
// Success
Updater.log('Update downloaded: ' + updateFile);
// Apply the update
Updater.apply();
});
});
},
/**
* Apply the update, remove app.asar and rename update.zip to app.asar
* */
'apply': function(){
try{
this.log("Going to unlink: " + AppPath.slice(0,-1));
FileSystem.unlink(AppPath.slice(0,-1), function(err) {
if (err) {
Updater.log("Couldn't unlink: " + AppPath.slice(0,-1));
return console.error(err);
}
Updater.log("Asar deleted successfully.");
});
}catch(error){
this.log('Delete error: ' + error);
// Failure
this.end(6);
}
try{
this.log("Going to rename: " + this.update.file + " to: " + AppPath.slice(0,-1));
FileSystem.rename(this.update.file,AppPath.slice(0,-1),function(err) {
if (err) {
Updater.log("Couldn't rename: " + Updater.update.file + " to: " + AppPath.slice(0,-1));
return console.error(err);
}
Updater.log("Update applied.");
})
this.log('End of update.');
// Success
this.end();
}catch(error){
this.log('Rename error: ' + error);
// Failure
this.end(6);
}
},
// app.asar is always EBUSY on Windows, so we need to try another
// way of replacing it. This should get called after the main Electron
// process has quit. Win32 calls 'move' and other platforms call 'mv'
'mvOrMove': function(child) {
var updateAsar = AppPathFolder + 'update.asar';
var appAsar = AppPathFolder + 'app.asar';
var winArgs = "";
Updater.log("Checking for " + updateAsar);
try {
FileSystem.accessSync(updateAsar)
try {
Updater.log("Going to shell out to move: " + updateAsar + " to: " + AppAsar);
if (process.platform==='win32') {
Updater.log("Going to start the windows updater:" + WindowsUpdater + " " + updateAsar + " " + appAsar);
// JSON.stringify() calls mean we're correctly quoting paths with spaces
winArgs = `${JSON.stringify(WindowsUpdater)} ${JSON.stringify(updateAsar)} ${JSON.stringify(appAsar)}`
Updater.log(winArgs);
// and the windowsVerbatimArguments options argument, in combination with the /s switch, stops windows stripping quotes from our commandline
// This doesn't work:
// child.spawn(`${JSON.stringify(WindowsUpdater)}`,[`${JSON.stringify(updateAsar)}`,`${JSON.stringify(appAsar)}`], {detached: true, windowsVerbatimArguments: true, stdio: 'ignore'});
// so we have to spawn a cmd shell, which then runs the updater, and leaves a visible window whilst running
child.spawn('cmd', ['/s', '/c', '"' + winArgs + '"'], {detached: true, windowsVerbatimArguments: true, stdio: 'ignore'});
} else {
// here's how we'd do this on Mac/Linux, but on Mac at least, the .asar isn't marked as busy, so the update process above
// is able to overwrite it.
//
// child.spawn('bash', ['-c', ['cd ' + JSON.stringify(AppPathFolder), 'mv -f update.asar app.asar'].join(' && ')], {detached: true});
}
child.unref; // let the child live on
} catch(error) {
Updater.log("Shelling out to move failed: " + error);
}
} catch(error) {
Updater.log("Couldn't see an " + updateAsar + " error was: " + error);
}
}
};
module.exports = Updater;