diff --git a/README.md b/README.md index 72887579f17..8fd494a14b4 100644 --- a/README.md +++ b/README.md @@ -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* 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 **How do I test a new _______?** diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100644 index 00000000000..d23b8b9faeb --- /dev/null +++ b/hooks/pre-commit @@ -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 $? diff --git a/setup-hooks.sh b/setup-hooks.sh new file mode 100644 index 00000000000..4ba5694a011 --- /dev/null +++ b/setup-hooks.sh @@ -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."