Skip to content

Commit 81c91ab

Browse files
thread-koderGeorge Khoury
andauthored
fix(validate-eslint): fix validaion logic (#6)
* fix(validate-eslint): fix validaion logic * refactor(eslint): clean up and refactor the script * feat(bla): remove bla * refactor(validate-eslint): check for eslint config file before running * refactor: clean up logging * refactor: remove bla Signed-off-by: George Khoury <[email protected]> * fix(validate-eslint): check for ESLint config files (both new flat config and legacy formats) * refactor: clean up logging --------- Signed-off-by: George Khoury <[email protected]> Co-authored-by: George Khoury <[email protected]>
1 parent d39d8a2 commit 81c91ab

File tree

1 file changed

+35
-44
lines changed

1 file changed

+35
-44
lines changed

scripts/validate-eslint.sh

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,61 @@
33
# CREDITS: https://gist.github.com/linhmtran168/2286aeafe747e78f53bf
44
# MODIFIED: t.hamoudi
55

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

8-
if [[ "$STAGED_FILES" = "" ]]; then
9+
echo -e "Found ${#STAGED_FILES[@]} staged files."
10+
11+
if [[ ${#STAGED_FILES[@]} -eq 0 ]]; then
12+
echo "No staged files found, exiting..."
913
exit 0
1014
fi
1115

12-
PASS=true
13-
14-
echo -e "\nValidating Javascript:\n"
15-
16+
echo -e "Trying to detect package manager..."
1617
# Detect package manager
1718
if [ -f "pnpm-lock.yaml" ]; then
1819
PACKAGE_MANAGER="pnpm"
20+
INSTALL_CMD="pnpm add -D eslint"
1921
elif [ -f "yarn.lock" ]; then
2022
PACKAGE_MANAGER="yarn"
23+
INSTALL_CMD="yarn add -D eslint"
2124
else
2225
PACKAGE_MANAGER="npm"
26+
INSTALL_CMD="npm install --save-dev eslint"
2327
fi
2428

25-
# First check for local eslint installation
29+
# Check for local eslint installation
2630
if [ -f "node_modules/.bin/eslint" ]; then
2731
ESLINT="./node_modules/.bin/eslint"
28-
echo -e "\t\033[32mUsing project's local ESLint installation\033[0m"
32+
echo -e "Starting ESLint validation..."
33+
echo -e "\e[1;32mA valid ESlint installation found at $ESLINT\e[0m"
34+
else
35+
echo -e "\e[1;31mNo valid ESlint installation found. Please install ESLint by running: "$INSTALL_CMD"\e[0m"
36+
exit 1
37+
fi
38+
39+
# Check for ESLint config files (both new flat config and legacy formats)
40+
if [ -f "eslint.config.js" ] || [ -f "eslint.config.mjs" ] || [ -f "eslint.config.cjs" ] || \
41+
[ -f "eslint.config.ts" ] || [ -f "eslint.config.mts" ] || [ -f "eslint.config.cts" ] || \
42+
[ -f ".eslintrc.js" ] || [ -f ".eslintrc.cjs" ] || [ -f ".eslintrc.json" ] || \
43+
[ -f ".eslintrc.yml" ] || [ -f ".eslintrc.yaml" ] || [ -f ".eslintrc" ]; then
44+
echo -e "\e[1;32mA valid ESLint configuration file was found.\e[0m"
2945
else
30-
echo -e "\t\033[33mNo local ESLint installation found. Installing...\033[0m"
31-
case $PACKAGE_MANAGER in
32-
"pnpm")
33-
pnpm add -D eslint
34-
;;
35-
"yarn")
36-
yarn add -D eslint
37-
;;
38-
*)
39-
npm install --save-dev eslint
40-
;;
41-
esac
42-
43-
if [ -f "node_modules/.bin/eslint" ]; then
44-
ESLINT="./node_modules/.bin/eslint"
45-
echo -e "\t\033[32mSuccessfully installed ESLint locally\033[0m"
46-
else
47-
echo -e "\t\033[41mFailed to install ESLint locally. Please install it manually using your package manager.\033[0m"
48-
exit 1
49-
fi
46+
echo -e "\e[1;31mNo valid ESLint configuration file found. Please initialize your configuration by running: "$ESLINT" --init\e[0m"
47+
exit 1
5048
fi
5149

52-
for FILE in $STAGED_FILES; do
53-
$ESLINT "$FILE"
50+
for FILE in "${STAGED_FILES[@]}"; do
51+
echo "Checking file: $FILE"
52+
"$ESLINT" "$FILE"
5453

5554
if [[ "$?" == 0 ]]; then
56-
echo -e "\t\033[32mESLint Passed: $FILE\033[0m"
55+
echo -e "\e[1;32m $FILE passed.\e[0m"
5756
else
58-
echo -e "\t\033[41mESLint Failed: $FILE\033[0m"
59-
PASS=false
57+
echo -e "\e[1;31m$FILE failed.\e[0m"
58+
echo -e "\e[1;31mCOMMIT FAILED: Your commit contains files that did not pass linting. Please fix the issues and try again.\e[0m"
59+
exit 1
6060
fi
6161
done
62-
63-
echo -e "\nJavascript validation completed!\n"
64-
65-
if ! $PASS; then
66-
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again."
67-
exit 1
68-
else
69-
echo -e "\033[42mCOMMIT SUCCEEDED\033[0m"
70-
fi
71-
72-
exit $?
62+
echo -e "\e[1;32mCOMMIT SUCCEEDED\e[0m"
63+
exit 0

0 commit comments

Comments
 (0)