#!/bin/bash
# pre-push: Run comprehensive tests before pushing
set -euo pipefail
echo "[pre-push] Running pre-push checks..."
# Clean up any leftover test processes first
if [ -f "scripts/cleanup-test-processes.sh" ]; then
echo "[pre-push] π§Ή Cleaning up leftover test processes..."
./scripts/cleanup-test-processes.sh
echo "[pre-push] β
Process cleanup complete."
fi
# Check if we're in a Node.js project
if [ ! -f "package.json" ]; then
echo "[pre-push] No package.json found, skipping Node.js checks"
exit 0
fi
# Run full test suite
if grep -q '"test"' package.json; then
echo "[pre-push] Running full test suite..."
if ! npm test; then
echo "[pre-push] β Tests failed. Push aborted."
echo "[pre-push] π§ͺ To fix: Review test failures above and fix the issues"
exit 1
fi
echo "[pre-push] β
All tests passed."
else
echo "[pre-push] No test script found in package.json, skipping tests."
fi
# Run test coverage check if available
if grep -q '"test:coverage"' package.json; then
echo "[pre-push] Running test coverage check..."
if ! npm run test:coverage; then
echo "[pre-push] β Coverage check failed. Push aborted."
echo "[pre-push] π To fix: Add tests to improve coverage or review coverage requirements"
exit 1
fi
echo "[pre-push] β
Coverage check passed."
fi
# Run security audit check
if command -v npm >/dev/null 2>&1; then
echo "[pre-push] Running security audit..."
if ! npm audit --audit-level=high; then
echo "[pre-push] β Security audit failed. Push aborted."
echo "[pre-push] π To fix: Run 'npm audit fix' to fix vulnerabilities automatically"
echo "[pre-push] Or run 'npm audit' to see detailed vulnerability information"
echo "[pre-push] For manual fixes, review and update vulnerable dependencies"
exit 1
fi
echo "[pre-push] β
Security audit passed."
fi
# Run lint check to ensure code quality
if command -v npx >/dev/null 2>&1; then
echo "[pre-push] Running comprehensive lint check..."
# Check JavaScript files
if find . -name "*.js" -not -path "./node_modules/*" -not -path "./coverage/*" | head -1 | grep -q .; then
if ! npx eslint .; then
echo "[pre-push] β ESLint check failed. Push aborted."
echo "[pre-push] π§ To fix: Run 'npm run lint:fix' and commit the changes"
exit 1
fi
echo "[pre-push] β
ESLint check passed."
fi
# Check formatting
if ! npx prettier --check "**/*.{js,mjs,cjs,json,md}" --ignore-path .gitignore; then
echo "[pre-push] β Prettier formatting check failed. Push aborted."
echo "[pre-push] π
To fix: Run 'npm run format' and commit the changes"
exit 1
fi
echo "[pre-push] β
Prettier formatting check passed."
# Check markdown
if find . -name "*.md" -not -path "./node_modules/*" | head -1 | grep -q .; then
if ! npx markdownlint-cli2; then
echo "[pre-push] β Markdown lint failed. Push aborted."
echo "[pre-push] π To fix: Run 'npm run markdown:fix' and commit the changes"
exit 1
fi
echo "[pre-push] β
Markdown lint passed."
# Check for dead links in markdown files
echo "[pre-push] Checking for dead links in markdown files..."
if ! npm run links:check:ci; then
echo "[pre-push] β Dead links found in markdown files. Push aborted."
echo "[pre-push] π To fix: Run 'npm run links:check' to see details and update broken links"
exit 1
fi
echo "[pre-push] β
Link checking passed."
fi
fi
echo "[pre-push] All pre-push checks passed! π"
exit 0