postinstall.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* eslint-disable max-len */
  2. var fs = require('fs');
  3. var os = require('os');
  4. var path = require('path');
  5. var env = process.env;
  6. var ADBLOCK = is(env.ADBLOCK);
  7. var COLOR = is(env.npm_config_color);
  8. var DISABLE_OPENCOLLECTIVE = is(env.DISABLE_OPENCOLLECTIVE);
  9. var SILENT = ['silent', 'error', 'warn'].indexOf(env.npm_config_loglevel) !== -1;
  10. var MINUTE = 60 * 1000;
  11. // you could add a PR with an env variable for your CI detection
  12. var CI = [
  13. 'BUILD_NUMBER',
  14. 'CI',
  15. 'CONTINUOUS_INTEGRATION',
  16. 'RUN_ID'
  17. ].some(function (it) { return is(env[it]); });
  18. var BANNER = '\u001B[96mThank you for using core-js (\u001B[94m https://github.com/zloirock/core-js \u001B[96m) for polyfilling JavaScript standard library!\u001B[0m\n\n' +
  19. '\u001B[96mThe project needs your help! Please consider supporting of core-js on Open Collective or Patreon: \u001B[0m\n' +
  20. '\u001B[96m>\u001B[94m https://opencollective.com/core-js \u001B[0m\n' +
  21. '\u001B[96m>\u001B[94m https://www.patreon.com/zloirock \u001B[0m\n\n' +
  22. '\u001B[96mAlso, the author of core-js (\u001B[94m https://github.com/zloirock \u001B[96m) is looking for a good job -)\u001B[0m\n';
  23. function is(it) {
  24. return !!it && it !== '0' && it !== 'false';
  25. }
  26. function isBannerRequired() {
  27. if (ADBLOCK || CI || DISABLE_OPENCOLLECTIVE || SILENT) return false;
  28. var file = path.join(os.tmpdir(), 'core-js-banners');
  29. var banners = [];
  30. try {
  31. var DELTA = Date.now() - fs.statSync(file).mtime;
  32. if (DELTA >= 0 && DELTA < MINUTE * 3) {
  33. banners = JSON.parse(fs.readFileSync(file, 'utf8'));
  34. if (banners.indexOf(BANNER) !== -1) return false;
  35. }
  36. } catch (error) {
  37. banners = [];
  38. }
  39. try {
  40. banners.push(BANNER);
  41. fs.writeFileSync(file, JSON.stringify(banners), 'utf8');
  42. } catch (error) { /* empty */ }
  43. return true;
  44. }
  45. function showBanner() {
  46. // eslint-disable-next-line no-console,no-control-regex
  47. console.log(COLOR ? BANNER : BANNER.replace(/\u001B\[\d+m/g, ''));
  48. }
  49. if (isBannerRequired()) showBanner();