angular - 如何在预提交时运行 ng lint 并检测错误?

标签 angular bash githooks 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
全局安装哈士奇:
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 钩子(Hook):
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,json,css,scss,md,ts,html}
[STARTED] prettier --write
[SUCCESS] prettier --write
[STARTED] eslint --fix
[SUCCESS] eslint --fix
[SUCCESS] Running tasks for *.{js,json,css,scss,md,ts,html}
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SUCCESS] Applying modifications...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...
[master 20fdf85] updated

关于angular - 如何在预提交时运行 ng lint 并检测错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67176520/

相关文章:

python - 在终端中每行的开头插入文件名中的更改字符串

git - 如何在 Jenkins 中一次只远程触发一个构建?

git - 使用GIT部署网站

angular - 错误 : No component factory found for MatSnackBarContainer

linux - awk 输出到变量

javascript - 如何将文本字段中的填充值保存到 Ionic 6/Capacitor 中的 SQLite 数据库?

bash - 使用 shell 变量传递选项时,autotools 配置错误

git - 如果我们更改 `git config core.hooksPath` ,如何恢复到默认值

angular - TS2339 : Property 'assign' does not exist on type

Angular2 使用指令在标签中插入自定义属性