Commit e4091760 by Skylar.Zheng Committed by xzyfer

Add a nice little progress bar while downloading binary

Forked from #1649.

This PR adds a small ascii progress bar to the binary download.
Stalled installations make up a steady background noise of our
issues. It's my hope that by making the binary fetch more
transparent we can stem the tide of those issues.

In order to address @saper's [concerns][1] this progress bar will
respect npm's `progress` config flag.

Downloading
```
Start downloading binary at https://github.com/sass/node-sass/releases/download/v3.8.0/darwin-x64-48_binding.node
Total 2602136 [===============          ] 1566256 60% 4.9s
```
Success
```
Start downloading binary at https://github.com/sass/node-sass/releases/download/v3.8.0/darwin-x64-48_binding.node
Total 2602136 [=========================] 2602136 100% 0.0s
Binary downloaded and installed at /tmp/node-sass/vendor/darwin-x64-48/binding.node
```

[1]: https://github.com/sass/node-sass/pull/1649#issuecomment-242720257
parent 9525d5cc
...@@ -64,6 +64,7 @@ ...@@ -64,6 +64,7 @@
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"nan": "^2.3.2", "nan": "^2.3.2",
"node-gyp": "^3.3.1", "node-gyp": "^3.3.1",
"progress": "^1.1.8",
"request": "^2.61.0", "request": "^2.61.0",
"sass-graph": "^2.1.1" "sass-graph": "^2.1.1"
}, },
......
...@@ -8,6 +8,7 @@ var fs = require('fs'), ...@@ -8,6 +8,7 @@ var fs = require('fs'),
path = require('path'), path = require('path'),
sass = require('../lib/extensions'), sass = require('../lib/extensions'),
request = require('request'), request = require('request'),
ProgressBar = require('progress'),
pkg = require('../package.json'); pkg = require('../package.json');
/** /**
...@@ -50,12 +51,14 @@ function download(url, dest, cb) { ...@@ -50,12 +51,14 @@ function download(url, dest, cb) {
var options = { var options = {
rejectUnauthorized: false, rejectUnauthorized: false,
proxy: getProxy(), proxy: getProxy(),
timeout: 1000, timeout: 60000,
headers: { headers: {
'User-Agent': getUserAgent(), 'User-Agent': getUserAgent(),
} }
}; };
console.log('Start downloading binary at', url);
try { try {
request(url, options, function(err, response) { request(url, options, function(err, response) {
if (err) { if (err) {
...@@ -70,6 +73,22 @@ function download(url, dest, cb) { ...@@ -70,6 +73,22 @@ function download(url, dest, cb) {
if (successful(response)) { if (successful(response)) {
response.pipe(fs.createWriteStream(dest)); 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) {
var bar = new ProgressBar('Total :total [:bar] :current :percent :etas', {
complete: '=',
incomplete: ' ',
width: 25,
total: parseInt(response.headers['content-length'])
});
response.on('data', function(chunk) {
bar.tick(chunk.length);
});
}
}); });
} catch (err) { } catch (err) {
cb(err); cb(err);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment