Skip to content

Instantly share code, notes, and snippets.

@xi
Last active September 12, 2018 13:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save xi/63216e505a6d55562eaf695811c6fc9c to your computer and use it in GitHub Desktop.
Run multiple linters
{
"name": "polylint.sh",
"description": "Run multiple linters efficiently.",
"homepage": "https://gist.github.com/xi/63216e505a6d55562eaf695811c6fc9c",
"version": "0.0.4",
"os": ["darwin", "linux"],
"files": ["polylint.sh"],
"bin": {
"polylint": "./polylint.sh"
}
}
#!/bin/sh
help() {
cat << EOF
Run multiple linters efficiently.
Usage:
polylint [-h] [-v] [-F] [-S]
Options:
-h show this help message
-v more verbose output
-F fail on first error
-S only run on files that are staged with git
Linters can be configured in a file called .polylintrc in the following format:
py:flake8 --exclude tests %s
js:jshint
css|scss:stylelint "%s"
EOF
}
verbose=false
staged_only=false
fail_early=false
grcode=0
ls_files() {
if $staged_only; then
git diff --staged --name-only --diff-filter=ACMR
else
git ls-files
fi | grep "\.\($1\)$"
}
count() {
echo $#
}
run_linter() {
files=$(ls_files "$1")
if [ -z "$files" ]; then
return 0
fi
if $verbose; then
echo "> Executing '$2' with $(count $files) files"
fi
cmd=$(printf "$2" "$files")
eval $cmd;
rcode=$?
if [ $rcode -ne 0 ]; then
if $fail_early; then
exit $rcode
else
grcode=$rcode
fi
fi
}
while getopts hvFS flag; do
case "$flag" in
(h) help; exit 0;;
(v) verbose=true;;
(F) fail_early=true;;
(S) staged_only=true;;
(*) exit 1;;
esac
done
while read line; do
exts=$(echo "$line" | sed 's/:.*//')
cmd=$(echo "$line" | sed 's/^[^:]*://')
run_linter "$exts" "$cmd"
done < .polylintrc
exit $grcode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment