【问题标题】:How to add git prehook to tslint?如何将 git prehook 添加到 tslint?
【发布时间】:2018-12-11 00:32:28
【问题描述】:

如果代码存在任何无法提交的 linting 问题,我正在尝试添加 prehook。实现它的正确方法是什么。

tslint.sh

#!/bin/sh
sh ./npm-install.sh
if [ $? -ne 0 ]; then
  echo "npm-install error, exiting.."
  exit 1
fi
echo "Running ts lint"
npm run lint
if [ $? -ne 0 ]; then
  echo "Unit tests error, exiting.."
  exit 1
fi

【问题讨论】:

    标签: node.js git tslint


    【解决方案1】:

    我有一个成功的经验来实现这一点:

    1. husky => 指定 git hook
    2. lint-staged => 对 git 中的暂存文件运行命令(因此无需对所有文件运行 tslint)

    参考:

    1. https://github.com/okonet/lint-staged
    2. https://www.npmjs.com/package/husky

    package.json 中,在husky 字段中指定lint-stagedpre-commit

    "dependencies": ...,
    "devDependencies": ...,
    "scripts" ...,
    "husky": {
        "hooks": {
            "pre-commit": "lint-staged"
        }
    },
    "lint-staged": {
        "*.ts": [ // target to all typescript files in staged stage in git
          "npm run lint", // your lint command
          "git add"   
        ]
    }
    

    【讨论】:

      【解决方案2】:

      这是一种方式:https://www.npmjs.com/package/pre-commit

      pre-commit 是 git 的预提交挂钩安装程序。它将确保您的 npm 测试(或其他指定的脚本)在您提交更改之前通过。这一切都在您的 package.json 中方便地配置。

      【讨论】:

        【解决方案3】:

        package.json:

        {
          "name": "app name",
          "version": "0.1.0",
          "license": "MIT",
          "author": "author",
          "contributors": [
        
          ],
          "description": "...",
          "scripts": {
            "ng": "ng",
            "precommit": "lint-staged"
          },
          ...,
          "lint-staged": {
            "*.{ts,js,sccs,json}": [
              "ng lint app-name --fix",
              "./node_modules/.bin/prettier --write",
              "git add"
            ]
          },
          "dependencies": {
            ...
          },
          "devDependencies": {
            ...
            "lint-staged": "^7.2.0",
            "prettier": "^1.13.5",
            "ts-node": "^6.1.2",
            "tslint": "^5.10.0",
            "typescript": "2.7.2"
          }
        }
        

        【讨论】:

        • 您不需要将app-name 添加为 linter 选项,通常将其声明为脚本然后调用它是一种更好的做法。 wtf 是sccs??
        猜你喜欢
        • 2017-10-08
        • 2018-07-15
        • 2015-11-05
        • 2018-11-14
        • 2021-04-02
        • 2014-09-14
        • 2019-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多