-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlefthook.yml
More file actions
161 lines (151 loc) · 6.96 KB
/
lefthook.yml
File metadata and controls
161 lines (151 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Lefthook configuration for React on Rails Demos monorepo
# https://github.com/evilmartians/lefthook
pre-commit:
parallel: true
commands:
trailing-newline:
glob: '*.{rb,js,ts,jsx,tsx,yml,yaml,json,md,sh}'
run: |
for file in {staged_files}; do
if [ -f "$file" ]; then
# Check if file ends with newline
if [ -n "$(tail -c 1 "$file")" ]; then
echo "❌ Missing trailing newline: $file"
# Add trailing newline
echo "" >> "$file"
git add "$file"
echo "✅ Added trailing newline to: $file"
fi
fi
done
rubocop:
glob: '*.rb'
run: bundle exec rubocop --force-exclusion {staged_files}
stage_fixed: true
eslint:
glob: '*.{js,jsx,ts,tsx}'
exclude: 'node_modules/**/*'
run: |
# Run ESLint on staged files in demos
demo_files=$(echo "{staged_files}" | tr ' ' '\n' | grep '^demos/' || true)
if [ -n "$demo_files" ]; then
for file in $demo_files; do
# Determine which demo this file belongs to
demo_dir=$(echo "$file" | cut -d'/' -f1-2)
rel_file=$(echo "$file" | sed "s|^$demo_dir/||")
if [ -f "$demo_dir/package.json" ]; then
echo "Running ESLint in $demo_dir for $rel_file"
(cd "$demo_dir" && npx eslint --fix "$rel_file" && git add "$rel_file")
fi
done
fi
prettier:
glob: '*.{js,jsx,ts,tsx,json,css,scss,md}'
exclude: 'node_modules/**/*'
run: |
# Run Prettier on staged files in demos
demo_files=$(echo "{staged_files}" | tr ' ' '\n' | grep '^demos/' || true)
if [ -n "$demo_files" ]; then
for file in $demo_files; do
# Determine which demo this file belongs to
demo_dir=$(echo "$file" | cut -d'/' -f1-2)
rel_file=$(echo "$file" | sed "s|^$demo_dir/||")
if [ -f "$demo_dir/package.json" ]; then
echo "Running Prettier in $demo_dir for $rel_file"
(cd "$demo_dir" && npx prettier --write "$rel_file" && git add "$rel_file")
fi
done
fi
commit-msg:
commands:
check-message:
run: |
# Ensure commit message is not empty
if ! grep -q '[^[:space:]]' {1}; then
echo "❌ Commit message cannot be empty"
exit 1
fi
pre-push:
commands:
check-local-gems:
run: |
echo "🔍 Checking for local gem paths in Gemfiles and package.json files..."
# Simple approach: check uncommitted/staged changes and recent commits on current branch
# Compare current branch with its remote tracking branch, or origin/main if no tracking branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
remote_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null || echo "origin/main")
# Check for local gem paths in Gemfiles (in commits being pushed)
if git diff --name-only "$remote_branch"...HEAD 2>/dev/null | grep -qE 'Gemfile$'; then
# Check for local gem paths, but exclude shakacode_demo_common which is intentionally local
local_gem_lines=$(git diff "$remote_branch"...HEAD -- '*/Gemfile' 'Gemfile' 2>/dev/null | grep -E '^\+.*gem.*path:' | grep -v 'shakacode_demo_common' || true)
if [ -n "$local_gem_lines" ]; then
echo ""
echo "=========================================="
echo "❌ ERROR: Found local gem paths in Gemfile(s)"
echo "=========================================="
echo ""
echo "You're trying to push changes with local gem paths (path: '...')."
echo "This usually means you forgot to restore after using bin/swap-deps."
echo ""
echo "📋 Detected local gem paths:"
echo "$local_gem_lines" | sed 's/^+/ /'
echo ""
echo "🔍 Affected files:"
git diff --name-only "$remote_branch"...HEAD -- '*/Gemfile' 'Gemfile' 2>/dev/null | sed 's/^/ /'
echo ""
echo "💡 To fix:"
echo " 1. Check if you used bin/swap-deps for local development:"
echo " bin/swap-deps --status"
echo ""
echo " 2. If yes, restore the original dependencies:"
echo " bin/swap-deps --restore"
echo ""
echo " 3. If you didn't use swap-deps, manually edit the Gemfile(s)"
echo " to use version constraints instead of local paths"
echo ""
echo " 4. After fixing, stage the changes and try pushing again"
echo ""
echo "ℹ️ Note: shakacode_demo_common is allowed as a local path"
echo "=========================================="
echo ""
exit 1
fi
fi
# Check for file: protocol in package.json files (in commits being pushed)
if git diff --name-only "$remote_branch"...HEAD 2>/dev/null | grep -qE 'package\.json$'; then
# Check for local file: paths, but exclude shakacode_demo_common which is intentionally local
local_npm_lines=$(git diff "$remote_branch"...HEAD -- '*/package.json' 'package.json' 2>/dev/null | grep -E '^\+.*"file:' | grep -v 'shakacode_demo_common' || true)
if [ -n "$local_npm_lines" ]; then
echo ""
echo "=========================================="
echo "❌ ERROR: Found local file: paths in package.json file(s)"
echo "=========================================="
echo ""
echo "You're trying to push changes with local npm package paths (file:...)."
echo "This usually means you forgot to restore after using bin/swap-deps."
echo ""
echo "📋 Detected local file: paths:"
echo "$local_npm_lines" | sed 's/^+/ /'
echo ""
echo "🔍 Affected files:"
git diff --name-only "$remote_branch"...HEAD -- '*/package.json' 'package.json' 2>/dev/null | sed 's/^/ /'
echo ""
echo "💡 To fix:"
echo " 1. Check if you used bin/swap-deps for local development:"
echo " bin/swap-deps --status"
echo ""
echo " 2. If yes, restore the original dependencies:"
echo " bin/swap-deps --restore"
echo ""
echo " 3. If you didn't use swap-deps, manually edit the package.json file(s)"
echo " to use version constraints instead of local file: paths"
echo ""
echo " 4. After fixing, stage the changes and try pushing again"
echo ""
echo "ℹ️ Note: shakacode_demo_common is allowed as a local path"
echo "=========================================="
echo ""
exit 1
fi
fi
echo "✅ No local gem or file paths found (shakacode_demo_common excluded)"