install.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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) {
  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. cb();
  54. }
  55. })
  56. .on('response', function(response) {
  57. var length = parseInt(response.headers['content-length'], 10);
  58. var progress = log.newItem('', length);
  59. if (successful(response)) {
  60. response.pipe(fs.createWriteStream(dest));
  61. }
  62. // The `progress` is true by default. However if it has not
  63. // been explicitly set it's `undefined` which is considered
  64. // as far as npm is concerned.
  65. if (process.env.npm_config_progress === 'true') {
  66. log.enableProgress();
  67. response.on('data', function(chunk) {
  68. progress.completeWork(chunk.length);
  69. })
  70. .on('end', progress.finish);
  71. }
  72. });
  73. } catch (err) {
  74. cb(err);
  75. }
  76. }
  77. /**
  78. * Check and download binary
  79. *
  80. * @api private
  81. */
  82. function checkAndDownloadBinary() {
  83. if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
  84. console.log('Skipping downloading binaries on CI builds');
  85. return;
  86. }
  87. var cachedBinary = sass.getCachedBinary(),
  88. cachePath = sass.getBinaryCachePath(),
  89. binaryPath = sass.getBinaryPath();
  90. if (sass.hasBinary(binaryPath)) {
  91. console.log('node-sass build', 'Binary found at', binaryPath);
  92. return;
  93. }
  94. try {
  95. mkdir.sync(path.dirname(binaryPath));
  96. } catch (err) {
  97. console.error('Unable to save binary', path.dirname(binaryPath), ':', err);
  98. return;
  99. }
  100. if (cachedBinary) {
  101. console.log('Cached binary found at', cachedBinary);
  102. fs.createReadStream(cachedBinary).pipe(fs.createWriteStream(binaryPath));
  103. return;
  104. }
  105. download(sass.getBinaryUrl(), binaryPath, function(err) {
  106. if (err) {
  107. console.error(err);
  108. return;
  109. }
  110. console.log('Binary saved to', binaryPath);
  111. cachedBinary = path.join(cachePath, sass.getBinaryName());
  112. if (cachePath) {
  113. console.log('Caching binary to', cachedBinary);
  114. try {
  115. mkdir.sync(path.dirname(cachedBinary));
  116. fs.createReadStream(binaryPath)
  117. .pipe(fs.createWriteStream(cachedBinary))
  118. .on('error', function (err) {
  119. console.log('Failed to cache binary:', err);
  120. });
  121. } catch (err) {
  122. console.log('Failed to cache binary:', err);
  123. }
  124. }
  125. });
  126. }
  127. /**
  128. * If binary does not exist, download it
  129. */
  130. checkAndDownloadBinary();