Maison > interface Web > js tutoriel > le corps du texte

Husky et lint-staged : maintenir la cohérence du code

WBOY
Libérer: 2024-07-20 10:23:38
original
376 Les gens l'ont consulté

When a team works on a software project together, it's important for everyone's code to be neat and easy to understand. But sometimes, different computers and ways of working can make the code messy. Tools like husky and lint-staged can help fix this problem by checking the code automatically before it's added to the project.


What is lint-staged?

lint-staged is a tool that checks your code for mistakes and fixes them when it's staged in git. By using lint-staged, it helps keep your code clean and consistent.

Installation

1 . Install lint-staged as a development dependency:

npm install --save-dev lint-staged
Copier après la connexion

2 . Configure lint-staged in your package.json file to run eslint and prettier on js and ts files.

"lint-staged": {
  "*.{js,jsx,ts,tsx}": [
    "eslint --fix --max-warnings=0", // both errors and warnings must be fixed
    // "eslint --fix" // errors must be fixed but warnings can be ignored
    "prettier --write"
  ]
}
Copier après la connexion

3 . Run lint-staged on staged files using the following command:

npx lint-staged
Copier après la connexion

What is husky?

husky is a tool that manages git hooks, automatically running scripts before each git commit. This setup ensures that lint-staged checks your code before it's committed. It helps you maintain code quality by catching issues before they're finalized.

Installation

1 . Install husky and initialize it:

# husky init (create .husky folder)
npx husky-init && npm install

# husky - Git hooks install
npx husky install
Copier après la connexion

2 . Check if prepare command is added in your package.json

"scripts": {
    "prepare": "husky install"
},
Copier après la connexion

3 . Edit .husky > pre-commit file with the following to run lint-staged before each commit

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
Copier après la connexion

How It Works

  1. Stage your code changes.
  2. husky triggers the pre-commit hook.
  3. The pre-commit hook executes lint-staged.
  4. lint-staged runs eslint and prettier checks on staged files.
  5. If errors or warnings are found, the commit is prevented with an error message.

Husky and lint-staged: Keeping Code Consistent


Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:dev.to
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!