Home > Web Front-end > JS Tutorial > body text

Enhancing Your Development Workflow with Husky, Commitlint, Prettier, and Lint-Staged

Susan Sarandon
Release: 2024-11-01 06:03:31
Original
970 people have browsed it

Enhancing Your Development Workflow with Husky, Commitlint, Prettier, and Lint-Staged

Setting up an automated workflow can greatly enhance code quality and consistency in your projects. In this guide, we’ll walk through setting up Husky, Commitlint, Prettier, and Lint-Staged to ensure your codebase is consistently formatted, follows commit message conventions, and has up-to-date dependencies after each merge.

Setting Up Husky

Husky helps you manage Git hooks effortlessly, allowing for automated tasks like code quality checks to run before every commit.

Installation

Install Husky as a dev dependency using npm (we will be using npm in this article):

npm install --save-dev husky
Copy after login

Initialization

To create a .husky directory where Git hooks will be stored, run:

npx husky init
Copy after login

Next, add the following script in your package.json to set up Husky when installing dependencies:

"scripts": {
  "prepare": "husky install"
}
Copy after login

Configuring Commitlint

Commitlint ensures that all commit messages follow a consistent format, maintaining a clean commit history.

Installation

Install Commitlint along with a conventional config:

npm install --save-dev @commitlint/config-conventional @commitlint/cli
Copy after login

Setup

  1. Create a commit-msg hook in .husky: Now create a new file in .husky directory named commit-msg and add this line:
npx husky add .husky/commit-msg "npx --no-install commitlint --edit \"\""
Copy after login
  1. Add a commitlint.config.js file to the root of your project with the following content:
module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    //   TODO Add Scope Enum Here
    // 'scope-enum': [2, 'always', ['yourscope', 'yourscope']],
    'type-enum': [
      2,
      'always',
      [
        'feat',
        'fix',
        'docs',
        'chore',
        'style',
        'refactor',
        'ci',
        'test',
        'revert',
        'perf',
        'vercel',
      ],
    ],
  },
};
Copy after login

Adding Lint-Staged and Prettier

Lint-Staged allows you to run scripts on staged files, and Prettier enforces a consistent style in your codebase.

Installation

Install both as dev dependencies:

npm install --save-dev lint-staged prettier
Copy after login

Prettier Configuration

Create a .prettierrc.json file in your project root with your preferred configuration. Here’s an example:

{
  "plugins": ["prettier-plugin-tailwindcss"],
  "printWidth": 120,
  "useTabs": false,
  "tabWidth": 2,
  "trailingComma": "es5", 
  "semi": true, 
  "singleQuote": true,
  "bracketSpacing": true, 
  "arrowParens": "always",
  "jsxSingleQuote": false, 
  "bracketSameLine": false,
  "endOfLine": "lf"
}
Copy after login

Lint-Staged Configuration

Add the following configuration to your package.json under lint-staged:

"lint-staged": {
    "**/*.{js,jsx,ts,tsx}": [
      "eslint --max-warnings=0",
      "prettier --write"
    ],
    "**/*.{html,json,css,scss,md,mdx}": [
      "prettier -w"
    ]
  }
Copy after login

Add a pre-commit hook to run Lint-Staged:

npx husky add .husky/pre-commit "npx lint-staged"
Copy after login

Adding a Post-Merge Hook for Dependencies

A post-merge hook ensures that your dependencies are updated after each merge by running npm install or any package manager.

Create a post-merge hook:

npx husky add .husky/post-merge "npm install"
Copy after login

Conclusion

With this setup, your project will maintain a standardized commit message format, automatically format code, and keep dependencies up-to-date post-merge. This robust workflow will streamline collaboration and improve code quality, helping you focus on building great features.

The above is the detailed content of Enhancing Your Development Workflow with Husky, Commitlint, Prettier, and Lint-Staged. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!