|
3 | 3 | # CREDITS: https://gist.github.com/linhmtran168/2286aeafe747e78f53bf |
4 | 4 | # MODIFIED: t.hamoudi |
5 | 5 |
|
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$)") |
7 | 8 |
|
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..." |
9 | 13 | exit 0 |
10 | 14 | fi |
11 | 15 |
|
12 | | -PASS=true |
13 | | - |
14 | | -echo -e "\nValidating Javascript:\n" |
15 | | - |
| 16 | +echo -e "Trying to detect package manager..." |
16 | 17 | # Detect package manager |
17 | 18 | if [ -f "pnpm-lock.yaml" ]; then |
18 | 19 | PACKAGE_MANAGER="pnpm" |
| 20 | + INSTALL_CMD="pnpm add -D eslint" |
19 | 21 | elif [ -f "yarn.lock" ]; then |
20 | 22 | PACKAGE_MANAGER="yarn" |
| 23 | + INSTALL_CMD="yarn add -D eslint" |
21 | 24 | else |
22 | 25 | PACKAGE_MANAGER="npm" |
| 26 | + INSTALL_CMD="npm install --save-dev eslint" |
23 | 27 | fi |
24 | 28 |
|
25 | | -# First check for local eslint installation |
| 29 | +# Check for local eslint installation |
26 | 30 | if [ -f "node_modules/.bin/eslint" ]; then |
27 | 31 | 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" |
29 | 45 | 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 |
50 | 48 | fi |
51 | 49 |
|
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" |
54 | 53 |
|
55 | 54 | if [[ "$?" == 0 ]]; then |
56 | | - echo -e "\t\033[32mESLint Passed: $FILE\033[0m" |
| 55 | + echo -e "\e[1;32m $FILE passed.\e[0m" |
57 | 56 | 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 |
60 | 60 | fi |
61 | 61 | 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