Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks
Tracked by #473
coolaj86 opened this issue Aug 15, 2022 · 0 comments
Open
2 tasks
Tracked by #473

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

coolaj86 opened this issue Aug 15, 2022 · 0 comments

Comments

@coolaj86
Copy link
Member

coolaj86 commented Aug 15, 2022

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

See also:

@coolaj86 coolaj86 transferred this issue from webinstall/webi-installers Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant