webinstall / webi-roadmap

A space where we can ideate without cluttering the main repo with stale issues
Mozilla Public License 2.0
2 stars 0 forks source link

Chore: Add `pre-commit` hook to handle JavaScript and Posix Shell #21

Open coolaj86 opened 2 years ago

coolaj86 commented 2 years ago

We want to update the husky pre-commit file fmt (i.e. prettier, shfmt) and lint (i.e. jshint, shellcheck) so that:

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

See also: