forked from nwutils/nw-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
110 lines (97 loc) · 2.71 KB
/
index.html
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
<html>
<head>
<title>updater</title>
</head>
<body>
<div id="version"></div>
<div id="loaded"></div>
<script>
var gui = require('nw.gui');
var copyPath, execPath;
if(gui.App.argv.length){
copyPath = gui.App.argv[0];
execPath = gui.App.argv[1];
}
//download new version of app in tmp
//unpack
//run updater
//updater will copy itself to origin directory
//updater will run app
var pkg = require('./package.json');
var request = require('request');
var url = require('url');
var path = require('path');
var os = require('os');
var fs = require('fs');
var k = 0;
var d = false;
var updater = require('./updater.js');
var upd = new updater(pkg);
var newVersionCheckIntervalId = null;
var tryingForNewVersion = false;
//for test purposes
if(!copyPath){
request.get(url.resolve(pkg.manifestUrl, '/version/'+ pkg.version));
document.getElementById('version').innerHTML = 'current version ' + pkg.version;
newVersionCheckIntervalId = setInterval(function(){
if(!d && !tryingForNewVersion) {
tryingForNewVersion = true; //lock
upd.checkNewVersion(versionChecked);
}
}, 500);
} else {
document.getElementById('version').innerHTML = 'copying app';
upd.install(copyPath, newAppInstalled);
function newAppInstalled(err){
if(err){
console.log(err);
return;
}
upd.run(execPath, null);
gui.App.quit();
}
}
function versionChecked(err, newVersionExists, manifest){
tryingForNewVersion = false; //unlock
if(err){
console.log(err);
return Error(err);
}
else if(d){
console.log('Already downloading');
return;
}
else if(!newVersionExists){
console.log('No new version exists');
return;
}
d = true;
clearInterval(newVersionCheckIntervalId);
var loaded = 0;
var newVersion = upd.download(function(error, filename){
newVersionDownloaded(error, filename, manifest);
}, manifest);
newVersion.on('data', function(chunk){
loaded+=chunk.length;
document.getElementById('loaded').innerHTML = "New version loading " + Math.floor(loaded / newVersion['content-length'] * 100) + '%';
})
}
function newVersionDownloaded(err, filename, manifest){
if(err){
console.log(err)
return Error(err);
}
document.getElementById('loaded').innerHTML = "unpacking: " + filename;
upd.unpack(filename, newVersionUnpacked, manifest);
}
function newVersionUnpacked(err, newAppPath){
if(err){
console.log(err)
return Error(err);
}
var runner = upd.runInstaller(newAppPath, [upd.getAppPath(), upd.getAppExec()]);
gui.App.quit();
}
</script>
</body>
</html>