Skip to content

Commit

Permalink
feat: core-js postinstall script
Browse files Browse the repository at this point in the history
  • Loading branch information
vltansky committed Apr 27, 2021
1 parent 7fc4f2a commit fd5c01d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ This project exists thanks to all the people who contribute. [[Contribute](CONTR

Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/swiper/contribute)]

#### `postinstall` message

Swiper is searching for bakers, so the package shows a message about it after installation. If it causes problems for you, you can disable it:

```
ADBLOCK=true npm install
// or
DISABLE_OPENCOLLECTIVE=true npm install
// or
npm install --loglevel silent
```

#### Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/swiper/contribute)]
Expand Down
4 changes: 2 additions & 2 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"swiper-bundle.esm.js"
],
"scripts": {
"postinstall": "node postinstall.js"
"postinstall": "node -e \"try{require('./postinstall')}catch(e){}\""
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -68,4 +68,4 @@
"dom7": "^3.0.0",
"ssr-window": "^3.0.0"
}
}
}
59 changes: 56 additions & 3 deletions package/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
console.log('\u001b[35m\u001b[1m','Love Swiper? Support Vladimir\'s work by donating or pledging on patreon:');
console.log('\u001b[22m\u001b[39m\u001b[32m','> On Patreon https://patreon.com/vladimirkharlampidi');
console.log('\u001b[22m\u001b[39m\u001b[32m','> On Open Collective https://opencollective.com/swiper');
/* eslint-disable max-len -- for better formatting */
var fs = require('fs');
var os = require('os');
var path = require('path');
var env = process.env;

var ADBLOCK = is(env.ADBLOCK);
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 OPEN_SOURCE_CONTRIBUTOR = is(env.OPEN_SOURCE_CONTRIBUTOR);
var MINUTE = 60 * 1000;

// you could add a PR with an env variable for your CI detection
var CI = [
'BUILD_NUMBER',
'CI',
'CONTINUOUS_INTEGRATION',
'DRONE',
'RUN_ID'
].some(function (it) { return is(env[it]); });

var BANNER = '\u001b[35m\u001b[1mLove Swiper? Support Vladimir\'s work by donating or pledging: \u001B[0m\n' +
'\u001b[22m\u001b[39m\u001b[32m> On Patreon https://patreon.com/vladimirkharlampidi \u001B[0m\n' +
'\u001b[22m\u001b[39m\u001b[32m> On Open Collective https://opencollective.com/swiper';

function is(it) {
return !!it && it !== '0' && it !== 'false';
}

function isBannerRequired() {
if (ADBLOCK || CI || DISABLE_OPENCOLLECTIVE || SILENT || OPEN_SOURCE_CONTRIBUTOR) return false;
var file = path.join(os.tmpdir(), 'swiper-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 -- output
console.log(COLOR ? BANNER : BANNER.replace(/\u001B\[\d+m/g, ''));
}

if (isBannerRequired()) showBanner();

0 comments on commit fd5c01d

Please sign in to comment.