Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.