install.js 4.3 KB

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