Skip to content

Commit 0c5f500

Browse files
committed
2.0 - Auto updater
1 parent d982498 commit 0c5f500

File tree

3 files changed

+67
-47
lines changed

3 files changed

+67
-47
lines changed

dist/index.html

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
<span class="sr-only">Loading...</span>
2424
</div>
2525
</app-yt>
26-
<script>
26+
<script>
2727
const AutoUpdater = require( "nw-autoupdater" ),
2828
updater = new AutoUpdater( require( "./package.json" ) ),
2929
output = document.querySelector( "#output" ),
30-
updateBar = document.getElementsByClassName("update-bar")[0].classList;
30+
updateBar = document.getElementsByClassName("update-bar")[0].classList;
3131

32-
async function main(){
32+
async function main(update){
3333
try {
3434
// Update copy is running to replace app with the update
3535
if ( updater.isSwapRequest() ) {
@@ -40,6 +40,7 @@
4040
await updater.restart();
4141
return;
4242
}
43+
4344
// Download/unpack update if any available
4445
const rManifest = await updater.readRemoteManifest();
4546
const needsUpdate = await updater.checkNewVersion( rManifest );
@@ -51,35 +52,42 @@
5152
setTimeout(() => {
5253
updateBar.remove('active');
5354
}, 4000);
55+
return;
56+
}
57+
58+
if (update) {
59+
output.innerHTML = `Preparing files...`;
60+
if (document.getElementById('update-buttons').hasChildNodes()) {
61+
var children = document.getElementById('update-buttons').childNodes;
62+
document.getElementById('update-buttons').removeChild(children[0]);
63+
document.getElementById('update-buttons').removeChild(children[0]);
64+
}
65+
return;
5466
} else {
5567
output.innerHTML = `Update available...`;
5668
updateBar.add('active', 'update-av');
5769
initUpdate();
70+
return;
5871
}
72+
73+
// Subscribe for progress events
74+
updater.on( "download", ( downloadSize, totalSize ) => {
75+
output.innerHTML = `Downloading...`;
76+
console.log( "download progress", Math.floor( downloadSize / totalSize * 100 ), "%" );
77+
});
78+
updater.on( "install", ( installFiles, totalFiles ) => {
79+
output.innerHTML = `Installing...`;
80+
console.log( "install progress", Math.floor( installFiles / totalFiles * 100 ), "%" );
81+
});
82+
const updateFile = await updater.download( rManifest );
83+
updater.unpack( updateFile );
84+
updateAppSwap();
5985
} catch ( e ) {
6086
console.error( e );
6187
}
6288
}
63-
64-
function updateApp() {
65-
var rManifest = updater.readRemoteManifest();
66-
67-
// Subscribe for progress events
68-
updater.on( "download", ( downloadSize, totalSize ) => {
69-
output.innerHTML = `Downloading...`;
70-
console.log( "download progress", Math.floor( downloadSize / totalSize * 100 ), "%" );
71-
});
72-
updater.on( "install", ( installFiles, totalFiles ) => {
73-
output.innerHTML = `Installing...`;
74-
console.log( "install progress", Math.floor( installFiles / totalFiles * 100 ), "%" );
75-
});
76-
const updateFile = updater.download( rManifest );
77-
updater.unpack( updateFile );
78-
updateAppSwap();
79-
}
8089

8190
function updateAppSwap() {
82-
output.innerHTML = `The application will automatically restart to finish installing the update...`;
8391
setTimeout(() => {
8492
updater.restartToSwap();
8593
}, 3000);
@@ -103,10 +111,12 @@
103111
btnUPD.innerHTML = 'Update';
104112
document.getElementById("update-buttons").appendChild(btnUPD);
105113

106-
btnUPD.onclick = updateApp;
114+
btnUPD.addEventListener("click", function(){
115+
main(true);
116+
});
107117
}
108118

109-
main();
119+
main(false);
110120

111121
</script>
112122
<script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="polyfills.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>

