Skip to content

refactor(validate-eslint): make the script POSIX compliant #7

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions scripts/validate-eslint.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/env bash
#!/bin/sh

# CREDITS: https://gist.github.com/linhmtran168/2286aeafe747e78f53bf
# MODIFIED: t.hamoudi

# Get staged JS/TS/Vue files as an array
readarray -t STAGED_FILES < <(git diff --cached --name-only --diff-filter=ACM | grep -E "(.js$|.jsx$|.ts$|.tsx$|.vue$)")
# Get staged JS/TS/Vue files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E "(.js$|.jsx$|.ts$|.tsx$|.vue$)")

echo -e "Found ${#STAGED_FILES[@]} staged files."

if [[ ${#STAGED_FILES[@]} -eq 0 ]]; then
if [ -z "$STAGED_FILES" ]; then
echo "No staged files found, exiting..."
exit 0
fi
Expand All @@ -29,10 +27,10 @@ fi
# Check for local eslint installation
if [ -f "node_modules/.bin/eslint" ]; then
ESLINT="./node_modules/.bin/eslint"
echo -e "Starting ESLint validation..."
echo -e "\e[1;32mA valid ESlint installation found at $ESLINT\e[0m"
echo "Starting ESLint validation..."
echo "A valid ESLint installation found at $ESLINT"
else
echo -e "\e[1;31mNo valid ESlint installation found. Please install ESLint by running: "$INSTALL_CMD"\e[0m"
echo "No valid ESLint installation found. Please install ESLint by running: $INSTALL_CMD"
exit 1
fi

Expand All @@ -41,23 +39,27 @@ if [ -f "eslint.config.js" ] || [ -f "eslint.config.mjs" ] || [ -f "eslint.confi
[ -f "eslint.config.ts" ] || [ -f "eslint.config.mts" ] || [ -f "eslint.config.cts" ] || \
[ -f ".eslintrc.js" ] || [ -f ".eslintrc.cjs" ] || [ -f ".eslintrc.json" ] || \
[ -f ".eslintrc.yml" ] || [ -f ".eslintrc.yaml" ] || [ -f ".eslintrc" ]; then
echo -e "\e[1;32mA valid ESLint configuration file was found.\e[0m"
echo "A valid ESLint configuration file was found."
else
echo -e "\e[1;31mNo valid ESLint configuration file found. Please initialize your configuration by running: "$ESLINT" --init\e[0m"
echo "No valid ESLint configuration file found. Please initialize your configuration by running: $ESLINT --init"
exit 1
fi

for FILE in "${STAGED_FILES[@]}"; do
echo "Checking file: $FILE"
"$ESLINT" "$FILE"
# Process each file
echo "$STAGED_FILES" | while IFS= read -r FILE; do
if [ -n "$FILE" ]; then
echo "Checking file: $FILE"
"$ESLINT" "$FILE"

if [[ "$?" == 0 ]]; then
echo -e "\e[1;32m $FILE passed.\e[0m"
else
echo -e "\e[1;31m$FILE failed.\e[0m"
echo -e "\e[1;31mCOMMIT FAILED: Your commit contains files that did not pass linting. Please fix the issues and try again.\e[0m"
exit 1
if [ "$?" -eq 0 ]; then
echo "$FILE passed."
else
echo "$FILE failed."
echo "COMMIT FAILED: Your commit contains files that did not pass linting. Please fix the issues and try again."
exit 1
fi
fi
done
echo -e "\e[1;32mCOMMIT SUCCEEDED\e[0m"

echo "COMMIT SUCCEEDED"
exit 0