Git hooks and code style Validator
English | 简体中文
This is a cli tool with following features.
- Check code style (using ESLint and Prettier under the hood).
- Provide the strictest
tsconfig
and type guard preset for TypeScript project. - Set up git hooks to automatically check code style.
- 🔧 Zero Configuration: ZERO configuration file is needed, while still allowing customization of
eslint
andprettier
rules. - 📦 Unified package: Just ONE npm package to install, replacing multiple. You don't need
eslint
,prettier
,lint-staged
any more. - 🚀 Simple to use: TWO core commands:
git-validator -u
andgit-validator install
.
Run this command in your project to check the codebase code style.
npx git-validator
Usually, we recommend you to install it and set it up in your project. Please continue reading the document below.
Run command below in the root of your project to install this tool.
npm install -D git-validator
Edit package.json > prepare
script and run it once.
{
"scripts": {
"prepare": "git-validator install",
"style": "git-validator", // Run this to check the whole project code style
"style:update": "git-validator -u" // Run this to check the whole project code style and apply fixes
}
}
npm run prepare
After running git-validator install
, each commit (using Git) will check the committed files' code style.
Powered by @git-validator/eslint-config, we now support .js
/ .mjs
/ .cjs
/ .jsx
/ .ts
/ .mts
/ .cts
/ .tsx
/ package.json
by default.
This tool has integrated @git-validator/tsconfig. Optionally, you can setup tsconfig.json
using git-validator/tsconfig
if you like. It provides a more consistent development experience.
// tsconfig.json
{
"extends": "git-validator/tsconfig"
}
For more best practices, please refer to the document of @git-validator/tsconfig
.
The default linting rule is @git-validator/eslint-config and the default formatting rule is @git-validator/prettier-config. You can add eslint.config.js
and prettier.config.js
in the root of project to apply your own linting & formatting rules.
eslint.config.js
example.
// You can also install and use other eslint config preset, like `@sxzz/eslint-config`.
import { Builder } from "@git-validator/eslint-config";
export default new Builder()
.enableJavascript()
.enableTypescript({
omit: ["no-var"], // Omit the rules as you want
})
.toConfig();
prettier.config.js
example.
import config from "@git-validator/prettier-config";
export default {
...config,
printWidth: 120,
};
By default, you don't need .eslintignore
and .prettierignore
files in the root of project.
We recommend you to use this tool in zero configs. If you have better suggestions about linting and formatting rules, please submit the issue or PR. Any reasonable suggestions are welcome!
When you commit your code, each file will be linted & formatted by npx git-validator -w
command. You can change this rule by adding a lint-staged.config.js
file in the root of your project. Here is an example.
// This config means js files will be linted & formatted and md files will formatted only.
export default {
"*.js": "npx git-validator -u",
"*.md": "npx git-validator format -u",
};
When you commit you code, it will lint (using eslint
) code first and then format (using prettier
) code. If you want to skip one of them, you can pass --no-lint
or --no-format
option when running git install
.
{
"scripts": {
// it will not lint code and will only format code when you commit your code
"prepare": "git-validator install --no-lint"
}
}
{
"scripts": {
// it will not format code and will only lint code when you commit your code
"prepare": "git-validator install --no-format"
}
}
This library can work as a standalone package. However, if you have Husky 5 or a later version installed and you have run git-validator install
, you should add .husky/pre-commit
file:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
.git/hooks/pre-commit $1
- Clone this repository.
- Enable Corepack using
corepack enable
. - Install dependencies using
pnpm install
. - Run
pnpm style:update
to develop. - Start coding and submit your PR.
Give a ⭐️ if this project helped you!
MIT