繁体   English   中英

如何在tslint中添加git prehook?

[英]How to add git prehook to tslint?

我正在尝试添加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

我在以下方面具有成功的实施经验:

  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"   
    ]
}

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

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

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"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM