有一个旧的 React App,我决定在项目中添加 eslint + prettier,然后想到通过使用 husky 添加 pre-commit hook 来禁止大家破坏项目。另外,为了不一次修复项目的所有代码,而仅在那些已更改的文件中添加 lint-staged 。这就是 package.json 的样子
{
"name": "Very intersing Project Best of the Best",
"version": "0.1.0",
"private": true,
"dependencies": {
"@many-many-diferen-dependencies": "^4.2.4",
},
"scripts": {
"start": "env-cmd -f .env.stage react-scripts start",
"start:dev": "env-cmd -f .env.dev react-scripts start",
"start:test": "env-cmd -f .env.test react-scripts start",
"start:ci": "env-cmd -f .env.ci react-scripts start",
"lint-staged": "lint-staged",
"lint": "eslint --debug src/",
"lint:write": "eslint --debug src/ --fix"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/core": "^7.10.2",
"@use-it/interval": "^0.1.3",
"babel-loader": "^8.1.0",
"camelcase": "^6.2.0",
"cross-env": "^7.0.2",
"env-cmd": "^10.1.0",
"eslint": "^6.6.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.5",
"eslint-plugin-react-hooks": "^4.0.8",
"font-awesome": "^4.7.0",
"http-proxy-middleware": "^1.0.4",
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"react-moment": "^1.0.0"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{js,jsx}": [
"eslint --fix",
"prettier --write"
]
}
}
什么是实际问题。我正在尝试添加一个 lint-staged 命令,该命令仅检查暂存文件而不首先将它们添加到暂存文件。现在这个命令只有在我执行 git status => git add 时才有效。我试图通过在此处添加 git add 来扩展“lint-staged”,但它不起作用。我想以某种方式运行这个命令。任何想法如何解决?
第二个问题可以有两个这样的块吗?
"lint-staged": {
"src/**/*.{js,jsx}": [
"eslint --fix",
"prettier --write"
]
}
像“lint-staged-1”?所以我称命令 lint-staged-1 而不是“lint-staged”。
第三个问题是如何添加一个命令来绕过 pre-commit ?这样我就可以在不运行这个钩子的情况下添加一个委员会?
我还没有测试过,但是你可以试试看它是否有效?