簡體   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