From 95aee6ec81efec4d92511afb0a2091c2efdc9246 Mon Sep 17 00:00:00 2001 From: Slinetrac Date: Wed, 22 Oct 2025 00:08:16 +0800 Subject: [PATCH] chore: better pre-hooks --- .husky/pre-commit | 51 ++++++++++++++++++++++++++++---------------- .husky/pre-push | 54 ++++++++++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 13cea09b..77736fde 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,24 +1,39 @@ #!/bin/bash set -euo pipefail -echo "[pre-commit] Running lint-staged for JS/TS files..." -# Auto-fix staged JS/TS files, print warnings but don't fail commit -npx lint-staged || true +ROOT_DIR="$(git rev-parse --show-toplevel)" +cd "$ROOT_DIR" -# Check staged Rust files -RUST_FILES=$(git diff --cached --name-only | grep -E '^src-tauri/.*\.rs$' || true) -if [ -n "$RUST_FILES" ]; then - echo "[pre-commit] Running rustfmt and clippy on staged Rust files..." - cd src-tauri || exit - - # Auto-format Rust code - cargo fmt - - # Lint with clippy, print warnings but don't fail commit - cargo clippy-all || echo "⚠️ clippy found issues, but commit will continue." - - cd .. +if ! command -v pnpm >/dev/null 2>&1; then + echo "❌ pnpm is required for pre-commit checks." + exit 1 fi -echo "[pre-commit] Checks completed. Some warnings may exist, please review." -exit 0 +echo "[pre-commit] Running lint-staged for JS/TS files..." +pnpm exec lint-staged + +RUST_FILES="$(git diff --cached --name-only --diff-filter=ACMR | grep -E '^src-tauri/.*\.rs$' || true)" +if [ -n "$RUST_FILES" ]; then + echo "[pre-commit] Formatting Rust changes with cargo fmt..." + ( + cd src-tauri + cargo fmt + ) + while IFS= read -r file; do + [ -n "$file" ] && git add "$file" + done <<< "$RUST_FILES" + + echo "[pre-commit] Linting Rust changes with cargo clippy..." + ( + cd src-tauri + cargo clippy-all + ) +fi + +TS_FILES="$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(ts|tsx)$' || true)" +if [ -n "$TS_FILES" ]; then + echo "[pre-commit] Running TypeScript type check..." + pnpm typecheck +fi + +echo "[pre-commit] All checks completed successfully." diff --git a/.husky/pre-push b/.husky/pre-push index 0cc5195d..43ffed41 100644 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -1,32 +1,42 @@ #!/bin/bash set -euo pipefail -remote_name="$1" +remote_name="${1:-origin}" +remote_url="${2:-unknown}" -# --- Rust clippy for staged files in src-tauri --- -if git diff --cached --name-only | grep -q '^src-tauri/'; then - echo "[pre-push] Running clippy on src-tauri..." - cargo clippy --manifest-path ./src-tauri/Cargo.toml -- -D warnings || { - echo "❌ Clippy found issues in src-tauri. Please fix them before pushing." - exit 1 - } +ROOT_DIR="$(git rev-parse --show-toplevel)" +cd "$ROOT_DIR" + +if ! command -v pnpm >/dev/null 2>&1; then + echo "❌ pnpm is required for pre-push checks." + exit 1 fi -# --- JS/TS format check only for main repo --- -if git remote get-url "$remote_name" >/dev/null 2>&1; then - remote_url=$(git remote get-url "$remote_name") - if [[ "$remote_url" =~ github\.com[:/]+clash-verge-rev/clash-verge-rev(\.git)?$ ]]; then - echo "[pre-push] Detected push to clash-verge-rev/clash-verge-rev ($remote_url)" - echo "[pre-push] Running pnpm format:check..." - if ! pnpm format:check; then - echo "❌ Code format check failed. Please fix formatting before pushing." - exit 1 - fi - else - echo "[pre-push] Not pushing to target repo. Skipping format check." - fi +echo "[pre-push] Preparing to push to '$remote_name' ($remote_url). Running full validation..." + +echo "[pre-push] Checking Prettier formatting..." +pnpm format:check + +echo "[pre-push] Running ESLint..." +pnpm lint + +echo "[pre-push] Running TypeScript type checking..." +pnpm typecheck + +if command -v cargo >/dev/null 2>&1; then + echo "[pre-push] Verifying Rust formatting..." + ( + cd src-tauri + cargo fmt --check + ) + + echo "[pre-push] Running cargo clippy..." + ( + cd src-tauri + cargo clippy-all + ) else - echo "[pre-push] Remote '$remote_name' does not exist. Skipping format check." + echo "[pre-push] ⚠️ cargo not found; skipping Rust checks." fi echo "[pre-push] All checks passed."