Skip to content

Instantly share code, notes, and snippets.

@ml-eds
Last active August 28, 2019 09:28
Show Gist options
  • Save ml-eds/8e0d05f7f4c33afa587a332515346e2b to your computer and use it in GitHub Desktop.
Save ml-eds/8e0d05f7f4c33afa587a332515346e2b to your computer and use it in GitHub Desktop.
Node + Typescript: How to configure git pre-commit hook with TSLint

TSLint pre-commit hook

Important!

Must be configured on top level directory of a project when having a multi-package project (e.g. frontend + backend).

Install

npm install --save-dev husky lint-staged

Configure

Add npm script "lint-staged" to package.json

"scripts": {
    "lint-staged": "lint-staged"
}

Add husky and lint-staged config to package.json

"husky": {
    "hooks": {
        "pre-commit": "npm run lint-staged"
    }
},
"lint-staged": {
    "*.ts": [
        "tslint -c tslint.json -p tsconfig.json",
        "git add"
    ]
}

Please adjust paths to tslint.json and tsconfig.json!

To make the hook stop when tslint finds a fault, change default severity for tslint in tslint.json:

"defaultSeverity": "error"

defaultSeverity "warning" will not stop the commit process!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment