install.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*!
  2. * node-sass: scripts/install.js
  3. */
  4. var fs = require('fs'),
  5. eol = require('os').EOL,
  6. mkdir = require('mkdirp'),
  7. path = require('path'),
  8. sass = require('../lib/extensions'),
  9. request = require('request'),
  10. log = require('npmlog'),
  11. downloadOptions = require('./util/downloadoptions');
  12. /**
  13. * Download file, if succeeds save, if not delete
  14. *
  15. * @param {String} url
  16. * @param {String} dest
  17. * @param {Function} cb
  18. * @api private
  19. */
  20. function download(url, dest, cb) {
  21. var reportError = function(err) {
  22. var timeoutMessge;
  23. if (err.code === 'ETIMEDOUT') {
  24. if (err.connect === true) {
  25. // timeout is hit while your client is attempting to establish a connection to a remote machine
  26. timeoutMessge = 'Timed out attemping to establish a remote connection';
  27. } else {
  28. timeoutMessge = 'Timed out whilst downloading the prebuilt binary';
  29. // occurs any time the server is too slow to send back a part of the response
  30. }
  31. }
  32. cb(['Cannot download "', url, '": ', eol, eol,
  33. typeof err.message === 'string' ? err.message : err, eol, eol,
  34. timeoutMessge ? timeoutMessge + eol + eol : timeoutMessge,
  35. 'Hint: If github.com is not accessible in your location', eol,
  36. ' try setting a proxy via HTTP_PROXY, e.g. ', eol, eol,
  37. ' export HTTP_PROXY=http://example.com:1234',eol, eol,
  38. 'or configure npm proxy via', eol, eol,
  39. ' npm config set proxy http://example.com:8080'].join(''));
  40. };
  41. var successful = function(response) {
  42. return response.statusCode >= 200 && response.statusCode < 300;
  43. };
  44. console.log('Downloading binary from', url);
  45. try {
  46. request(url, downloadOptions(), function(err, response, buffer) {
  47. if (err) {
  48. reportError(err);
  49. } else if (!successful(response)) {
  50. reportError(['HTTP error', response.statusCode, response.statusMessage].join(' '));
  51. } else {
  52. console.log('Download complete');
  53. if (successful(response)) {
  54. fs.createWriteStream(dest)
  55. .on('error', cb)
  56. .end(buffer, cb);
  57. } else {
  58. cb();
  59. }
  60. }
  61. })
  62. .on('response', function(response) {
  63. var length = parseInt(response.headers['content-length'], 10);
  64. var progress = log.newItem('', length);
  65. // The `progress` is true by default. However if it has not
  66. // been explicitly set it's `undefined` which is considered
  67. // as far as npm is concerned.
  68. if (process.env.npm_config_progress === 'true') {
  69. log.enableProgress();
  70. response.on('data', function(chunk) {
  71. progress.completeWork(chunk.length);
  72. })
  73. .on('end', progress.finish);
  74. }
  75. });
  76. } catch (err) {
  77. cb(err);
  78. }
  79. }
  80. /**
  81. * Check and download binary
  82. *
  83. * @api private
  84. */
  85. function checkAndDownloadBinary() {
  86. if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
  87. console.log('Skipping downloading binaries on CI builds');
  88. return;
  89. }
  90. var cachedBinary = sass.getCachedBinary(),
  91. cachePath = sass.getBinaryCachePath(),
  92. binaryPath = sass.getBinaryPath();
  93. if (sass.hasBinary(binaryPath)) {
  94. console.log('node-sass build', 'Binary found at', binaryPath);
  95. return;
  96. }
  97. try {
  98. mkdir.sync(path.dirname(binaryPath));
  99. } catch (err) {
  100. console.error('Unable to save binary', path.dirname(binaryPath), ':', err);
  101. return;
  102. }
  103. if (cachedBinary) {
  104. console.log('Cached binary found at', cachedBinary);
  105. fs.createReadStream(cachedBinary).pipe(fs.createWriteStream(binaryPath));
  106. return;
  107. }
  108. download(sass.getBinaryUrl(), binaryPath, function(err) {
  109. if (err) {
  110. console.error(err);
  111. return;
  112. }
  113. console.log('Binary saved to', binaryPath);
  114. cachedBinary = path.join(cachePath, sass.getBinaryName());
  115. if (cachePath) {
  116. console.log('Caching binary to', cachedBinary);
  117. try {
  118. mkdir.sync(path.dirname(cachedBinary));
  119. fs.createReadStream(binaryPath)
  120. .pipe(fs.createWriteStream(cachedBinary))
  121. .on('error', function (err) {
  122. console.log('Failed to cache binary:', err);
  123. });
  124. } catch (err) {
  125. console.log('Failed to cache binary:', err);
  126. }
  127. }
  128. });
  129. }
  130. /**
  131. * If binary does not exist, download it
  132. */
  133. checkAndDownloadBinary();