Skip to content

Commit

Permalink
feat(opencollective prompt): add prompt in postinstall script
Browse files Browse the repository at this point in the history
  • Loading branch information
misterdev committed Mar 11, 2019
1 parent 6aec4c3 commit dd9d528
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ yarn.lock

# source maps of docs
docs/**/*.map

# open collective badge
.lastocprint
1 change: 1 addition & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions bin/opencollective.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
31 changes: 12 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
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 All @@ -62,10 +63,6 @@
]
},
"jest": {
"testPathIgnorePatterns": [
"^.+\\.(ts)?$",
"/node_modules/"
],
"testEnvironment": "node",
"collectCoverage": true,
"coverageReporters": [
Expand Down Expand Up @@ -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",
Expand All @@ -170,4 +163,4 @@
"type": "opencollective",
"url": "https://opencollective.com/webpack"
}
}
}

0 comments on commit dd9d528

Please sign in to comment.