如何在tslint中添加git prehook?

时间:2018-07-02 18:32:46

标签: node.js git tslint

如果代码提交失败,我会尝试添加预钩。什么是正确的实现方法。

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

3 个答案:

答案 0 :(得分:2)

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

  

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

答案 1 :(得分:2)

我在以下方面实现了成功的经验:

  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中,在lint-staged字段中指定precommitscripts

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

答案 2 :(得分:0)

package.json:

class BaseClass {
    protected obj: string[] = [];
    Todo() {
        this.obj.push("1");
        this.obj.push("2");
        this.obj.push("3");
    }
}
class ChildClass1 extends BaseClass {
    Todo() {
        super.Todo();
        this.obj.push("4");
        this.obj.push("5");
        this.obj.push("6");
    }
}
class ChildClass2 extends BaseClass {
    Todo() {
        super.Todo();
        this.obj.push("7");
        this.obj.push("8");
        this.obj.push("9");
    }
}
var child1 = new ChildClass1();
var child2 = new ChildClass2();
child1.Todo();
child2.Todo();