Last active
April 15, 2021 03:36
-
-
Save kentcdodds/9768d9a8d0bfbf6797cd to your computer and use it in GitHub Desktop.
Shell script to lint only changed files to be used as a githook (specific to my project)
This file contains hidden or 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
#!/usr/bin/env bash | |
set -e | |
# allow being run from somewhere other than the git rootdir | |
gitroot=$(git rev-parse --show-cdup) | |
# default gitroot to . if we're already at the rootdir | |
gitroot=${gitroot:-.}; | |
nm_bin=$gitroot/node_modules/.bin | |
echo "Linting changed files" | |
SRC_FILES=$(git diff --staged --diff-filter=ACMTUXB --name-only -- '*.js' | grep -v '\.test\|mock\|e2e\.js$') && x=1 | |
TEST_FILES=$(git diff --staged --diff-filter=ACMTUXB --name-only -- '*.js' | grep '\.test\|mock\|e2e\.js$') && x=1 | |
function lint() { | |
if [ "$2" ]; then | |
echo "Linting changed $1 files" | |
$nm_bin/eslint $2 -c other/${1}.eslintrc | |
else | |
echo "No $1 files changed" | |
fi | |
} | |
lint "app" $SRC_FILES; | |
lint "test" $TEST_FILES; | |
echo "⚡️ changed files passed linting! ⚡️" |
This is amazing. Here's my simplified version for linting only changed js and jsx files. This lints all changed files, not just staged ones.
eslint $(git diff head --name-only | grep '^.*[.js,.jsx]$')
See also: https://pypi.org/project/lint_diffs: works with many linters (including eslint) and many vcs. It can run on all changed files, and show linter errors for the whole file, or for only changed lines. Good for gradually improving linter errors.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, how can I use this script for my project?