Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Add a nice little progress bar while downloading binary #1694

Merged
merged 2 commits into from
Sep 4, 2016
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"mkdirp": "^0.5.1",
"nan": "^2.3.2",
"node-gyp": "^3.3.1",
"npmlog": "^4.0.0",
"request": "^2.61.0",
"sass-graph": "^2.1.1"
},
Expand Down
18 changes: 18 additions & 0 deletions scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var fs = require('fs'),
path = require('path'),
sass = require('../lib/extensions'),
request = require('request'),
log = require('npmlog'),
pkg = require('../package.json');

/**
Expand Down Expand Up @@ -56,6 +57,8 @@ function download(url, dest, cb) {
}
};

console.log('Start downloading binary at', url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the console.log intentional? Not sure if the log.newGroup makes sense here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it was. The user wont get any progress output until we get a response which won't happen for things like connection timeouts.

Tracker groups not appear to produce any output. I could be wrong, the docs aren't useful.


try {
request(url, options, function(err, response) {
if (err) {
Expand All @@ -67,9 +70,24 @@ function download(url, dest, cb) {
}
})
.on('response', function(response) {
var length = parseInt(response.headers['content-length'], 10);
var progress = log.newItem(url, length);

if (successful(response)) {
response.pipe(fs.createWriteStream(dest));
}

// The `progress` is true by default. However if it has not
// been explicitly set it's `undefined` which is considered
// as far as npm is concerned.
if (process.env.npm_config_progress !== false) {
log.enableProgress();

response.on('data', function(chunk) {
progress.completeWork(chunk.length);
})
.on('end', progress.finish);
}
});
} catch (err) {
cb(err);
Expand Down