Created
July 24, 2016 19:35
-
-
Save kkleidal/31ac2c1d601cf74a1d4a619adfe03cfa to your computer and use it in GitHub Desktop.
Express.js linting and beautifying pre-commit hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
ln -s $(pwd)/pre-commit.bash .git/hooks/pre-commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
... | |
"dependencies": {}, | |
"devDependencies": { | |
"js-beautify": "~1.6.3", | |
"jshint": "~2.9.2" | |
}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node ./bin/www", | |
"lint": "./node_modules/jshint/bin/jshint *.js bin/* controllers/* models/* routes/*", | |
"beautify": "./node_modules/js-beautify/js/bin/js-beautify.js -rf *.js bin/* controllers/* models/* routes/*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# pre-commit.sh | |
git stash -q --keep-index | |
commit () { | |
git stash pop -q | |
exit 0 | |
} | |
abort () { | |
echo "ABORTING: Make sure you have linted and beautified the code." >&2 | |
git checkout . | |
git stash pop -q | |
exit 1 | |
} | |
# Test prospective commit | |
cd app | |
npm run-script lint || abort | |
BEFORE=$(git status) | |
npm run-script beautify || abort | |
AFTER=$(git status) | |
[ "$BEFORE" == "$AFTER" ] || abort | |
cd - | |
commit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment