如何解决如何在预提交时运行 ng lint 并检测错误?
我的预提交脚本有问题。我正在尝试运行我的 angular 项目预提交文件的“ng lint”;当检测到 lint 错误但预提交过程成功通过时,我会在日志中看到错误。 我需要在预提交文件中执行 ng-lint,因为我要运行其他验证...当 ng lint 未成功通过时,我如何获取消息?
我的脚本:
#!/bin/bash
### other scripts to validate custom commits
-----------
### Run ng lint
echo "pre-commit hook --> linting" ng lint
ng lint
echo -e "ng lint pass sucessfully \n\n"
日志
pre-commit hook --> linting
Linting "front-firefly-backoffice"...
/home/apanez/Escritorio/htdocs/eci/front-firefly/src/app/stock/limit-sale/components/limit-sale-detail/limit-sale-detail.component.ts:30:14
ERROR: 30:14 component-class-suffix The name of the class LimitSaleDetailComp should end with the suffix Component (https://angular.io/styleguide#style-02-03)
Lint errors found in the listed files.
ng lint pass sucessfully
解决方法
修复提交时的 lint 问题
您可以在提交时自动修复所有 lint 问题。
步骤如下:
使用 eslint(将 YOUR_PROJECT_NAME 替换为您的 Angular 项目名称)
ng add @angular-eslint/schematics
ng g @angular-eslint/schematics:convert-tslint-to-eslint YOUR_PROJECT_NAME
安装 eslint 规则插件,以便您可以设置 JSON 规则:
npm install --save-dev eslint-plugin-json
更新 .eslintrc.json
以使用插件:
{
"extends": ["plugin:json/recommended"],...
}
安装 prettier、husky 和 lint-staged
npm install --save-dev prettier husky lint-staged
全局安装 husky:
npm install husky -g
安装 git hooks(这将创建 .husky
文件夹):
husky install
通过将以下内容添加到 package.config
来设置 lint-staged:
"devDependencies: {
...
},"lint-staged": {
"*.{js,json,css,scss,md,ts,html}": [
"prettier --write","eslint --fix"
]
}
可选:到目前为止,尝试使用 lint-staged 测试您的配置
npx lint-staged
添加预提交的 git 钩子:
husky add .husky/pre-commit "npx lint-staged"
修改 package.json
以添加安装后步骤以安装 husky:
"scripts": {
"postinstall": "husky install && husky add .husky/pre-commit \"npx lint-staged\"",...
},
测试您的设置
修改一些 .ts、json 或 .html 文件
git add *
git commit -m "updated"
你应该看到:
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.{js,html}
[STARTED] prettier --write
[SUCCESS] prettier --write
[STARTED] eslint --fix
[SUCCESS] eslint --fix
[SUCCESS] Running tasks for *.{js,html}
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SUCCESS] Applying modifications...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...
[master 20fdf85] updated
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。