diff --git a/CHANGELOG.md b/CHANGELOG.md index a46f4624d52b..5b66ab89eb23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ ## Changelog +##### Unreleased [LEGACY] +- Show similar `postinstall` messages only once per `npm i`, [#597](https://github.com/zloirock/core-js/issues/597) + ##### 2.6.9 [LEGACY] - 2019.05.27 - Some fixes and improvements of the `postinstall` script like support `npm` color config ([#556](https://github.com/zloirock/core-js/issues/556)) or adding support of `ADBLOCK` env variable diff --git a/package.json b/package.json index e5760f454068..5ddba2d06ac7 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "promises-tests": "promises-aplus-tests tests/promises-aplus/adapter", "observables-tests": "node tests/observables/adapter && node tests/observables/adapter-library", "test": "npm run grunt clean copy && npm run lint && npm run grunt livescript client karma:default && npm run grunt library karma:library && npm run promises-tests && npm run observables-tests && lsc tests/commonjs", - "postinstall": "node scripts/postinstall || echo \"ignore\"" + "postinstall": "node postinstall || echo \"ignore\"" }, "license": "MIT", "keywords": [ diff --git a/postinstall.js b/postinstall.js new file mode 100644 index 000000000000..4b7481c1413b --- /dev/null +++ b/postinstall.js @@ -0,0 +1,49 @@ +/* eslint-disable max-len */ +var fs = require('fs'); +var os = require('os'); +var path = require('path'); +var env = process.env; + +var ADBLOCK = is(env.ADBLOCK); +var CI = is(env.CI); +var COLOR = is(env.npm_config_color); +var DISABLE_OPENCOLLECTIVE = is(env.DISABLE_OPENCOLLECTIVE); +var SILENT = ['silent', 'error', 'warn'].indexOf(env.npm_config_loglevel) !== -1; +var MINUTE = 60 * 1000; + +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' + + '\u001B[96mThe project needs your help! Please consider supporting of core-js on Open Collective or Patreon: \u001B[0m' + + '\u001B[96m>\u001B[94m https://opencollective.com/core-js \u001B[0m' + + '\u001B[96m>\u001B[94m https://www.patreon.com/zloirock \u001B[0m\n' + + '\u001B[96mAlso, the author of core-js (\u001B[94m https://github.com/zloirock \u001B[96m) is looking for a good job -)\u001B[0m\n'; + +function is(it) { + return !!it && it !== '0' && it !== 'false'; +} + +function isBannerRequired() { + if (ADBLOCK || CI || DISABLE_OPENCOLLECTIVE || SILENT) return false; + var file = path.join(os.tmpdir(), 'core-js-banners'); + var banners = []; + try { + var DELTA = Date.now() - fs.statSync(file).mtime; + if (DELTA >= 0 && DELTA < MINUTE * 3) { + banners = JSON.parse(fs.readFileSync(file, 'utf8')); + if (banners.indexOf(BANNER) !== -1) return false; + } + } catch (error) { + banners = []; + } + try { + banners.push(BANNER); + fs.writeFileSync(file, JSON.stringify(banners), 'utf8'); + } catch (error) { /* empty */ } + return true; +} + +function showBanner() { + // eslint-disable-next-line no-console,no-control-regex + console.log(COLOR ? BANNER : BANNER.replace(/\u001B\[\d+m/g, '')); +} + +if (isBannerRequired()) showBanner(); diff --git a/scripts/postinstall.js b/scripts/postinstall.js deleted file mode 100644 index e3b1c0d7467a..000000000000 --- a/scripts/postinstall.js +++ /dev/null @@ -1,23 +0,0 @@ -/* eslint-disable max-len */ -var env = process.env; -var ADBLOCK = is(env.ADBLOCK); -var CI = is(env.CI); -var COLOR = is(env.npm_config_color); -var SILENT = !!~['silent', 'error', 'warn'].indexOf(env.npm_config_loglevel); - -function is(it) { - return !!it && it !== '0' && it !== 'false'; -} - -function log(it) { - // eslint-disable-next-line no-console,no-control-regex - console.log(COLOR ? it : it.replace(/\u001B\[\d+m/g, '')); -} - -if (!ADBLOCK && !CI && !SILENT) { - log('\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'); - log('\u001B[96mThe project needs your help! Please consider supporting of core-js on Open Collective or Patreon: \u001B[0m'); - log('\u001B[96m>\u001B[94m https://opencollective.com/core-js \u001B[0m'); - log('\u001B[96m>\u001B[94m https://www.patreon.com/zloirock \u001B[0m\n'); - log('\u001B[96mAlso, the author of core-js (\u001B[94m https://github.com/zloirock \u001B[96m) is looking for a good job -)\u001B[0m\n'); -}