-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
install.js
113 lines (91 loc) · 2.21 KB
/
install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*!
* node-sass: scripts/install.js
*/
var fs = require('fs'),
mkdir = require('mkdirp'),
npmconf = require('npmconf'),
path = require('path'),
request = require('request');
require('../lib/extensions');
/**
* Download file, if succeeds save, if not delete
*
* @param {String} url
* @param {String} dest
* @param {Function} cb
* @api private
*/
function download(url, dest, cb) {
applyProxy({ rejectUnauthorized: false }, function(options) {
var returnError = function(err) {
cb(typeof err.message === 'string' ? err.message : err);
};
request.get(url, options).on('response', function(response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
returnError(['Can not download file from:', url].join());
return;
}
response.pipe(fs.createWriteStream(dest));
cb();
}).on('error', returnError);
});
}
/**
* Get applyProxy settings
*
* @param {Object} options
* @param {Function} cb
* @api private
*/
function applyProxy(options, cb) {
npmconf.load({}, function (er, conf) {
var proxyUrl;
if (!er) {
proxyUrl = conf.get('https-proxy') ||
conf.get('proxy') ||
conf.get('http-proxy');
}
var env = process.env;
options.proxy = proxyUrl ||
env.HTTPS_PROXY ||
env.https_proxy ||
env.HTTP_PROXY ||
env.http_proxy;
cb(options);
});
}
/**
* Check and download binary
*
* @api private
*/
function checkAndDownloadBinary() {
try {
process.sass.getBinaryPath(true);
return;
} catch (e) { }
mkdir(path.dirname(process.sass.binaryPath), function(err) {
if (err) {
console.error(err);
return;
}
download(process.sass.binaryUrl, process.sass.binaryPath, function(err) {
if (err) {
console.error(err);
return;
}
console.log('Binary downloaded and installed at', process.sass.binaryPath);
});
});
}
/**
* Skip if CI
*/
if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
console.log('Skipping downloading binaries on CI builds');
return;
}
/**
* If binary does not exsit, download it
*/
checkAndDownloadBinary();