Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download progress bar using progress. #80

Merged
merged 2 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"download": "^5.0.3",
"file-exists": "^2.0.0",
"merge": "^1.2.0",
"multimeter": "^0.1.1",
"progress": "^2.0.3",
"rimraf": "^2.2.8",
"semver": "^5.1.0",
"yargs": "^3.2.1"
Expand Down
36 changes: 32 additions & 4 deletions scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var download = require('download');
var rimraf = require('rimraf');
var semver = require('semver');
var createBar = require('multimeter')(process);
var ProgressBar = require('progress')
var path = require('path');
var fs = require('fs');
var merge = require('merge');
Expand Down Expand Up @@ -79,7 +79,7 @@ if (!url) logError('Could not find a compatible version of nw.js to download for
var dest = path.resolve(__dirname, '..', 'nwjs');
rimraf.sync(dest);

var bar = createBar({ before: url + ' [' });
var bar = new ProgressBar(url + ' [:bar] :current/:totalM', {total: 100, clear: true});

var total = 0;
var progress = 0;
Expand All @@ -103,7 +103,35 @@ if( parsedUrl.protocol == 'file:' ) {
.use( Decompress.targz(decompressOptions) )
.run( cb );
} else {
var progress = {
total: null,
downloaded: 0,
start: function (response) {
this.total = parseInt(response.headers['content-length']);
bar.total = (this.total / 1000000).toFixed(2);
},
recieved: function (chunk) {
this.downloaded += chunk.length;
if (this.total) {
bar.update(this.downloaded / this.total);
}
}
};

download(url, dest, merge({ extract: true }, decompressOptions))
.then(function() {cb();})
.catch(function(e) {cb(e);});
.on('response', function (response) {
progress.start(response);
})
.on('data', function (chunk) {
progress.recieved(chunk);
})
.on('end', function () {
bar.terminate();
})
.then(function () {
cb();
})
.catch(function (err) {
cb(err);
});
}