diff --git a/bin/cli.js b/bin/cli.js index b0dc2649d2d..40cef49f4c9 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -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; diff --git a/bin/opencollective.js b/bin/opencollective.js new file mode 100644 index 00000000000..a6266a25b4e --- /dev/null +++ b/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(); diff --git a/package.json b/package.json index 4e7f1393bb6..06baa84f421 100644 --- a/package.json +++ b/package.json @@ -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",