From dd9d5285c19a2857e18033468b7420ede392f6e9 Mon Sep 17 00:00:00 2001 From: "devid.farinelli@gmail.com" Date: Mon, 11 Mar 2019 16:56:53 +0100 Subject: [PATCH] feat(opencollective prompt): add prompt in postinstall script --- .gitignore | 3 +++ bin/cli.js | 1 + bin/opencollective.js | 47 +++++++++++++++++++++++++++++++++++++++++++ package.json | 31 +++++++++++----------------- 4 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 bin/opencollective.js diff --git a/.gitignore b/.gitignore index ef696edb785..f84c225c2ba 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,6 @@ yarn.lock # source maps of docs docs/**/*.map + +# open collective badge +.lastocprint diff --git a/bin/cli.js b/bin/cli.js index b0dc2649d2d..bd2f3602565 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -473,6 +473,7 @@ 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}`); + require("./opencollective"); } if (!options.watch && stats.hasErrors()) { process.exitCode = 2; diff --git a/bin/opencollective.js b/bin/opencollective.js new file mode 100644 index 00000000000..cb9f08f80d7 --- /dev/null +++ b/bin/opencollective.js @@ -0,0 +1,47 @@ +const child_process = require("child_process"); +const chalk = require("chalk"); +const fs = require("fs"); +const path = require("path"); + +// 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 = process.platform === "win32" ? 80 : parseInt(child_process.execSync("tput cols").toString()); + // 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"); +} + + +const now = new Date(); +if (now.getDay() == 1 ) { + const lastPrintFile = path.resolve(__dirname, "../.lastocprint"); + fs.readFile(lastPrintFile, "utf8", (err, lastPrint = 0) => { + if (err && err.code !== "ENOENT") return; + if (now - lastPrint > 6 * 24 * 60 * 60 * 1000) { + printBadge(); + fs.writeFileSync(lastPrintFile, now); + } + }); +} \ No newline at end of file diff --git a/package.json b/package.json index b9430b21ae3..14c7b1e2f58 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", @@ -62,10 +63,6 @@ ] }, "jest": { - "testPathIgnorePatterns": [ - "^.+\\.(ts)?$", - "/node_modules/" - ], "testEnvironment": "node", "collectCoverage": true, "coverageReporters": [ @@ -120,44 +117,40 @@ "loader-utils": "^1.1.0", "supports-color": "^5.5.0", "v8-compile-cache": "^2.0.2", - "yargs": "^12.0.5" + "yargs": "^12.0.4" }, "peerDependencies": { "webpack": "4.x.x" }, "devDependencies": { - "@babel/preset-env": "^7.3.4", - "@babel/register": "^7.0.0", "@commitlint/cli": "^7.2.1", "@commitlint/config-lerna-scopes": "^7.2.1", - "@commitlint/prompt-cli": "^7.5.0", + "@commitlint/prompt-cli": "^7.2.1", "@commitlint/travis-cli": "^7.2.1", - "@types/jest": "^23.3.14", + "@types/jest": "^23.3.9", "@types/node": "^10.12.9", "babel-preset-env": "^1.7.0", - "babel-preset-jest": "^24.3.0", "bundlesize": "^0.17.0", "codecov": "^3.1.0", - "commitizen": "^3.0.7", + "commitizen": "^3.0.4", "commitlint-config-cz": "^0.10.1", "conventional-changelog-cli": "^2.0.11", "cz-customizable": "^5.3.0", "eslint": "^5.9.0", "eslint-plugin-node": "^8.0.0", - "esm": "^3.2.14", "execa": "^1.0.0", "husky": "^1.1.4", - "jest": "^24.3.1", - "jest-cli": "^24.3.1", - "jest-junit": "^6.3.0", - "lerna": "^3.13.1", + "jest": "^23.6.0", + "jest-cli": "^23.6.0", + "jest-junit": "^5.0.0", + "lerna": "^3.10.7", "lint-staged": "7.x.x", - "nyc": "^13.3.0", + "nyc": "^13.1.0", "prettier-eslint-cli": "^4.7.1", "readable-stream": "^3.0.6", "rimraf": "^2.6.2", "schema-utils": "^1.0.0", - "ts-jest": "^23.10.5", + "ts-jest": "^23.10.4", "ts-node": "^7.0.1", "tslint": "^5.11.0", "typedoc": "^0.13.0", @@ -170,4 +163,4 @@ "type": "opencollective", "url": "https://opencollective.com/webpack" } -} +} \ No newline at end of file