Skip to content

Commit

Permalink
Merge pull request #755 from misterdev/feature/opencollective-postins…
Browse files Browse the repository at this point in the history
…tall

enh(Add open collective prompt)
  • Loading branch information
evenstensberg committed Mar 16, 2019
2 parents 05b92af + 55992a4 commit 9597377
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bin/cli.js
Expand Up @@ -473,6 +473,26 @@ For more information, see https://webpack.js.org/api/cli/.`);
const statsString = stats.toString(outputOptions);
const delimiter = outputOptions.buildDelimiter ? `${outputOptions.buildDelimiter}\n` : "";
if (statsString) stdout.write(`${statsString}\n${delimiter}`);

/**
* Show a hint to donate to our Opencollective
* once a week, only on Monday
*/
const openCollectivePath = __dirname + "/opencollective.js";
const MONDAY = 1;
const SIX_DAYS = 518400000;
const now = new Date();
if (now.getDay() === MONDAY) {
const { statSync, utimesSync } = require("fs");
const lastPrint = statSync(openCollectivePath).atime;
const lastPrintTS = new Date(lastPrint).getTime();
const timeSinceLastPrint = now.getTime() - lastPrintTS;
if (timeSinceLastPrint > SIX_DAYS) {
require(openCollectivePath);
// On windows we need to manually update the atime
utimesSync(openCollectivePath, now, now);
}
}
}
if (!options.watch && stats.hasErrors()) {
process.exitCode = 2;
Expand Down
33 changes: 33 additions & 0 deletions bin/opencollective.js
@@ -0,0 +1,33 @@
const chalk = require("chalk");

// Only show emoji on OSx (Windows shell doesn't like them that much ¯\_(ツ)_/¯ )
function emoji(emoji) {
if (process.stdout.isTTY && process.platform === "darwin") {
return emoji;
} else {
return "";
}
}

function print(str = "", color = "dim") {
const terminalCols = 80;
// eslint-disable-next-line no-control-regex
const ansiEscapeSeq = /\u001b\[[0-9]{1,2}m/g;
const strLength = str.replace(ansiEscapeSeq, "").length;
const leftPaddingLength = Math.floor((terminalCols - strLength) / 2);
const leftPadding = " ".repeat(leftPaddingLength);
str = chalk[color](str);
console.log(leftPadding, str);
}

function printBadge() {
console.log("\n");
print(`${chalk.bold("Thanks for using")} ${chalk.bold.blue("Webpack!")}`);
print(`Please consider donating to our ${chalk.bold.blue("Open Collective")}`);
print("to help us maintain this package.");
console.log("\n\n");
print(`${emoji("👉")} ${chalk.bold.yellow(" Donate:")} ${chalk.reset.underline.yellow("https://opencollective.com/webpack/donate")}`);
console.log("\n");
}

printBadge();
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -36,6 +36,7 @@
"format": "prettier-eslint ./bin/*.js ./test/**/*.js ./packages/**/*.js --write",
"lint:codeOnly": "eslint \"{bin}/**/!(__testfixtures__)/*.js\" \"{bin}/**.js\"",
"lint": "eslint \"./bin/*.js\" \"./test/**/*.js\" \"packages/**/!(node_modules)/*.test.js\"",
"postinstall": "node ./bin/opencollective.js",
"pretest": "npm run build && npm run lint && npm run tslint",
"reportCoverage": "nyc report --reporter=json && codecov -f coverage/coverage-final.json --disable=gcov",
"test": "nyc jest --maxWorkers=4 --reporters=default --reporters=jest-junit",
Expand Down

0 comments on commit 9597377

Please sign in to comment.