Skip to content

Husky

  1. 安装
bash
npm i -D husky
bash
yarn add -D husky
bash
pnpm add -D husky
bash
bun add -D husky
  1. 初始化husky
bash
npx husky init

在初始化之前 要先创建仓库 -> git init

它会做两件事

  • 创建.husky目录
.husky
├── pre-commit
  • package.json里加
json
"scripts": {
    "prepare": "husky"
}

pre-commit

.husky/pre-commit

bash
#!/usr/bin/env sh
pnpm run lint:vue && pnpm lint:style && pnpm spellcheck

每次git commit 都会执行pnpm run lint:vue && pnpm lint:style && pnpm spellcheck

lint-staged

husky搭配lint-staged使用

  1. 安装
bash
npm i -D lint-staged
bash
yarn add -D lint-staged
bash
pnpm add -D lint-staged
bash
bun add -D lint-staged
  1. package.json 里加
json
"lint-staged": {
    "*.{vue,js,ts,jsx,tsx}": [
        "eslint --fix",
        "prettier --write"
    ]
}
  1. 修改.husky/pre-commit
bash
#!/usr/bin/env sh
npx lint-staged