@localtech/claude-code-toolkit 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,257 +1,257 @@
1
- #!/bin/bash
2
- # HOOK: pre-commit-code-quality-guardian
3
- # DESCRIPTION: Comprehensive code quality checks before commits
4
- # AUTHOR: Claude Code Hooks Master
5
- # VERSION: 1.0.0
6
- # DEPENDS: node, npm, eslint, prettier, typescript
7
-
8
- set -e
9
-
10
- # Colors for output
11
- RED='\033[0;31m'
12
- GREEN='\033[0;32m'
13
- YELLOW='\033[1;33m'
14
- BLUE='\033[0;34m'
15
- NC='\033[0m'
16
-
17
- # Logging functions
18
- log_info() {
19
- echo -e "${BLUE}[QUALITY]${NC} $1"
20
- }
21
-
22
- log_success() {
23
- echo -e "${GREEN}[PASS]${NC} $1"
24
- }
25
-
26
- log_warn() {
27
- echo -e "${YELLOW}[WARN]${NC} $1"
28
- }
29
-
30
- log_error() {
31
- echo -e "${RED}[FAIL]${NC} $1"
32
- }
33
-
34
- # Timer function
35
- start_timer() {
36
- start_time=$(date +%s)
37
- }
38
-
39
- end_timer() {
40
- end_time=$(date +%s)
41
- duration=$((end_time - start_time))
42
- echo -e "${BLUE}[TIME]${NC} Completed in ${duration}s"
43
- }
44
-
45
- # Check if file exists and is executable
46
- check_dependency() {
47
- if ! command -v "$1" &> /dev/null; then
48
- log_error "Required dependency '$1' not found"
49
- return 1
50
- fi
51
- return 0
52
- }
53
-
54
- # Get staged files for analysis
55
- get_staged_files() {
56
- git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|ts|jsx|tsx|vue|svelte)$' || true
57
- }
58
-
59
- # Lint staged files
60
- run_linter() {
61
- local files=("$@")
62
- if [ ${#files[@]} -eq 0 ]; then
63
- log_info "No JavaScript/TypeScript files to lint"
64
- return 0
65
- fi
66
-
67
- log_info "Running ESLint on ${#files[@]} files..."
68
- start_timer
69
-
70
- if npx eslint "${files[@]}" --quiet; then
71
- log_success "ESLint checks passed"
72
- end_timer
73
- return 0
74
- else
75
- log_error "ESLint found issues. Please fix them before committing."
76
- log_info "Tip: Run 'npx eslint --fix' to auto-fix some issues"
77
- return 1
78
- fi
79
- }
80
-
81
- # Format staged files
82
- run_formatter() {
83
- local files=("$@")
84
- if [ ${#files[@]} -eq 0 ]; then
85
- log_info "No files to format"
86
- return 0
87
- fi
88
-
89
- log_info "Running Prettier on ${#files[@]} files..."
90
- start_timer
91
-
92
- if npx prettier --check "${files[@]}"; then
93
- log_success "Code formatting is correct"
94
- end_timer
95
- return 0
96
- else
97
- log_error "Code formatting issues found."
98
- log_info "Tip: Run 'npx prettier --write' to auto-format"
99
- return 1
100
- fi
101
- }
102
-
103
- # Type checking for TypeScript
104
- run_type_check() {
105
- if [ -f "tsconfig.json" ]; then
106
- log_info "Running TypeScript type checking..."
107
- start_timer
108
-
109
- if npx tsc --noEmit; then
110
- log_success "TypeScript compilation successful"
111
- end_timer
112
- return 0
113
- else
114
- log_error "TypeScript errors found. Please fix type issues."
115
- return 1
116
- fi
117
- else
118
- log_info "No tsconfig.json found, skipping TypeScript checks"
119
- return 0
120
- fi
121
- }
122
-
123
- # Security vulnerability check
124
- run_security_check() {
125
- log_info "Running security vulnerability check..."
126
- start_timer
127
-
128
- # Check for common security issues
129
- local staged_files
130
- mapfile -t staged_files < <(get_staged_files)
131
-
132
- local has_security_issues=false
133
-
134
- for file in "${staged_files[@]}"; do
135
- # Check for console.log in production code
136
- if grep -q "console\." "$file" && [[ "$file" != *".test."* ]] && [[ "$file" != *".spec."* ]]; then
137
- log_warn "console statements found in $file (remove for production)"
138
- has_security_issues=true
139
- fi
140
-
141
- # Check for debugger statements
142
- if grep -q "debugger" "$file"; then
143
- log_warn "debugger statement found in $file"
144
- has_security_issues=true
145
- fi
146
-
147
- # Check for potential secrets
148
- if grep -i -q "password\|secret\|token\|key" "$file" && grep -q "=" "$file"; then
149
- log_warn "Potential secret exposure in $file - review carefully"
150
- has_security_issues=true
151
- fi
152
- done
153
-
154
- if [ "$has_security_issues" = false ]; then
155
- log_success "Security check passed"
156
- fi
157
-
158
- end_timer
159
- return 0 # Don't fail commit for warnings
160
- }
161
-
162
- # Performance check
163
- run_performance_check() {
164
- log_info "Running basic performance checks..."
165
- start_timer
166
-
167
- local staged_files
168
- mapfile -t staged_files < <(get_staged_files)
169
-
170
- local has_performance_issues=false
171
-
172
- for file in "${staged_files[@]}"; do
173
- # Check bundle size for large files
174
- local file_size
175
- file_size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || echo "0")
176
-
177
- if [ "$file_size" -gt 1000000 ]; then # 1MB
178
- log_warn "Large file detected: $file (${file_size} bytes)"
179
- has_performance_issues=true
180
- fi
181
-
182
- # Check for inefficient patterns
183
- if grep -q "for.*in.*array\|for.*in.*object" "$file"; then
184
- log_warn "Potential inefficient loop in $file - consider alternatives"
185
- has_performance_issues=true
186
- fi
187
- done
188
-
189
- if [ "$has_performance_issues" = false ]; then
190
- log_success "Performance check passed"
191
- fi
192
-
193
- end_timer
194
- return 0
195
- }
196
-
197
- # Main execution
198
- main() {
199
- log_info "🛡️ Claude Code Quality Guardian - Pre-commit Hook"
200
- echo "=================================================="
201
-
202
- # Check dependencies
203
- local deps=("node" "npm" "npx")
204
- for dep in "${deps[@]}"; do
205
- if ! check_dependency "$dep"; then
206
- log_error "Missing required dependency: $dep"
207
- exit 1
208
- fi
209
- done
210
-
211
- # Get staged files
212
- local staged_files
213
- mapfile -t staged_files < <(get_staged_files)
214
-
215
- if [ ${#staged_files[@]} -eq 0 ]; then
216
- log_info "No JavaScript/TypeScript files staged for commit"
217
- exit 0
218
- fi
219
-
220
- log_info "Checking ${#staged_files[@]} files: ${staged_files[*]}"
221
- echo
222
-
223
- # Run all quality checks
224
- local checks_passed=true
225
-
226
- if ! run_linter "${staged_files[@]}"; then
227
- checks_passed=false
228
- fi
229
-
230
- if ! run_formatter "${staged_files[@]}"; then
231
- checks_passed=false
232
- fi
233
-
234
- if ! run_type_check; then
235
- checks_passed=false
236
- fi
237
-
238
- run_security_check
239
- run_performance_check
240
-
241
- echo
242
- if [ "$checks_passed" = true ]; then
243
- log_success "🎉 All quality checks passed! Your code is ready for commit."
244
- exit 0
245
- else
246
- log_error "❌ Quality checks failed. Please fix the issues and try again."
247
- echo
248
- log_info "Quick fixes:"
249
- echo " • Run 'npx eslint --fix' to auto-fix linting issues"
250
- echo " • Run 'npx prettier --write' to auto-format code"
251
- echo " • Check TypeScript errors with 'npx tsc --noEmit'"
252
- exit 1
253
- fi
254
- }
255
-
256
- # Run main function
257
- main "$@"
1
+ #!/bin/bash
2
+ # HOOK: pre-commit-code-quality-guardian
3
+ # DESCRIPTION: Comprehensive code quality checks before commits
4
+ # AUTHOR: Claude Code Hooks Master
5
+ # VERSION: 1.0.0
6
+ # DEPENDS: node, npm, eslint, prettier, typescript
7
+
8
+ set -e
9
+
10
+ # Colors for output
11
+ RED='\033[0;31m'
12
+ GREEN='\033[0;32m'
13
+ YELLOW='\033[1;33m'
14
+ BLUE='\033[0;34m'
15
+ NC='\033[0m'
16
+
17
+ # Logging functions
18
+ log_info() {
19
+ echo -e "${BLUE}[QUALITY]${NC} $1"
20
+ }
21
+
22
+ log_success() {
23
+ echo -e "${GREEN}[PASS]${NC} $1"
24
+ }
25
+
26
+ log_warn() {
27
+ echo -e "${YELLOW}[WARN]${NC} $1"
28
+ }
29
+
30
+ log_error() {
31
+ echo -e "${RED}[FAIL]${NC} $1"
32
+ }
33
+
34
+ # Timer function
35
+ start_timer() {
36
+ start_time=$(date +%s)
37
+ }
38
+
39
+ end_timer() {
40
+ end_time=$(date +%s)
41
+ duration=$((end_time - start_time))
42
+ echo -e "${BLUE}[TIME]${NC} Completed in ${duration}s"
43
+ }
44
+
45
+ # Check if file exists and is executable
46
+ check_dependency() {
47
+ if ! command -v "$1" &> /dev/null; then
48
+ log_error "Required dependency '$1' not found"
49
+ return 1
50
+ fi
51
+ return 0
52
+ }
53
+
54
+ # Get staged files for analysis
55
+ get_staged_files() {
56
+ git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|ts|jsx|tsx|vue|svelte)$' || true
57
+ }
58
+
59
+ # Lint staged files
60
+ run_linter() {
61
+ local files=("$@")
62
+ if [ ${#files[@]} -eq 0 ]; then
63
+ log_info "No JavaScript/TypeScript files to lint"
64
+ return 0
65
+ fi
66
+
67
+ log_info "Running ESLint on ${#files[@]} files..."
68
+ start_timer
69
+
70
+ if npx eslint "${files[@]}" --quiet; then
71
+ log_success "ESLint checks passed"
72
+ end_timer
73
+ return 0
74
+ else
75
+ log_error "ESLint found issues. Please fix them before committing."
76
+ log_info "Tip: Run 'npx eslint --fix' to auto-fix some issues"
77
+ return 1
78
+ fi
79
+ }
80
+
81
+ # Format staged files
82
+ run_formatter() {
83
+ local files=("$@")
84
+ if [ ${#files[@]} -eq 0 ]; then
85
+ log_info "No files to format"
86
+ return 0
87
+ fi
88
+
89
+ log_info "Running Prettier on ${#files[@]} files..."
90
+ start_timer
91
+
92
+ if npx prettier --check "${files[@]}"; then
93
+ log_success "Code formatting is correct"
94
+ end_timer
95
+ return 0
96
+ else
97
+ log_error "Code formatting issues found."
98
+ log_info "Tip: Run 'npx prettier --write' to auto-format"
99
+ return 1
100
+ fi
101
+ }
102
+
103
+ # Type checking for TypeScript
104
+ run_type_check() {
105
+ if [ -f "tsconfig.json" ]; then
106
+ log_info "Running TypeScript type checking..."
107
+ start_timer
108
+
109
+ if npx tsc --noEmit; then
110
+ log_success "TypeScript compilation successful"
111
+ end_timer
112
+ return 0
113
+ else
114
+ log_error "TypeScript errors found. Please fix type issues."
115
+ return 1
116
+ fi
117
+ else
118
+ log_info "No tsconfig.json found, skipping TypeScript checks"
119
+ return 0
120
+ fi
121
+ }
122
+
123
+ # Security vulnerability check
124
+ run_security_check() {
125
+ log_info "Running security vulnerability check..."
126
+ start_timer
127
+
128
+ # Check for common security issues
129
+ local staged_files
130
+ mapfile -t staged_files < <(get_staged_files)
131
+
132
+ local has_security_issues=false
133
+
134
+ for file in "${staged_files[@]}"; do
135
+ # Check for console.log in production code
136
+ if grep -q "console\." "$file" && [[ "$file" != *".test."* ]] && [[ "$file" != *".spec."* ]]; then
137
+ log_warn "console statements found in $file (remove for production)"
138
+ has_security_issues=true
139
+ fi
140
+
141
+ # Check for debugger statements
142
+ if grep -q "debugger" "$file"; then
143
+ log_warn "debugger statement found in $file"
144
+ has_security_issues=true
145
+ fi
146
+
147
+ # Check for potential secrets
148
+ if grep -i -q "password\|secret\|token\|key" "$file" && grep -q "=" "$file"; then
149
+ log_warn "Potential secret exposure in $file - review carefully"
150
+ has_security_issues=true
151
+ fi
152
+ done
153
+
154
+ if [ "$has_security_issues" = false ]; then
155
+ log_success "Security check passed"
156
+ fi
157
+
158
+ end_timer
159
+ return 0 # Don't fail commit for warnings
160
+ }
161
+
162
+ # Performance check
163
+ run_performance_check() {
164
+ log_info "Running basic performance checks..."
165
+ start_timer
166
+
167
+ local staged_files
168
+ mapfile -t staged_files < <(get_staged_files)
169
+
170
+ local has_performance_issues=false
171
+
172
+ for file in "${staged_files[@]}"; do
173
+ # Check bundle size for large files
174
+ local file_size
175
+ file_size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || echo "0")
176
+
177
+ if [ "$file_size" -gt 1000000 ]; then # 1MB
178
+ log_warn "Large file detected: $file (${file_size} bytes)"
179
+ has_performance_issues=true
180
+ fi
181
+
182
+ # Check for inefficient patterns
183
+ if grep -q "for.*in.*array\|for.*in.*object" "$file"; then
184
+ log_warn "Potential inefficient loop in $file - consider alternatives"
185
+ has_performance_issues=true
186
+ fi
187
+ done
188
+
189
+ if [ "$has_performance_issues" = false ]; then
190
+ log_success "Performance check passed"
191
+ fi
192
+
193
+ end_timer
194
+ return 0
195
+ }
196
+
197
+ # Main execution
198
+ main() {
199
+ log_info "🛡️ Claude Code Quality Guardian - Pre-commit Hook"
200
+ echo "=================================================="
201
+
202
+ # Check dependencies
203
+ local deps=("node" "npm" "npx")
204
+ for dep in "${deps[@]}"; do
205
+ if ! check_dependency "$dep"; then
206
+ log_error "Missing required dependency: $dep"
207
+ exit 1
208
+ fi
209
+ done
210
+
211
+ # Get staged files
212
+ local staged_files
213
+ mapfile -t staged_files < <(get_staged_files)
214
+
215
+ if [ ${#staged_files[@]} -eq 0 ]; then
216
+ log_info "No JavaScript/TypeScript files staged for commit"
217
+ exit 0
218
+ fi
219
+
220
+ log_info "Checking ${#staged_files[@]} files: ${staged_files[*]}"
221
+ echo
222
+
223
+ # Run all quality checks
224
+ local checks_passed=true
225
+
226
+ if ! run_linter "${staged_files[@]}"; then
227
+ checks_passed=false
228
+ fi
229
+
230
+ if ! run_formatter "${staged_files[@]}"; then
231
+ checks_passed=false
232
+ fi
233
+
234
+ if ! run_type_check; then
235
+ checks_passed=false
236
+ fi
237
+
238
+ run_security_check
239
+ run_performance_check
240
+
241
+ echo
242
+ if [ "$checks_passed" = true ]; then
243
+ log_success "🎉 All quality checks passed! Your code is ready for commit."
244
+ exit 0
245
+ else
246
+ log_error "❌ Quality checks failed. Please fix the issues and try again."
247
+ echo
248
+ log_info "Quick fixes:"
249
+ echo " • Run 'npx eslint --fix' to auto-fix linting issues"
250
+ echo " • Run 'npx prettier --write' to auto-format code"
251
+ echo " • Check TypeScript errors with 'npx tsc --noEmit'"
252
+ exit 1
253
+ fi
254
+ }
255
+
256
+ # Run main function
257
+ main "$@"