Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add lint-staged and husky to package.json #741

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lint-staged
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

⚠️ Potential issue

Based on the results, I can now provide a final response as we have confirmed:

  1. The pre-commit hook lacks executable permissions
  2. The husky setup is incomplete (missing .husky/_ directory)
  3. The package.json has husky and lint-staged configurations

需要完善 Git Hook 配置和权限设置

pre-commit 钩子脚本存在以下问题需要修正:

  • 文件内容需要更新:
+#!/usr/bin/env sh
+. "$(dirname -- "$0")/_/husky.sh"
+
-lint-staged
+npx lint-staged
  • 需要执行以下命令来设置正确的文件权限:
chmod +x .husky/pre-commit
  • Husky 设置不完整,需要重新执行初始化:
npm run prepare

这些更改将确保:

  1. 脚本能被正确解释执行
  2. Git Hook 具有必要的执行权限
  3. Husky 的依赖文件被正确安装
🔗 Analysis chain

需要完善 shell 脚本的基本要求

这个 pre-commit 钩子脚本缺少必要的 shell 脚本组件,可能会导致执行失败。建议进行以下修改:

应用以下更改:

+#!/usr/bin/env sh
+. "$(dirname -- "$0")/_/husky.sh"
+
-lint-staged
+npx lint-staged

修改说明:

  1. 添加 shebang 指令以确保正确的解释器
  2. 导入 husky 的 shell 助手函数
  3. 使用 npx 来执行 lint-staged 命令

Let me verify the husky setup further to provide a comprehensive response.

Based on the results, I'll generate a final response in Chinese that addresses both the script content and file permissions issues.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 验证文件是否具有可执行权限
ls -l .husky/pre-commit

Length of output: 90


Script:

#!/bin/bash
# Check package.json for husky and lint-staged configuration
cat package.json | jq '.scripts,.husky,.["lint-staged"]'

# Check if husky.sh exists
ls -l .husky/_

Length of output: 626

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"prepublishOnly": "npm run compile && np --yolo --no-publish",
"postpublish": "tnpm sync rc-menu",
"start": "dumi dev",
"test": "rc-test"
"test": "rc-test",
"prepare": "husky && dumi setup"
},
"dependencies": {
"@babel/runtime": "^7.10.1",
Expand All @@ -66,7 +67,9 @@
"eslint-plugin-unicorn": "^51.0.1",
"father": "^4.0.0",
"gh-pages": "^6.1.0",
"husky": "^9.1.6",
"less": "^4.1.3",
"lint-staged": "^15.2.10",
"np": "^10.0.5",
"rc-test": "^7.0.14",
"react": "^18.3.1",
Expand All @@ -77,5 +80,8 @@
"peerDependencies": {
"react": ">=16.9.0",
"react-dom": ">=16.9.0"
},
"lint-staged": {
"*": "prettier --write --ignore-unknown"
Comment on lines +84 to +85
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

lint-staged 配置优化建议

当前配置对所有文件类型使用 Prettier 进行格式化,这可能不是最优的方案。

建议根据文件类型设置不同的 lint 规则:

 "lint-staged": {
-  "*": "prettier --write --ignore-unknown"
+  "*.{js,jsx,ts,tsx}": [
+    "eslint --fix",
+    "prettier --write"
+  ],
+  "*.{json,md,yml}": "prettier --write"
 }

这样可以:

  1. 对 JavaScript/TypeScript 文件进行 ESLint 检查
  2. 针对不同文件类型使用合适的格式化规则
  3. 提高提交前检查的效率
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"lint-staged": {
"*": "prettier --write --ignore-unknown"
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
],
"*.{json,md,yml}": "prettier --write"
}

}
}
Loading