为了确保只有合格的代码才能够提交到仓库。需要配置自动化脚本,确保代码在提交前通过了代码验证工具的检验。
实际上 git 本身就设计了生命周期钩子来完成这个任务。但是设置过程比较复杂。所以通常情况下会使用 husky 来简化配置。
bashpnpm i husky -D
会创建一个 npm script
npm set-script prepare "husky install"
执行下方命令,以添加生命周期钩子:
sqlnpx husky add .husky/pre-commit "pnpm lint"
会创建 .husky/pre-commit
文件,其内容如下
bash#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
pnpm lint
在每次提交时,都将会执行 lint 脚本来检查代码。
不过更多的做法都是用 github action 配置 CI 在虚拟机上跑测试,而不是本地测试。(故这步可省略)
执行下方命令,以添加生命周期钩子:
bashnpx husky add .husky/pre-push "pnpm test"
commitlint - Lint commit messages
安装
bashpnpm i -g @commitlint/cli @commitlint/config-conventional
bashecho "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
:::warning 注意
windows 系统请勿使用上行命令,否则会导致编码不是 UTF-8。建议直接复制文本内容到 commitlint.config.js
javascriptmodule.exports = { extends: ['@commitlint/config-conventional'] }
:::
将 commitlint 脚本添加到 githooks 中, 让每次提交前都验证信息是否正常。
bashnpx husky add .husky/commit-msg "npx --no-install commitlint --edit "$1""
其内容如下
bash#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no-install commitlint --edit "$1"
测试 commit 提交 echo 'foo: bar' | commitlint
将会报错,不符合 commit msg 规范。
echo 'foo: bar' | commitlint ⧗ input: foo: bar✖ type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum] ✖ found 1 problems, 0 warnings ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
本文作者:任浪漫
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明出处!