#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# 在提交前运行代码格式化和ESLint检查
echo "正在运行代码格式化和检查..."

# 运行ESLint修复
if ! npx eslint --fix --ext .js,.jsx,.html --max-warnings 0 $(git diff --staged --name-only --diff-filter=ACM); then
  echo "ESLint检查失败，请修复代码中的问题。"
  exit 1
fi

# 运行Prettier格式化
if ! npx prettier --write --ignore-unknown $(git diff --staged --name-only --diff-filter=ACM); then
  echo "Prettier格式化失败。"
  exit 1
fi

# 自动将格式化后的文件添加到暂存区
git add $(git diff --name-only --diff-filter=ACM)

echo "代码格式化和检查完成！"
