Skip to content

Instantly share code, notes, and snippets.

@stowball
Last active May 3, 2018 05:58
Show Gist options
  • Save stowball/9bb93aa2578a2b094b8fccbe136da61b to your computer and use it in GitHub Desktop.
Save stowball/9bb93aa2578a2b094b8fccbe136da61b to your computer and use it in GitHub Desktop.
Pre-commit hook to lint and flow check files to be committed
#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$files" == "" ]; then
exit 0
fi
flow=$(npm run flow) # Change this to your flow script in package.json
eslint=$PWD/node_modules/.bin/eslint
flowfiles=""
flowerrors="false"
lintfiles=""
linterrors="false"
for file in ${files}; do
if [[ "$flow" == *"$file"* ]]; then
flowfiles+="\n$file"
flowerrors="true"
fi
done
for file in ${files}; do
lint=$($eslint -c .eslintrc ${file})
if [[ "$lint" == *"problem"* ]]; then
lintfiles+="\n$file"
linterrors="true"
fi
done
if [ "$flowerrors" == "true" ]; then
echo "$flow"
echo "\n\033[41m COMMIT FAILED: \033[0m Your commit contains flow errors in: $flowfiles"
fi
if [ "$linterrors" == "true" ]; then
$eslint -c .eslintrc ${files}
echo "\n\033[41m COMMIT FAILED: \033[0m Your commit contains lint errors and/or warnings in: $lintfiles"
fi
if [ "$flowerrors" == "true" ] || [ "$linterrors" == "true" ] ; then
exit 1
fi
@droopycom
Copy link

This does not work as you would expect, as it would run lint against the files in the working directory, rather than the actual committed files.

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