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

har*_*con 7 bash lint githooks 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"
Run Code Online (Sandbox Code Playgroud)

日志

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
Run Code Online (Sandbox Code Playgroud)

pix*_*its 8

修复提交时的 lint 问题

您可以在提交时自动修复所有 lint 问题。

步骤如下:

使用 eslint (将 YOUR_PROJECT_NAME 替换为您的 Angular 项目的名称)

ng add @angular-eslint/schematics
ng g @angular-eslint/schematics:convert-tslint-to-eslint YOUR_PROJECT_NAME
Run Code Online (Sandbox Code Playgroud)

安装 eslint 规则插件,以便您可以设置 JSON 规则:

npm install --save-dev eslint-plugin-json
Run Code Online (Sandbox Code Playgroud)

更新.eslintrc.json以使用该插件:

ng add @angular-eslint/schematics
ng g @angular-eslint/schematics:convert-tslint-to-eslint YOUR_PROJECT_NAME
Run Code Online (Sandbox Code Playgroud)

安装 prettier、husky 和 ​​lint-staged

npm install --save-dev prettier husky lint-staged
Run Code Online (Sandbox Code Playgroud)

全局安装 husky:

npm install husky -g
Run Code Online (Sandbox Code Playgroud)

安装 git hooks (这将创建.husky文件夹):

husky install  
Run Code Online (Sandbox Code Playgroud)

通过添加以下内容来设置 lint-staged package.config

  "devDependencies: {
     ...
  },
  "lint-staged": {
    "*.{js,json,css,scss,md,ts,html}": [
      "prettier --write",
      "eslint --fix"
    ]
  }
Run Code Online (Sandbox Code Playgroud)

可选:尝试使用 lint-staged 来测试到目前为止的配置

npx lint-staged
Run Code Online (Sandbox Code Playgroud)

添加预提交 git hook:

husky add .husky/pre-commit "npx lint-staged"
Run Code Online (Sandbox Code Playgroud)

修改package.json添加安装后步骤来安装husky:

"scripts": {
  "postinstall": "husky install && husky add .husky/pre-commit \"npx lint-staged\"",
   ...
},
Run Code Online (Sandbox Code Playgroud)

测试您的设置

修改一些 .ts、json 或 .html 文件

git add *
git commit -m "updated"
Run Code Online (Sandbox Code Playgroud)

你应该看到:

[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
Run Code Online (Sandbox Code Playgroud)