postinstall.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var fs = require('fs');
  2. var os = require('os');
  3. var path = require('path');
  4. var env = process.env;
  5. var ADBLOCK = is(env.ADBLOCK);
  6. var COLOR = is(env.npm_config_color);
  7. var DISABLE_OPENCOLLECTIVE = is(env.DISABLE_OPENCOLLECTIVE);
  8. var SILENT = ['silent', 'error', 'warn'].indexOf(env.npm_config_loglevel) !== -1;
  9. var OPEN_SOURCE_CONTRIBUTOR = is(env.OPEN_SOURCE_CONTRIBUTOR);
  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. 'DRONE',
  17. 'RUN_ID'
  18. ].some(function (it) { return is(env[it]); });
  19. 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' +
  20. '\u001B[96mThe project needs your help! Please consider supporting of core-js:\u001B[0m\n' +
  21. '\u001B[96m>\u001B[94m https://opencollective.com/core-js \u001B[0m\n' +
  22. '\u001B[96m>\u001B[94m https://patreon.com/zloirock \u001B[0m\n' +
  23. '\u001B[96m>\u001B[94m bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz \u001B[0m\n\n' +
  24. '\u001B[96mAlso, the author of core-js (\u001B[94m https://github.com/zloirock \u001B[96m) is looking for a good job -)\u001B[0m\n';
  25. function is(it) {
  26. return !!it && it !== '0' && it !== 'false';
  27. }
  28. function isBannerRequired() {
  29. if (ADBLOCK || CI || DISABLE_OPENCOLLECTIVE || SILENT || OPEN_SOURCE_CONTRIBUTOR) return false;
  30. var file = path.join(os.tmpdir(), 'core-js-banners');
  31. var banners = [];
  32. try {
  33. var DELTA = Date.now() - fs.statSync(file).mtime;
  34. if (DELTA >= 0 && DELTA < MINUTE * 3) {
  35. banners = JSON.parse(fs.readFileSync(file));
  36. if (banners.indexOf(BANNER) !== -1) return false;
  37. }
  38. } catch (error) {
  39. banners = [];
  40. }
  41. try {
  42. banners.push(BANNER);
  43. fs.writeFileSync(file, JSON.stringify(banners), 'utf8');
  44. } catch (error) { /* empty */ }
  45. return true;
  46. }
  47. function showBanner() {
  48. // eslint-disable-next-line no-console, regexp/no-control-character -- output
  49. console.log(COLOR ? BANNER : BANNER.replace(/\u001B\[\d+m/g, ''));
  50. }
  51. if (isBannerRequired()) showBanner();