install.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var fs = require('fs'),
  2. path = require('path'),
  3. request = require('request'),
  4. mkdirp = require('mkdirp'),
  5. exec = require('shelljs').exec,
  6. npmconf = require('npmconf'),
  7. packageInfo = require('../package.json');
  8. require('../lib/extensions');
  9. /**
  10. * Download file, if succeeds save, if not delete
  11. *
  12. * @param {String} url
  13. * @param {String} dest
  14. * @param {Function} cb
  15. * @api private
  16. */
  17. function download(url, dest, cb) {
  18. applyProxy({ rejectUnauthorized: false }, function(options) {
  19. var returnError = function(err) {
  20. cb(typeof err.message === 'string' ? err.message : err);
  21. };
  22. request.get(url, options).on('response', function(response) {
  23. if (response.statusCode < 200 || response.statusCode >= 300) {
  24. returnError('Can not download file from ' + url);
  25. return;
  26. }
  27. response.pipe(fs.createWriteStream(dest));
  28. }).on('error', returnError);
  29. });
  30. }
  31. /**
  32. * Get applyProxy settings
  33. *
  34. * @param {Object} options
  35. * @param {Function} cb
  36. * @api private
  37. */
  38. function applyProxy(options, cb) {
  39. npmconf.load({}, function (er, conf) {
  40. var proxyUrl;
  41. if (!er) {
  42. ['https-proxy', 'proxy', 'http-proxy'].some(function(setting) {
  43. var npmProxyUrl = conf.get(setting);
  44. if (npmProxyUrl) {
  45. proxyUrl = npmProxyUrl;
  46. return true;
  47. }
  48. });
  49. }
  50. if (!proxyUrl) {
  51. var env = process.env;
  52. proxyUrl = env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy;
  53. }
  54. options.proxy = proxyUrl;
  55. cb(options);
  56. });
  57. }
  58. /**
  59. * Check if binaries exists
  60. *
  61. * @api private
  62. */
  63. function checkAndFetchBinaries() {
  64. fs.exists(path.join(__dirname, '..', 'vendor', process.sassBinaryName), function (exists) {
  65. if (exists) {
  66. return;
  67. }
  68. fetch();
  69. });
  70. }
  71. /**
  72. * Fetch binaries
  73. *
  74. * @api private
  75. */
  76. function fetch() {
  77. var url = [
  78. 'https://raw.githubusercontent.com/sass/node-sass-binaries/v',
  79. packageInfo.version, '/', process.sassBinaryName,
  80. '/binding.node'
  81. ].join('');
  82. var dir = path.join(__dirname, '..', 'vendor', process.sassBinaryName);
  83. var dest = path.join(dir, 'binding.node');
  84. mkdirp(dir, function(err) {
  85. if (err) {
  86. console.error(err);
  87. return;
  88. }
  89. download(url, dest, function(err) {
  90. if (err) {
  91. console.error(err);
  92. return;
  93. }
  94. console.log('Binary downloaded and installed at ' + dest);
  95. });
  96. });
  97. }
  98. /**
  99. * Skip if CI
  100. */
  101. if (process.env.SKIP_SASS_BINARY_DOWNLOAD_FOR_CI) {
  102. console.log('Skipping downloading binaries on CI builds');
  103. return;
  104. }
  105. /**
  106. * Run
  107. */
  108. checkAndFetchBinaries();