Skip to content

Instantly share code, notes, and snippets.

@brendanmckenzie
Last active July 5, 2019 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanmckenzie/029cfece81cdcffdbe23bd2c2431c846 to your computer and use it in GitHub Desktop.
Save brendanmckenzie/029cfece81cdcffdbe23bd2c2431c846 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const { exec } = require("child_process");
// this script aims to ensure that the master branch is never
// merged into another branch and thus will never end up in
// production
// installation:
// 1. yarn add -D husky
// 2. place this file in the root of the repository
// 3. add the following to package.json
// 4. create a .master file and commit it directly on the master branch
//
// ```
// "husky": {
// "hooks": {
// "pre-commit": "node .master.check.js",
// "pre-push": "node .master.check.js",
// "post-merge": "node .master.check.js"
// }
// }
// ```
exec("git rev-parse --abbrev-ref HEAD", (execErr, stdout) => {
if (execErr) {
// something went wrong executing the git command
console.error(`this script shouldn't be run here`);
process.exit(2);
}
const branch = stdout.trim();
fs.access(".master", fs.constants.F_OK, accessErr => {
if (accessErr) {
// .master file does not exists
if (branch === "master") {
console.warn(".master file does not exist");
console.warn("this is the master branch, so it should");
console.log("creating");
fs.writeFile(".master", "", () => {
process.exit(3);
});
} else {
console.info(".master file does not exist on this branch");
console.info("all good");
process.exit(0);
}
} else {
console.log(".master file exists");
if (branch !== "master") {
console.error(`this isn't the master branch so that's a problem`);
process.exit(1);
} else {
console.log(`branch is master, so that's fine`);
process.exit(0);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment