Skip to content

Instantly share code, notes, and snippets.

@suhas-karanth
Last active July 31, 2016 11:21
Show Gist options
  • Save suhas-karanth/b815177fc3f8a385f4292a4b4a5f3097 to your computer and use it in GitHub Desktop.
Save suhas-karanth/b815177fc3f8a385f4292a4b4a5f3097 to your computer and use it in GitHub Desktop.
Eslint validation with pre-commit
{
"name": "example-app",
"description": "Example package file for demonstrating pre-commit-eslint",
"version": "0.0.1",
"author": "Suhas Karanth",
"repository": {
"type": "git",
"url": "git://github.com/suhas-karanth/example-app.git"
},
"engines": {
"node": ">=6.3.0"
},
"scripts": {
"eslint": "./pre-commit-eslint.sh",
"test": "mocha -R spec"
},
"pre-commit": ["eslint", "test"],
"devDependencies": {
"eslint": "^3.1.1",
"mocha": "*",
"pre-commit": "^1.1.3"
}
}
#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
if [[ "$STAGED_FILES" = "" ]]; then
echo "Currently there are no files staged in git for validation."
echo -e "Run 'git help add' for more on staging files for a commit.\n"
exit 0
fi
GREEN='\033[32m'
RED_BG='\033[41m'
GREEN_BG='\033[42m'
NC='\033[0m'
PASS=true
echo -e "\nValidating Javascript with eslint:"
# Check for eslint
which eslint &> /dev/null
if [[ "$?" == 1 ]]; then
echo -e "\t${RED_BG}Please install ESlint${NC}"
exit 1
fi
for FILE in ${STAGED_FILES}
do
eslint "$FILE"
if [[ "$?" == 0 ]]; then
echo -e "\t${GREEN}Passed: $FILE${NC}"
else
echo -e "\t${RED_BG}Failed: $FILE${NC}"
PASS=false
fi
done
echo -e "Javascript validation completed."
if ! ${PASS}; then
echo -e "\n${RED_BG}ESlint validation failed!${NC} Fix the ESLint errors and try again. You can run ESLint validation manually via 'npm run eslint'."
exit 1
else
echo -e "\n${GREEN_BG}ESlint validation succeeded :)${NC}"
fi
exit $?

The test script and pre-commit-eslint.sh are triggered before commit. If there are any eslint validation errors or if any unit test fails, commit will fail:

suhas:~/workspace/node-dogstatsd$ git commit

Validating Javascript with eslint:

/home/suhas/workspace/node-dogstatsd/lib/statsd.js
  30:9  error  'x' is defined but never used  no-unused-vars

✖ 1 problem (1 error, 0 warnings)

	Failed: lib/statsd.js
	Passed: test/test_statsd.js

Javascript validation completed!

Eslint validation failed! Fix the ESLint errors and try again. You can run ESLint validation manually via 'npm run eslint'.
pre-commit: 
pre-commit: We've failed to pass the specified git pre-commit hooks as the `eslint`
pre-commit: hook returned an exit code (1). If you're feeling adventurous you can
pre-commit: skip the git pre-commit hooks by adding the following flags to your commit:
pre-commit: 
pre-commit:   git commit -n (or --no-verify)
pre-commit: 
pre-commit: This is ill-advised since the commit is broken.
pre-commit:

Make sure the script is executable chmod +x pre-commit-eslint.sh. The script was originally shared here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment