added "how to setup" the hook to eslint --fix each new file before commit (if wanted)

This commit is contained in:
Greenlamp 2024-05-22 02:13:15 +02:00
parent 9a8b6047c8
commit 78f8abcf6c
3 changed files with 59 additions and 0 deletions

View File

@ -16,6 +16,15 @@ If you have the motivation and experience with Typescript/Javascript (or are wil
- *if you run into any errors, reach out in the **#dev-corner** channel in discord* - *if you run into any errors, reach out in the **#dev-corner** channel in discord*
2. Run `npm run start:dev` to locally run the project in `localhost:8000` 2. Run `npm run start:dev` to locally run the project in `localhost:8000`
## Setting Up Git Hooks
After cloning the repository, run the following command to set up the pre-commit hook:
```sh
./setup-hooks.sh
```
This will run npx eslint --fix XXX where XXX is each file in your commit
### ❔ FAQ ### ❔ FAQ
**How do I test a new _______?** **How do I test a new _______?**

36
hooks/pre-commit Normal file
View File

@ -0,0 +1,36 @@
#!/bin/sh
# Adjust the grep pattern to include .js, .jsx, .ts, and .tsx files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E "\.(jsx?|tsx?)$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
echo "Validating Javascript and TypeScript:\n"
for FILE in $STAGED_FILES
do
# Attempt to fix issues automatically
npx eslint --fix "$FILE"
if [[ "$?" == 0 ]]; then
echo "ESLint Passed: $FILE"
else
echo "ESLint Failed: $FILE"
PASS=false
fi
done
echo "Javascript and TypeScript validation completed!\n"
if ! $PASS; then
echo "COMMIT FAILED: Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again."
exit 1
else
echo "COMMIT SUCCEEDED"
fi
exit $?

14
setup-hooks.sh Normal file
View File

@ -0,0 +1,14 @@
#!/bin/sh
# Ensure the script is run from the project root
SCRIPT_DIR=$(dirname "$0")
HOOKS_DIR="$SCRIPT_DIR/hooks"
GIT_HOOKS_DIR="$SCRIPT_DIR/.git/hooks"
# Copy the pre-commit hook to the .git/hooks directory
cp "$HOOKS_DIR/pre-commit" "$GIT_HOOKS_DIR/pre-commit"
# Make the pre-commit hook executable
chmod +x "$GIT_HOOKS_DIR/pre-commit"
echo "Pre-commit hook installed successfully."