Skip to content

Commit d39d8a2

Browse files
committed
fix(validate-eslint.sh): improve and fix eslint validation logic
1 parent dd78ea8 commit d39d8a2

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

scripts/validate-eslint.sh

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,44 @@ PASS=true
1313

1414
echo -e "\nValidating Javascript:\n"
1515

16-
# Check for eslint
17-
which eslint &>/dev/null
18-
if [[ "$?" == 1 ]]; then
19-
echo -e "\t\033[41mPlease install ESlint\033[0m"
20-
exit 1
16+
# Detect package manager
17+
if [ -f "pnpm-lock.yaml" ]; then
18+
PACKAGE_MANAGER="pnpm"
19+
elif [ -f "yarn.lock" ]; then
20+
PACKAGE_MANAGER="yarn"
21+
else
22+
PACKAGE_MANAGER="npm"
23+
fi
24+
25+
# First check for local eslint installation
26+
if [ -f "node_modules/.bin/eslint" ]; then
27+
ESLINT="./node_modules/.bin/eslint"
28+
echo -e "\t\033[32mUsing project's local ESLint installation\033[0m"
29+
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
2150
fi
2251

2352
for FILE in $STAGED_FILES; do
24-
eslint "$FILE"
53+
$ESLINT "$FILE"
2554

2655
if [[ "$?" == 0 ]]; then
2756
echo -e "\t\033[32mESLint Passed: $FILE\033[0m"

0 commit comments

Comments
 (0)