|
1 | 1 | #!/bin/sh |
2 | 2 | . "$(dirname "$0")/_/husky.sh" |
3 | 3 |
|
4 | | -echo "Running Prettier on .snippets/code/**/*.{js,json,html}" |
5 | | -npx prettier --write .snippets/code/**/*.{js,json,html} |
6 | | - |
7 | | -# Find all TOML files and normalize paths |
8 | | -ALL_FILES=$(find .snippets/code/ -type f -name "*.toml" | sed 's|//|/|g') |
9 | | - |
10 | | -# Run Taplo if there are files to format |
11 | | -if [ -n "$ALL_FILES" ]; then |
12 | | - echo "Formatting files with Taplo..." |
13 | | - echo "$ALL_FILES" | while read -r file; do |
14 | | - echo "Formatting $file" |
15 | | - npx taplo fmt "$file" |
16 | | - if [ $? -ne 0 ]; then |
17 | | - echo "Error: Taplo formatting failed." |
18 | | - exit 1 |
19 | | - fi |
20 | | - done |
| 4 | +# Get list of staged files that match our patterns |
| 5 | +STAGED_JS_JSON_HTML=$(git diff --cached --name-only --diff-filter=AM | grep -E '^.snippets/code/.*\.(js|json|html)$' || true) |
| 6 | +STAGED_TOML=$(git diff --cached --name-only --diff-filter=AM | grep -E '^.snippets/code/.*\.toml$' || true) |
| 7 | + |
| 8 | +# Format JavaScript, JSON, and HTML files if any exist |
| 9 | +if [ -n "$STAGED_JS_JSON_HTML" ]; then |
| 10 | + echo "Running Prettier on staged files..." |
| 11 | + echo "$STAGED_JS_JSON_HTML" | while read -r file; do |
| 12 | + if [ -f "$file" ]; then |
| 13 | + echo "Formatting $file" |
| 14 | + npx prettier --write "$file" || exit 1 |
| 15 | + git add "$file" |
| 16 | + fi |
| 17 | + done |
21 | 18 | else |
22 | | - echo "No files to format." |
| 19 | + echo "No JavaScript, JSON, or HTML files to format." |
| 20 | +fi |
| 21 | + |
| 22 | +# Format TOML files if any exist |
| 23 | +if [ -n "$STAGED_TOML" ]; then |
| 24 | + echo "Formatting TOML files with Taplo..." |
| 25 | + echo "$STAGED_TOML" | while read -r file; do |
| 26 | + if [ -f "$file" ]; then |
| 27 | + echo "Formatting $file" |
| 28 | + npx taplo fmt "$file" || exit 1 |
| 29 | + git add "$file" |
| 30 | + fi |
| 31 | + done |
| 32 | +else |
| 33 | + echo "No TOML files to format." |
23 | 34 | fi |
24 | 35 |
|
25 | 36 | echo "Adding formatted files back to the commit..." |
|
0 commit comments