Initial commit
[yaffs-website] / node_modules / node-sass / scripts / install.js
1 /*!
2  * node-sass: scripts/install.js
3  */
4
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');
13
14 /**
15  * Download file, if succeeds save, if not delete
16  *
17  * @param {String} url
18  * @param {String} dest
19  * @param {Function} cb
20  * @api private
21  */
22
23 function download(url, dest, cb) {
24   var reportError = function(err) {
25     var timeoutMessge;
26
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';
31       } else {
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
34       }
35
36     }
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(''));
45   };
46
47   var successful = function(response) {
48     return response.statusCode >= 200 && response.statusCode < 300;
49   };
50
51   console.log('Downloading binary from', url);
52
53   try {
54     request(url, downloadOptions(), function(err, response) {
55       if (err) {
56         reportError(err);
57       } else if (!successful(response)) {
58         reportError(['HTTP error', response.statusCode, response.statusMessage].join(' '));
59       } else {
60         console.log('Download complete');
61         cb();
62       }
63     })
64     .on('response', function(response) {
65       var length = parseInt(response.headers['content-length'], 10);
66       var progress = log.newItem('', length);
67
68       if (successful(response)) {
69         response.pipe(fs.createWriteStream(dest));
70       }
71
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') {
76         log.enableProgress();
77
78         response.on('data', function(chunk) {
79           progress.completeWork(chunk.length);
80         })
81         .on('end', progress.finish);
82       }
83     });
84   } catch (err) {
85     cb(err);
86   }
87 }
88
89 /**
90  * Check and download binary
91  *
92  * @api private
93  */
94
95 function checkAndDownloadBinary() {
96   if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
97     console.log('Skipping downloading binaries on CI builds');
98     return;
99   }
100
101   var cachedBinary = sass.getCachedBinary(),
102     cachePath = sass.getBinaryCachePath(),
103     binaryPath = sass.getBinaryPath();
104
105   if (sass.hasBinary(binaryPath)) {
106     console.log('node-sass build', 'Binary found at', binaryPath);
107     return;
108   }
109
110   try {
111     mkdir.sync(path.dirname(binaryPath));
112   } catch (err) {
113     console.error('Unable to save binary', path.dirname(binaryPath), ':', err);
114     return;
115   }
116
117   if (cachedBinary) {
118     console.log('Cached binary found at', cachedBinary);
119     fs.createReadStream(cachedBinary).pipe(fs.createWriteStream(binaryPath));
120     return;
121   }
122
123   download(sass.getBinaryUrl(), binaryPath, function(err) {
124     if (err) {
125       console.error(err);
126       return;
127     }
128
129     console.log('Binary saved to', binaryPath);
130
131     cachedBinary = path.join(cachePath, sass.getBinaryName());
132
133     if (cachePath) {
134       console.log('Caching binary to', cachedBinary);
135
136       try {
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);
142           });
143       } catch (err) {
144         console.log('Failed to cache binary:', err);
145       }
146     }
147   });
148 }
149
150 /**
151  * If binary does not exist, download it
152  */
153
154 checkAndDownloadBinary();