【问题标题】:How can I run ng lint and detect errors on pre-commit?如何运行 ng lint 并在预提交时检测错误?
【发布时间】:2021-07-14 12:11:00
【问题描述】:

我的预提交脚本有问题。我正在尝试运行我的 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

【问题讨论】:

  • 使用子shell 捕获ng lint 输出和LINTMSG="$(ng lint)"ng lint 可能会在成功完成时返回 0,而在失败时可能会返回任何其他值。然后您可以使用if [ $? != 0 ]; then { processing error from $LINTMSG } 进行测试。
  • 谢谢!!!是什么?在 [$? != 0 ]
  • $? 设置为最后执行命令的退出代码。大多数命令(无论是否内置)都将返回 0 表示成功,而其他任何命令都表示失败。我建议您阅读 bash 手册页,特殊参数部分。
  • 我会的!!对不起,我对 bash 命令了解不多,谢谢!!
  • set -e 在你的 shell 脚本中会使出错的子命令出错

标签: angular bash githooks lint


【解决方案1】:

修复提交时的 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,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

【讨论】:

  • 你应该改变 `husky add .husky/pre-commit \"npx lint-staged\"` 为 `husky set .husky/pre-commit \"npx lint-staged\"` 否则' add' 命令会在你每次运行 npm install 时在你的 pre-commit 文件中添加一个新的npx lint-staged
猜你喜欢
  • 2018-10-07
  • 2018-07-22
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多