Skip to content

Instantly share code, notes, and snippets.

@kkleidal
Created July 24, 2016 19:35
Show Gist options
  • Save kkleidal/31ac2c1d601cf74a1d4a619adfe03cfa to your computer and use it in GitHub Desktop.
Save kkleidal/31ac2c1d601cf74a1d4a619adfe03cfa to your computer and use it in GitHub Desktop.
Express.js linting and beautifying pre-commit hook
#!/bin/sh
ln -s $(pwd)/pre-commit.bash .git/hooks/pre-commit
{
...
"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/*"
}
}
#!/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