Rumah hujung hadapan web tutorial js Husky and lint-staged: Mengekalkan Kod Konsisten

Husky and lint-staged: Mengekalkan Kod Konsisten

Jul 20, 2024 am 10:23 AM

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
Salin selepas log masuk

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"
  ]
}
Salin selepas log masuk

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

npx lint-staged
Salin selepas log masuk

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
Salin selepas log masuk

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

"scripts": {
    "prepare": "husky install"
},
Salin selepas log masuk

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
Salin selepas log masuk

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


Atas ialah kandungan terperinci Husky and lint-staged: Mengekalkan Kod Konsisten. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Tag artikel panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Ganti aksara rentetan dalam javascript Ganti aksara rentetan dalam javascript Mar 11, 2025 am 12:07 AM

Ganti aksara rentetan dalam javascript

Tutorial Persediaan API Carian Google Custom Tutorial Persediaan API Carian Google Custom Mar 04, 2025 am 01:06 AM

Tutorial Persediaan API Carian Google Custom

Contoh warna json fail Contoh warna json fail Mar 03, 2025 am 12:35 AM

Contoh warna json fail

8 plugin susun atur halaman jquery yang menakjubkan 8 plugin susun atur halaman jquery yang menakjubkan Mar 06, 2025 am 12:48 AM

8 plugin susun atur halaman jquery yang menakjubkan

10 JQuery Syntax Highlighters 10 JQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 JQuery Syntax Highlighters

Bina Aplikasi Web Ajax anda sendiri Bina Aplikasi Web Ajax anda sendiri Mar 09, 2025 am 12:11 AM

Bina Aplikasi Web Ajax anda sendiri

Apa itu ' ini ' Dalam JavaScript? Apa itu ' ini ' Dalam JavaScript? Mar 04, 2025 am 01:15 AM

Apa itu ' ini ' Dalam JavaScript?

10 JavaScript & JQuery MVC Tutorial 10 JavaScript & JQuery MVC Tutorial Mar 02, 2025 am 01:16 AM

10 JavaScript & JQuery MVC Tutorial

See all articles