#!/bin/bash
# pre-commit: Run linting and formatting checks before commit
set -euo pipefail
echo "[pre-commit] Running pre-commit checks..."
# Check if we're in a Node.js project
if [ ! -f "package.json" ]; then
echo "[pre-commit] No package.json found, skipping Node.js checks"
exit 0
fi
# Run ESLint with --fix on staged JavaScript files
js_files=""
while IFS= read -r file; do
[ -z "$file" ] && continue
js_files="$js_files '$file'"
done < <(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|mjs|cjs)$' || true)
if [ -n "$js_files" ]; then
if command -v npx >/dev/null 2>&1; then
echo "[pre-commit] Running ESLint --fix on staged JavaScript files..."
# shellcheck disable=SC2086
if ! eval npx eslint --fix $js_files; then
echo "[pre-commit] ESLint --fix failed. Please review and fix the remaining issues above."
exit 1
fi
echo "[pre-commit] ESLint --fix completed successfully."
# Re-add files that may have been fixed by ESLint
# shellcheck disable=SC2086
eval git add $js_files
else
echo "[pre-commit] npx not found, skipping ESLint."
fi
fi
# Run Prettier --write on staged files
staged_files=""
while IFS= read -r file; do
[ -z "$file" ] && continue
staged_files="$staged_files '$file'"
done < <(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|mjs|cjs|json|md)$' || true)
if [ -n "$staged_files" ]; then
if command -v npx >/dev/null 2>&1; then
echo "[pre-commit] Running Prettier --write on staged files..."
# shellcheck disable=SC2086
if ! eval npx prettier --write $staged_files; then
echo "[pre-commit] Prettier --write failed. Please review the errors above."
exit 1
fi
echo "[pre-commit] Prettier formatting completed successfully."
# Re-add files that may have been formatted by Prettier
# shellcheck disable=SC2086
eval git add $staged_files
else
echo "[pre-commit] npx not found, skipping Prettier."
fi
fi
# Lint and fix markdown files with markdownlint-cli2
md_files=""
while IFS= read -r file; do
[ -z "$file" ] && continue
md_files="$md_files '$file'"
done < <(git diff --cached --name-only --diff-filter=ACM | grep -E '\.md$' || true)
if [ -n "$md_files" ]; then
if command -v npx >/dev/null 2>&1; then
echo "[pre-commit] Running markdownlint --fix on staged markdown files..."
# shellcheck disable=SC2086
if ! eval npx markdownlint-cli2 --fix $md_files; then
echo "[pre-commit] markdownlint --fix failed. Please review and fix the remaining issues above."
exit 1
fi
echo "[pre-commit] Markdown lint and fix completed successfully."
# Re-add files that may have been fixed by markdownlint
# shellcheck disable=SC2086
eval git add $md_files
else
echo "[pre-commit] npx not found, skipping markdown lint."
fi
fi
# Run tests in pre-commit if they are fast (skip if they take too long)
if [ -f "package.json" ] && grep -q '"test"' package.json; then
echo "[pre-commit] Running quick test suite..."
if ! npm run test 2>/dev/null; then
echo "[pre-commit] Tests failed. Please fix failing tests before committing."
exit 1
fi
echo "[pre-commit] Tests passed."
fi
echo "[pre-commit] All checks passed!"
exit 0