2 * node-sass: scripts/install.js
5 var fs = require('fs'),
6 eol = require('os').EOL,
7 mkdir = require('mkdirp'),
8 path = require('path'),
9 sass = require('../lib/extensions'),
10 request = require('request'),
11 log = require('npmlog'),
12 downloadOptions = require('./util/downloadoptions');
15 * Download file, if succeeds save, if not delete
18 * @param {String} dest
19 * @param {Function} cb
23 function download(url, dest, cb) {
24 var reportError = function(err) {
27 if (err.code === 'ETIMEDOUT') {
28 if (err.connect === true) {
29 // timeout is hit while your client is attempting to establish a connection to a remote machine
30 timeoutMessge = 'Timed out attemping to establish a remote connection';
32 timeoutMessge = 'Timed out whilst downloading the prebuilt binary';
33 // occurs any time the server is too slow to send back a part of the response
37 cb(['Cannot download "', url, '": ', eol, eol,
38 typeof err.message === 'string' ? err.message : err, eol, eol,
39 timeoutMessge ? timeoutMessge + eol + eol : timeoutMessge,
40 'Hint: If github.com is not accessible in your location', eol,
41 ' try setting a proxy via HTTP_PROXY, e.g. ', eol, eol,
42 ' export HTTP_PROXY=http://example.com:1234',eol, eol,
43 'or configure npm proxy via', eol, eol,
44 ' npm config set proxy http://example.com:8080'].join(''));
47 var successful = function(response) {
48 return response.statusCode >= 200 && response.statusCode < 300;
51 console.log('Downloading binary from', url);
54 request(url, downloadOptions(), function(err, response) {
57 } else if (!successful(response)) {
58 reportError(['HTTP error', response.statusCode, response.statusMessage].join(' '));
60 console.log('Download complete');
64 .on('response', function(response) {
65 var length = parseInt(response.headers['content-length'], 10);
66 var progress = log.newItem('', length);
68 if (successful(response)) {
69 response.pipe(fs.createWriteStream(dest));
72 // The `progress` is true by default. However if it has not
73 // been explicitly set it's `undefined` which is considered
74 // as far as npm is concerned.
75 if (process.env.npm_config_progress === 'true') {
78 response.on('data', function(chunk) {
79 progress.completeWork(chunk.length);
81 .on('end', progress.finish);
90 * Check and download binary
95 function checkAndDownloadBinary() {
96 if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
97 console.log('Skipping downloading binaries on CI builds');
101 var cachedBinary = sass.getCachedBinary(),
102 cachePath = sass.getBinaryCachePath(),
103 binaryPath = sass.getBinaryPath();
105 if (sass.hasBinary(binaryPath)) {
106 console.log('node-sass build', 'Binary found at', binaryPath);
111 mkdir.sync(path.dirname(binaryPath));
113 console.error('Unable to save binary', path.dirname(binaryPath), ':', err);
118 console.log('Cached binary found at', cachedBinary);
119 fs.createReadStream(cachedBinary).pipe(fs.createWriteStream(binaryPath));
123 download(sass.getBinaryUrl(), binaryPath, function(err) {
129 console.log('Binary saved to', binaryPath);
131 cachedBinary = path.join(cachePath, sass.getBinaryName());
134 console.log('Caching binary to', cachedBinary);
137 mkdir.sync(path.dirname(cachedBinary));
138 fs.createReadStream(binaryPath)
139 .pipe(fs.createWriteStream(cachedBinary))
140 .on('error', function (err) {
141 console.log('Failed to cache binary:', err);
144 console.log('Failed to cache binary:', err);
151 * If binary does not exist, download it
154 checkAndDownloadBinary();