一、操作 git 钩子的工具
组件库是一个多人协作的项目,Git 的提交说明精准,在后期协作以及 Bug 处理时会变得有据可查,项目的开发可以根据规范的提交说明快速生成开发日志,从而方便开发者或用户追踪项目的开发信息和功能特性。提交规范使用的是 Angular 团队规范
commit message 提交符合 Angular 团队规范,需要在 comit 之前做校验,husky 工具可以定义拦截 git 钩子,对提交的文件和信息做校验和自动修复
1、安装
a、安装命令
pnpm add husky -D
b、初始化.husky,并指定该目录为 git hooks 所在的目录
--no-install 参数表示强制 npx 使用项目中 node_modules 目录下的husky依赖包
npx --no-install husky install
c、创建 pre-commit
npx --no-instal husky add .husky/pre-commit "npm run lint"
pre-commit 在 commit 之前会执行 npm run lint 校验代码,可以定义你的执行脚本,校验不通过不允许 commit 提交
2、配置辅助 commit 信息
commitizen 是一个撰写符合 Commit Message 标准的一款工具。通过它可以实现交互式撰写规范的 Commit Message
a、安装命令
pnpm add commitizen -D
b、安装适配器
pnpm add cz-conventional-changelog -D
在package文件中,添加以下代码:
{
"scripts": {
"commit": "git-cz"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
c、使用
凡是用到git commit命令,一律改为使用git cz。这时,就会出现选项,用来生成符合格式的 Commit message
d、提交格式
1、type
commit的类型:
// ? 选择要提交的更改类型:(使用箭头键)
? Select the type of change that you're committing: (Use arrow keys)
// ❯ 功能: 新功能
❯ feat: A new feature
// 修复: 错误修复
fix: A bug fix
// 文档: 仅文档更改
docs: Documentation only changes
// 格式: 不影响代码含义的更改(空白、格式、缺少分号等)
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
// 重构: 既不修复错误也不添加功能的代码更改
refactor: A code change that neither fixes a bug nor adds a feature
// 性能: 提高性能的代码更改
perf: A code change that improves performance
// 测试: 添加缺失的测试或更正现有测试
test: Adding missing tests or correcting existing tests
// 构建: 影响构建系统或外部依赖关系的更改(示例范围:gulp、broccoli、npm)
build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
// 持续集成配置: 更改持续集成的配置文件和脚本(示例范围:Travis、Circle、BrowserStack、SauceLabs)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
// 其他: 不修改src或测试文件的其他更改
chore: Other changes that don't modify src or test files
// 还原: 恢复以前的提交
revert: Reverts a previous commit
// (上下移动以显示更多选择)
(Move up and down to reveal more choices)
2、scope
commit影响的范围,比如数据层、控制层、视图层等。根据项目本身情况处理,如: views, components, utils, test...
// ? 此更改的范围是什么(例如组件或文件名):(按enter键跳过)
? What is the scope of this change (e.g. component or file name): (press enter to skip)
3、subject
commit目的的简短描述,不超过 50 个字符
// ? 写一个简短的命令式时态描述(最多72个字符):
? Write a short, imperative tense description of the change (max 80 chars):
(0)
4、body
commit的详细描述
// ? 提供更改的详细说明:(按enter键跳过)
? Provide a longer description of the change: (press enter to skip)
5、footer
部分只用于以下两种情况:不兼容变动、关闭 Issue
// ? 有什么突破性的变化吗?(是/否)
? Are there any breaking changes? (y/N)
注:
如果选择的是y
// ? 描述突破性变化:
? Describe the breaking changes:
// ? 此更改是否影响任何未决问题?(是/否)
? Does this change affect any open issues? (y/N)
注:
如果选择的是y
// ? 添加问题参考(如“修复123号”、“重新123号”):
? Add issue references (e.g. "fix #123", "re #123".):
注:
此时提交会报缺少脚本:“lint”的错误,修改.husky/pre-commit文件,或配置lint脚本(参照Prettier)