husky \ lint-staged \ commitlint 实现代码提交前检查

话不多说,直接上步骤:

1、安装husky

bash 复制代码
pnpm install husky -D

2、在 package.json 的 script 添加命令

bash 复制代码
"scripts": {
    "prepare": "husky install"
}

3、执行 pnpm run prepare

执行完项目根目录也会出现一个.husky文件夹

4、安装lint-staged

bash 复制代码
pnpm install lint-staged -D

5、配置 package.json

javascript 复制代码
{
  "lint-staged": {
    "**/*.{js,jsx,ts,tsx,vue,json,md}": "eslint --fix --ignore-path .gitignore"
  }
}

6、在 .husky 文件下建立 pre-commit 文件

bash 复制代码
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm exec lint-staged

7、安装

bash 复制代码
pnpm i @commitlint/cli @commitlint/config-conventional @esbuild-kit/cjs-loader -D

8、在 .husky 文件下建立 commit-msg 文件

bash 复制代码
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

pnpm exec commitlint --config commitlint.config.js --edit "${1}"

附:commitlint.config.ts 和 commitlint.config.js

TypeScript 复制代码
// commitlint.config.ts
import { execSync } from 'child_process'
import fg from 'fast-glob'

const getPackages = (packagePath) =>
  fg.sync('*', { cwd: packagePath, onlyDirectories: true })

const scopes = [
  ...getPackages('packages'),
  ...getPackages('internal'),
  'docs',
  'style',
  'ci',
  'var',
  'ssr',
]

const gitStatus = execSync('git status --porcelain || true')
  .toString()
  .trim()
  .split('\n')

const scopeComplete = gitStatus
  .find((r) => ~r.indexOf('M  packages'))
  ?.replace(/\//g, '%%')
  ?.match(/packages%%((\w|-)*)/)?.[1]

export default {
  rules: {
    /**
     * type[scope]: [function] description
     *      ^^^^^
     */
    'scope-enum': [2, 'always', scopes],
    /**
     * type[scope]: [function] description
     *
     * ^^^^^^^^^^^^^^ empty line.
     * - Something here
     */
    'body-leading-blank': [1, 'always'],
    /**
     * type[scope]: [function] description
     *
     * - something here
     *
     * ^^^^^^^^^^^^^^
     */
    'footer-leading-blank': [1, 'always'],
    /**
     * type[scope]: [function] description [No more than 72 characters]
     *      ^^^^^
     */
    'header-max-length': [2, 'always', 72],
    'scope-case': [2, 'always', 'lower-case'],
    'subject-case': [
      1,
      'never',
      ['sentence-case', 'start-case', 'pascal-case', 'upper-case'],
    ],
    'subject-empty': [2, 'never'],
    'subject-full-stop': [2, 'never', '.'],
    'type-case': [2, 'always', 'lower-case'],
    'type-empty': [2, 'never'],
    /**
     * type[scope]: [function] description
     * ^^^^
     */
    'type-enum': [
      2,
      'always',
      [
        'build',
        'chore',
        'ci',
        'docs',
        'feat',
        'fix',
        'perf',
        'refactor',
        'revert',
        'release',
        'style',
        'test',
        'improvement',
      ],
    ],
  },
  prompt: {
    defaultScope: scopeComplete,
    customScopesAlign: !scopeComplete ? 'top' : 'bottom',
    allowCustomIssuePrefixs: false,
    allowEmptyIssuePrefixs: false,
  },
}
javascript 复制代码
// commitlint.config.js
// commitlint uses `ts-node` to load typescript config, it's too slow. So we replace it with `esbuild`.
require('@esbuild-kit/cjs-loader')
module.exports = require('./commitlint.config.ts').default

然后试试提交代码,应该可以了!

相关推荐
GIS数据转换器2 分钟前
智慧林草“一张图“平台
java·大数据·服务器·前端·javascript·数据库·人工智能
fpcc5 分钟前
跟我学C++中级篇—static_assert和assert
开发语言·c++
2401_885885046 分钟前
国际语音php接口代码示例:PHP使用cURL快速调用语音发送API
android·开发语言·前端·人工智能·python·php·语音识别
leoZ2316 分钟前
AI+前端提效-08 AI自动化文档:前端组件、接口、项目文档自动生成
前端·人工智能·深度学习·神经网络·目标检测·自然语言处理·自动化
lilian23311 分钟前
HarmonyOS 7 新特性(五)|ContainerReader 容器断点与自适应布局
前端·华为·harmonyos
萌动的小火苗13 分钟前
Linux进程线程面试题【无答案】
linux·运维·服务器·c语言·开发语言
计算机魔术师24 分钟前
Google DeepMind 将 AI 科学家 Co-Scientist 扩展为实验室集成研究伙伴
前端
2601_9657984734 分钟前
BrandBoost WordPress Theme Review: Fast Agency Web Architecture
前端
律宏阔37 分钟前
Electron 集成 Drizzle + SQLite 踩坑笔记
前端
律宏阔39 分钟前
Electron preload.ts 类型无法自动推导的解决方案
前端