chore: light hook

This commit is contained in:
Slinetrac
2025-10-09 10:29:20 +08:00
Unverified
parent 4417fe6cd9
commit f5c2b2a23d
5 changed files with 37 additions and 135 deletions

View File

@@ -1,45 +1,24 @@
#!/bin/bash
set -euo pipefail
# Run lint-staged to handle formatting and linting for JS/TS files
# This handles prettier and eslint --fix automatically
npx lint-staged
# Check for any remaining linting errors in JS/TS files that weren't fixed automatically
# Only run this if JS/TS files are staged (lint-staged already handled these)
JS_FILES=$(git diff --cached --name-only | grep -E '\.(ts|tsx|js|jsx)' || true)
if [ -n "$JS_FILES" ]; then
echo "Verifying no remaining linting issues in staged files..."
# Use --max-warnings 0 to ensure no warnings either
if ! pnpm lint:staged --max-warnings 0; then
echo "ESLint found unfixable issues or warnings in staged files. Please fix them before committing."
echo "Files affected: $JS_FILES"
exit 1
fi
fi
# Check for staged Rust files and handle them
RUST_FILES=$(git diff --cached --name-only | grep -E '^src-tauri/.*\.(rs)$' || true)
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
# Check staged Rust files
RUST_FILES=$(git diff --cached --name-only | grep -E '^src-tauri/.*\.rs$' || true)
if [ -n "$RUST_FILES" ]; then
echo "Running rustfmt and clippy on staged Rust files..."
echo "[pre-commit] Running rustfmt and clippy on staged Rust files..."
cd src-tauri || exit
# Auto-format Rust code
cargo fmt
# Check if there are still formatting issues after auto-formatting
if ! cargo fmt -- --check; then
echo "rustfmt still found formatting issues after auto-formatting."
cd ..
exit 1
fi
# Run clippy for linting
if ! cargo clippy; then
echo "clippy found issues. Please fix them before committing."
cd ..
exit 1
fi
# Lint with clippy, print warnings but don't fail commit
cargo clippy || echo "⚠️ clippy found issues, but commit will continue."
cd ..
fi
# Allow commit
echo "[pre-commit] Checks completed. Some warnings may exist, please review."
exit 0

View File

@@ -1,26 +1,24 @@
#!/bin/bash
set -euo pipefail
# $1: remote name (e.g., origin)
# $2: remote url (e.g., git@github.com:clash-verge-rev/clash-verge-rev.git)
remote_name="$1"
# --- Rust clippy for staged files in src-tauri ---
if git diff --cached --name-only | grep -q '^src-tauri/'; then
cargo clippy --manifest-path ./src-tauri/Cargo.toml -- -D warnings
if [ $? -ne 0 ]; then
echo "Clippy found issues in src-tauri. Please fix them before pushing."
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
fi
}
fi
# Only run format check if the remote exists and is the main repo
remote_name="$1"
# --- 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
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..."
pnpm format:check
if [ $? -ne 0 ]; then
if ! pnpm format:check; then
echo "❌ Code format check failed. Please fix formatting before pushing."
exit 1
fi
@@ -28,7 +26,8 @@ if git remote get-url "$remote_name" >/dev/null 2>&1; then
echo "[pre-push] Not pushing to target repo. Skipping format check."
fi
else
echo "[pre-push] Remote $remote_name does not exist. Skipping format check."
echo "[pre-push] Remote '$remote_name' does not exist. Skipping format check."
fi
echo "[pre-push] All checks passed."
exit 0

View File

@@ -1,9 +0,0 @@
export default {
// Run Prettier on all supported file types
"*.{js,jsx,ts,tsx,json,css,scss,md,html}": ["prettier --write"],
// Run ESLint on JavaScript/TypeScript files with cache for performance
"*.{js,jsx,ts,tsx}": [
"eslint --cache --cache-location .eslintcache --fix",
"eslint --cache --cache-location .eslintcache",
],
};

View File

@@ -28,12 +28,9 @@
"clippy": "cargo clippy --manifest-path ./src-tauri/Cargo.toml",
"lint": "eslint -c eslint.config.ts --cache --cache-location .eslintcache src",
"lint:fix": "eslint -c eslint.config.ts --cache --cache-location .eslintcache --fix src",
"lint:staged": "eslint -c eslint.config.ts --cache --cache-location .eslintcache",
"lint:all": "node scripts/lint-all.mjs",
"format": "prettier --write .",
"format:check": "prettier --check .",
"type-check": "tsc --noEmit",
"validate": "pnpm format:check && pnpm lint && pnpm type-check"
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@dnd-kit/core": "^6.3.1",
@@ -121,6 +118,17 @@
"vite-plugin-monaco-editor": "^1.1.0",
"vite-plugin-svgr": "^4.5.0"
},
"lint-staged": {
"*.{ts,tsx,js,jsx}": [
"eslint --fix",
"prettier --write",
"git add"
],
"*.{css,scss,json,md}": [
"prettier --write",
"git add"
]
},
"type": "module",
"packageManager": "pnpm@9.13.2"
}

View File

@@ -1,75 +0,0 @@
#!/usr/bin/env node
import { spawn, spawnSync } from "child_process";
import { exit } from "process";
// Function to run a command
function runCommand(command, args, cwd = ".") {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: "inherit", cwd, shell: true });
child.on("close", (code) => {
if (code === 0) {
resolve();
} else {
reject(
new Error(`${command} ${args.join(" ")} exited with code ${code}`),
);
}
});
});
}
// Function to check if a command exists
function commandExists(command) {
try {
const result = spawnSync(command, ["--version"], { stdio: "pipe" });
return result.status === 0;
} catch (error) {
return false;
}
}
async function lintAll() {
console.log("Starting comprehensive lint process...");
try {
// Run TypeScript/JavaScript linting
console.log("\n--- Linting TypeScript/JavaScript files ---");
await runCommand("pnpm", ["lint"]);
// Run TypeScript type checking
console.log("\n--- Type checking ---");
await runCommand("pnpm", ["type-check"]);
// Run prettier check
console.log("\n--- Checking formatting ---");
await runCommand("pnpm", ["format:check"]);
console.log("\n--- Linting Rust files ---");
// Check for rustfmt
if (!commandExists("cargo")) {
throw new Error("Cargo is not available. Please install Rust and Cargo.");
}
// Run rustfmt
console.log("Running rustfmt...");
await runCommand("cargo", ["fmt", "--", "--check"], "./src-tauri");
// Run clippy
console.log("Running clippy...");
await runCommand(
"cargo",
["clippy", "--", "-D", "warnings"],
"./src-tauri",
);
console.log("\n✅ All linting checks passed!");
} catch (error) {
console.error("\n[ERROR] Linting failed:", error.message);
exit(1);
}
}
lintAll();