dist/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "yt-app",
44
"description": "YouTube Player",
55
"author": "Alex Olari <queadx@gmail.com>",
6-
"version": "1.0.0",
6+
"version": "1.0.1",
77
"keywords": [
88
"youtube app",
99
"node-webkit"
@@ -24,7 +24,7 @@
2424
},
2525
"scripts": {
2626
"postversion": "npm run package",
27-
"package": "nwb nwbuild -v 0.25.2 . -o ../release --output-format=ZIP --output-name={name}-v{version}-{target}"
27+
"package": "nwb nwbuild -v 0.25.2-sdk . -o ../release --output-format=ZIP --output-name={name}-v{version}-{target}"
2828
},
2929
"dependencies": {
3030
"nw-autoupdater": "*"

src/index.html

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
const AutoUpdater = require( "nw-autoupdater" ),
2828
updater = new AutoUpdater( require( "./package.json" ) ),
2929
output = document.querySelector( "#output" ),
30-
updateBar = document.getElementsByClassName("update-bar")[0].classList;
30+
updateBar = document.getElementsByClassName("update-bar")[0].classList;
3131

32-
async function main(){
32+
async function main(update){
3333
try {
3434
// Update copy is running to replace app with the update
3535
if ( updater.isSwapRequest() ) {
@@ -40,6 +40,7 @@
4040
await updater.restart();
4141
return;
4242
}
43+
4344
// Download/unpack update if any available
4445
const rManifest = await updater.readRemoteManifest();
4546
const needsUpdate = await updater.checkNewVersion( rManifest );
@@ -51,35 +52,42 @@
5152
setTimeout(() => {
5253
updateBar.remove('active');
5354
}, 4000);
55+
return;
56+
}
57+
58+
if (update) {
59+
output.innerHTML = `Preparing files...`;
60+
if (document.getElementById('update-buttons').hasChildNodes()) {
61+
var children = document.getElementById('update-buttons').childNodes;
62+
document.getElementById('update-buttons').removeChild(children[0]);
63+
document.getElementById('update-buttons').removeChild(children[0]);
64+
}
65+
return;
5466
} else {
5567
output.innerHTML = `Update available...`;
5668
updateBar.add('active', 'update-av');
5769
initUpdate();
70+
return;
5871
}
72+
73+
// Subscribe for progress events
74+
updater.on( "download", ( downloadSize, totalSize ) => {
75+
output.innerHTML = `Downloading...`;
76+
console.log( "download progress", Math.floor( downloadSize / totalSize * 100 ), "%" );
77+
});
78+
updater.on( "install", ( installFiles, totalFiles ) => {
79+
output.innerHTML = `Installing...`;
80+
console.log( "install progress", Math.floor( installFiles / totalFiles * 100 ), "%" );
81+
});
82+
const updateFile = await updater.download( rManifest );
83+
updater.unpack( updateFile );
84+
updateAppSwap();
5985
} catch ( e ) {
6086
console.error( e );
6187
}
6288
}
63-
64-
function updateApp() {
65-
var rManifest = updater.readRemoteManifest();
66-
67-
// Subscribe for progress events
68-
updater.on( "download", ( downloadSize, totalSize ) => {
69-
output.innerHTML = `Downloading...`;
70-
console.log( "download progress", Math.floor( downloadSize / totalSize * 100 ), "%" );
71-
});
72-
updater.on( "install", ( installFiles, totalFiles ) => {
73-
output.innerHTML = `Installing...`;
74-
console.log( "install progress", Math.floor( installFiles / totalFiles * 100 ), "%" );
75-
});
76-
const updateFile = updater.download( rManifest );
77-
updater.unpack( updateFile );
78-
updateAppSwap();
79-
}
8089

8190
function updateAppSwap() {
82-
output.innerHTML = `The application will automatically restart to finish installing the update...`;
8391
setTimeout(() => {
8492
updater.restartToSwap();
8593
}, 3000);
@@ -103,10 +111,12 @@
103111
btnUPD.innerHTML = 'Update';
104112
document.getElementById("update-buttons").appendChild(btnUPD);
105113

106-
btnUPD.onclick = updateApp;
114+
btnUPD.addEventListener("click", function(){
115+
main(true);
116+
});
107117
}
108118

109-
main();
119+
main(false);
110120

111121
</script>
112122
</body>

0 commit comments

Comments
 (0)