We want to update the husky pre-commit file fmt (i.e. prettier, shfmt) and lint (i.e. jshint, shellcheck) so that:
[ ] only files that are staged for commit are checked
[ ] use the "autofix" capabilities of prettier and shellcheck where possible
Here's an example that can be used to "autofix" with prettier (and there's a link to a similar script for shellcheck - both may need tweaking):
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# from https://prettier.io/docs/en/precommit.html#option-6-shell-script
# Should be functionally equivalent to `npm run fmt`
# We're not using basetag
#npx basetag rebase
FILES=$(
git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g'
)
if [ -z "$FILES" ]; then
exit 0
fi
echo "$FILES" | xargs npx prettier@2 --ignore-unknown --write
echo "$FILES" | xargs git add
# Should be functionally equivalent to `npm run lint`
JS_FILES=$(
echo "$FILES" | grep '\.js$' || true
)
if [ -z "$JS_FILES" ]; then
exit 0
fi
echo "$JS_FILES" | xargs npx jshint@2 -c .jshintrc.lax
We want to update the husky pre-commit file
fmt
(i.e.prettier
,shfmt
) andlint
(i.e.jshint
,shellcheck
) so that:prettier
andshellcheck
where possibleHere's an example that can be used to "autofix" with prettier (and there's a link to a similar script for shellcheck - both may need tweaking):
See also: