postinstall.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. /* eslint-disable node/no-sync -- avoiding overcomplicating */
  3. /* eslint-disable unicorn/prefer-node-protocol -- ancient env possible */
  4. var fs = require('fs');
  5. var os = require('os');
  6. var path = require('path');
  7. var env = process.env;
  8. var ADBLOCK = is(env.ADBLOCK);
  9. var COLOR = is(env.npm_config_color);
  10. var DISABLE_OPENCOLLECTIVE = is(env.DISABLE_OPENCOLLECTIVE);
  11. var SILENT = ['silent', 'error', 'warn'].indexOf(env.npm_config_loglevel) !== -1;
  12. var OPEN_SOURCE_CONTRIBUTOR = is(env.OPEN_SOURCE_CONTRIBUTOR);
  13. var MINUTE = 60 * 1000;
  14. // you could add a PR with an env variable for your CI detection
  15. var CI = [
  16. 'BUILD_NUMBER',
  17. 'CI',
  18. 'CONTINUOUS_INTEGRATION',
  19. 'DRONE',
  20. 'RUN_ID'
  21. ].some(function (it) { return is(env[it]); });
  22. 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' +
  23. '\u001B[96mThe project needs your help! Please consider supporting core-js:\u001B[0m\n' +
  24. '\u001B[96m>\u001B[94m https://opencollective.com/core-js \u001B[0m\n' +
  25. '\u001B[96m>\u001B[94m https://patreon.com/zloirock \u001B[0m\n' +
  26. '\u001B[96m>\u001B[94m https://boosty.to/zloirock \u001B[0m\n' +
  27. '\u001B[96m>\u001B[94m bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz \u001B[0m\n\n' +
  28. '\u001B[96mI highly recommend reading this:\u001B[94m https://github.com/zloirock/core-js/blob/master/docs/2023-02-14-so-whats-next.md \u001B[96m\u001B[0m\n';
  29. function is(it) {
  30. return !!it && it !== '0' && it !== 'false';
  31. }
  32. function isBannerRequired() {
  33. if (ADBLOCK || CI || DISABLE_OPENCOLLECTIVE || SILENT || OPEN_SOURCE_CONTRIBUTOR) return false;
  34. var file = path.join(os.tmpdir(), 'core-js-banners');
  35. var banners = [];
  36. try {
  37. var DELTA = Date.now() - fs.statSync(file).mtime;
  38. if (DELTA >= 0 && DELTA < MINUTE * 3) {
  39. banners = JSON.parse(fs.readFileSync(file));
  40. if (banners.indexOf(BANNER) !== -1) return false;
  41. }
  42. } catch (error) {
  43. banners = [];
  44. }
  45. try {
  46. banners.push(BANNER);
  47. fs.writeFileSync(file, JSON.stringify(banners), 'utf8');
  48. } catch (error) { /* empty */ }
  49. return true;
  50. }
  51. function showBanner() {
  52. // eslint-disable-next-line no-console, regexp/no-control-character -- output
  53. console.log(COLOR ? BANNER : BANNER.replace(/\u001B\[\d+m/g, ''));
  54. }
  55. if (isBannerRequired()) showBanner();