Skip to content

Commit e16e2da

Browse files
committed
fix: pre-commit hook updated to only apply prettier and taplo over staged files
1 parent 5cc0fd6 commit e16e2da

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

.husky/pre-commit

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
echo "Running Prettier on .snippets/code/**/*.{js,json,html}"
5-
npx prettier --write .snippets/code/**/*.{js,json,html}
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)
67

7-
IGNORED_FILES=(
8-
".snippets/code/develop/toolkit/parachains/spawn-chains/zombienet/write-tests/big-network-test.toml"
9-
".snippets/code/develop/toolkit/parachains/spawn-chains/zombienet/write-tests/small-network-test.toml"
10-
".snippets/code/develop/toolkit/parachains/spawn-chains/zombienet/write-tests/spawn-a-basic-chain-test.toml"
11-
".snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-unit-testing/cargo-dev-dependencies.toml"
12-
)
13-
14-
# Normalize ignored files pattern
15-
EXCLUDE_PATTERN=$(printf "%s\n" "${IGNORED_FILES[@]}" | sed 's/\//\\\//g' | paste -sd "|" -)
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
18+
else
19+
echo "No JavaScript, JSON, or HTML files to format."
20+
fi
1621

17-
# Find all TOML files and normalize paths
18-
ALL_FILES=$(find .snippets/code/ -type f -name "*.toml" | sed 's|//|/|g')
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."
34+
fi
1935

20-
# Exclude ignored files
21-
FILES_TO_FORMAT=$(echo "$ALL_FILES" | grep -Ev "$EXCLUDE_PATTERN")
36+
# Run the Python script to generate llms.txt if any relevant files changed
37+
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=AM | grep -E '^.snippets/code/' || true)
38+
echo "Changed files in snippets/code: $CHANGED_FILES"
2239

23-
# Run Taplo if there are files to format
24-
if [ -n "$FILES_TO_FORMAT" ]; then
25-
echo "Formatting files with Taplo..."
26-
echo "$FILES_TO_FORMAT" | while read -r file; do
27-
npx taplo fmt "$file"
28-
if [ $? -ne 0 ]; then
29-
echo "Error: Taplo formatting failed."
30-
exit 1
40+
if [ -n "$CHANGED_FILES" ]; then
41+
echo "Running Python script to generate llms.txt..."
42+
python3 scripts/generate_llms.py || exit 1
43+
44+
# Check if llms.txt was generated and add it to the commit
45+
if [ -f "llms.txt" ]; then
46+
echo "Staging llms.txt file..."
47+
git add llms.txt
48+
else
49+
echo "No llms.txt file generated."
3150
fi
32-
done
3351
else
34-
echo "No files to format."
35-
fi
36-
37-
echo "Adding formatted files back to the commit..."
38-
git add .snippets/code/**/*.{js,json,html,toml}
52+
echo "No relevant files changed, skipping llms.txt generation."
53+
fi

0 commit comments

Comments
 (0)