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

Improve install script #663

Merged
merged 2 commits into from
Feb 12, 2015
Merged
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
57 changes: 25 additions & 32 deletions scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ var fs = require('fs'),
path = require('path'),
request = require('request'),
mkdirp = require('mkdirp'),
exec = require('shelljs').exec;
exec = require('shelljs').exec,
npmconf = require('npmconf'),
packageInfo = require('../package.json');

require('../lib/extensions');

Expand All @@ -16,27 +18,17 @@ require('../lib/extensions');
*/

function download(url, dest, cb) {
var file = fs.createWriteStream(dest);

applyProxy({ rejectUnauthorized: false }, function(options) {
var returnError = function(err) {
fs.unlink(dest);
cb(typeof err.message === 'string' ? err.message : err);
};
var req = request.get(url, options).on('response', function(response) {
request.get(url, options).on('response', function(response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
returnError('Can not download file from ' + url);
return;
}
response.pipe(file);

file.on('finish', function() {
file.close(cb);
});
response.pipe(fs.createWriteStream(dest));
}).on('error', returnError);

req.end();
req.on('error', returnError);
});
}

Expand All @@ -49,25 +41,26 @@ function download(url, dest, cb) {
*/

function applyProxy(options, cb) {
require('npmconf').load({}, function (er, conf) {
var getProxyFromEnv = true;
['https-proxy', 'proxy', 'http-proxy'].forEach(function(setting) {
var proxyUrl = conf.get(setting);

if(proxyUrl && proxyUrl === require('url').parse(proxyUrl)) {
options.proxy = proxyUrl;
getProxyFromEnv = false;
cb(options);
return;
}
});
npmconf.load({}, function (er, conf) {
var proxyUrl;

if (!er) {
['https-proxy', 'proxy', 'http-proxy'].some(function(setting) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW this could be reduced to

var proxyUrl = ['https-proxy', 'proxy', 'http-proxy'].filter(function(setting) { 
  return conf.get(setting); 
})[0];

Copy link
Contributor

Choose a reason for hiding this comment

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

Or even simpler yet?

var env = process.env;
options.proxy = conf.get('https-proxy') || conf.get('proxy') || conf.get('http-proxy') || 
                env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;

var npmProxyUrl = conf.get(setting);
if (npmProxyUrl) {
proxyUrl = npmProxyUrl;
return true;
}
});
}

if(getProxyFromEnv) {
if (!proxyUrl) {
var env = process.env;
options.proxy = env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;

cb(options);
proxyUrl = env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;
}

options.proxy = proxyUrl;
cb(options);
});
}

Expand All @@ -77,7 +70,7 @@ function applyProxy(options, cb) {
* @api private
*/

function exists() {
function checkAndFetchBinaries() {
fs.exists(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function (exists) {
if (exists) {
return;
Expand All @@ -96,7 +89,7 @@ function exists() {
function fetch() {
var url = [
'https://raw.githubusercontent.com/sass/node-sass-binaries/v',
require('../package.json').version, '/', process.sassBinaryName,
packageInfo.version, '/', process.sassBinaryName,
'/binding.node'
].join('');
var dir = path.join(__dirname, '..', 'vendor', process.sassBinaryName);
Expand Down Expand Up @@ -132,4 +125,4 @@ if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
* Run
*/

exists();
checkAndFetchBinaries();