1
- #! /bin/sh
1
+ #! /usr/bin/env bash
2
+ set -euo pipefail
2
3
3
- # D.A.T.A. Pre-commit Hook with JSDoc Generation
4
- # 1. Generates JSDoc for staged JavaScript files
5
- # 2. Runs ESLint checks for async/await issues
4
+ echo " 🖖 D.A.T.A. pre-commit"
6
5
7
- echo " 🖖 D.A.T.A. Pre-commit Hook - Ensuring code quality and documentation..."
6
+ GIT_ROOT=" $( git rev-parse --show-toplevel) "
7
+ cd " $GIT_ROOT "
8
8
9
- # Get the root directory of the git repository
10
- GIT_ROOT=$( git rev-parse --show-toplevel)
9
+ # Only staged JS/TS files in repo (not node_modules / dist)
10
+ STAGED=" $( git diff --cached --name-only --diff-filter=ACM \
11
+ | grep -E ' \.(mjs|cjs|js|ts|tsx)$' \
12
+ | grep -Ev ' (^|/)(node_modules|dist|build)/' || true) "
11
13
12
- # Change to the git root directory
13
- cd " $GIT_ROOT " || exit 1
14
-
15
- # Get list of staged JavaScript files (exclude node_modules and only include src/, bin/, scripts/)
16
- STAGED_FILES=$( git diff --cached --name-only --diff-filter=ACM | grep ' \.js$' | grep -E ' ^(src/|bin/|scripts/)' | grep -v node_modules)
17
-
18
- if [ -z " $STAGED_FILES " ]; then
19
- echo " ℹ️ No JavaScript files to process"
14
+ if [ -z " $STAGED " ]; then
15
+ echo " ✅ No JS/TS staged — skipping lint"
20
16
exit 0
21
17
fi
22
18
23
- echo " 📁 Processing files:"
24
- echo " $STAGED_FILES " | sed ' s/^/ - /'
25
- echo " "
26
-
27
- # Step 1: Generate JSDoc for staged files
28
- echo " 🤖 Generating JSDoc documentation..."
29
-
30
- # Check if JSDoc generation should be skipped
31
- if [ " $SKIP_JSDOC " = " true" ]; then
32
- echo " ⏭️ Skipping JSDoc generation (SKIP_JSDOC=true)"
33
- else
34
- # Convert file list to space-separated arguments for the JSDoc generator
35
- JSDOC_FILES=" "
36
- for file in $STAGED_FILES ; do
37
- JSDOC_FILES=" $JSDOC_FILES $file "
38
- done
39
-
40
- # Run JSDoc generation
41
- node " $GIT_ROOT /scripts/jsdoc/generate-jsdoc.js" $JSDOC_FILES
42
-
43
- JSDOC_EXIT=$?
44
-
45
- if [ $JSDOC_EXIT -eq 0 ]; then
46
- echo " ✅ JSDoc generation completed"
47
-
48
- # Re-stage files that may have been updated with JSDoc
49
- for file in $STAGED_FILES ; do
50
- if [ -f " $file " ]; then
51
- git add " $file "
52
- fi
53
- done
54
- else
55
- echo " ⚠️ JSDoc generation had issues, but continuing with commit"
56
- echo " 💡 Tip: Set SKIP_JSDOC=true to skip JSDoc generation"
57
- fi
58
- fi
59
-
60
- echo " "
61
-
62
- # Step 2: Run ESLint checks
63
- echo " 🔍 Running ESLint checks..."
64
-
65
- # Run ESLint on staged files
66
- npx eslint $STAGED_FILES
67
-
68
- ESLINT_EXIT=$?
69
-
70
- if [ $ESLINT_EXIT -eq 0 ]; then
71
- echo " ✅ ESLint checks passed!"
19
+ # Prefer pnpm if available, otherwise fallback
20
+ if command -v pnpm > /dev/null 2>&1 ; then
21
+ echo " 🔧 Linting with pnpm exec eslint"
22
+ pnpm exec eslint --max-warnings=0 $STAGED
72
23
else
73
- echo " ❌ ESLint found issues. Please fix them before committing."
74
- echo " "
75
- echo " 💡 Tip: You can run 'npm run lint:fix' to auto-fix some issues"
76
- echo " 💡 Tip: Set SKIP_JSDOC=true if JSDoc generation is causing issues"
77
- exit 1
24
+ echo " 🔧 Linting with npx eslint"
25
+ npx eslint --max-warnings=0 $STAGED
78
26
fi
79
27
80
- # Step 3: Check specifically for async/await issues
81
- echo " "
82
- echo " 🔍 Checking for floating promises and async issues..."
83
-
84
- # Look for common async/await problems in staged files
85
- for file in $STAGED_FILES ; do
86
- # Check for .then() without catch
87
- if grep -E ' \.then\([^)]*\)[^.]*(;|$)' " $file " > /dev/null 2>&1 ; then
88
- echo " ⚠️ Warning: $file may have unhandled promises (.then without .catch)"
89
- fi
90
-
91
- # Check for async functions without await
92
- if grep -E ' async\s+[^{]*\{[^}]*\}' " $file " | grep -v await > /dev/null 2>&1 ; then
93
- echo " ⚠️ Warning: $file may have async functions without await"
94
- fi
95
- done
96
-
97
- echo " "
98
- echo " 🎯 Pre-commit checks complete! Code quality and documentation ensured."
99
- echo " "
100
- echo " 💡 To skip JSDoc generation: SKIP_JSDOC=true git commit"
101
- echo " 💡 To manually generate JSDoc: npm run jsdoc:generate"
102
- echo " 💡 To generate JSDoc for specific files: npm run jsdoc:files -- file1.js file2.js"
28
+ # Optional: run related tests (uncomment once tests exist)
29
+ # if command -v pnpm >/dev/null 2>&1; then
30
+ # pnpm exec vitest --run --passWithNoTests --findRelatedTests $STAGED
31
+ # fi
103
32
104
- exit 0
33
+ echo " ✅ Hook OK "
0 commit comments