Loading...
As the project grows larger it's critical for the whole team to follow certain code quality and...
Git hooks are scripts that run automatically every time a particular event occurs in a Git repository.Numerous git hook managers are available for Node.js. Husky, pre-commit, and Lefthook are a few examples of such tools. We opted for Lefthook for this tutorial due to its capability to execute scripts concurrently and its simplicity. So let's start! Before each commit(pre-commit) we need to ensure the following.
npm audit
or pnpm audit --audit-level high
{ "scripts":{ + "gitleaks": "gitleaks detect -v" } }
add the following changes to your package.jsonnpm init @eslint/config
{ "scripts":{ + "lint": "eslint --ignore-path .gitignore \"{src,tests}/**/*.+(ts|js|tsx)\"", } }
{ "scripts":{ + "typecheck": "tsc --noEmit", } }
Package.jsonnpm install --save-dev --save-exact prettier
{ "scripts":{ + "format": "prettier --ignore-path .prettierignore --write \"**/*.+(js|ts|json|tsx|mdx)\" --log-level silent", } }
Package.jsonnpm i validate-branch-name -D
{ "validate-branch-name": { "pattern": "^(feat|fix|hotfix|release|test|experimental)/.+$", "errorMsg": "Branch name validation failed" }, }
create the file commitlint.config.js at the rootnpm i @commitlint/cli -D npm i @commitlint/config-conventional -D
create the file .lefthook/commit-msg/commitlint.shmodule.exports = { extends: ['@commitlint/config-conventional'], // => @commitlint/config-conventional };
echo $(head -n1 $1) | npx commitlint --color
// install lefthook
npm install lefthook --save-dev
package.json# create a lefthook.yml file in the root pre-commit: parallel: true commands: lint: glob: '*.{js,ts,jsx,tsx}' # glob filter for list of files run: npm run lint format: run: npm run format types: glob: '*.{js,ts, jsx, tsx}' run: npm run typecheck gitLeaks: run: npm run gitleaks pre-push: parallel: true commands: branchName: run: npx validate-branch-name packages-audit: tags: frontend security run: npm audit commit-msg: parallel: true scripts: "commitlint.sh": runner: bash
Run{ "scripts":{ + "prepare": "lefthook install", } }
lefthook install
and try making your first commit.