赞
踩
客户端hooks不随代码提交
#+BEGIN_SRC bash :noeval
git config core.hooksPath hooks
#_END_SRC
键入提交信息前运行,通常用来检查代码防干扰,类似lint功能,行位空格等
npx eslint ./src
注: 如果是在.git/hooks下创建一个文件 pre-commit是一样的效果。只是此方式文件在.git下不会被推送到远程
git config core.hooksPath .mygithooks
# 提交前将代码改成错误的lint进行提交测试
git status
git add .
git commit -m "测试lint"
注:若git commit的时候报错如下
原因:没有执行权限,需要给文件添加写的权限chmod +x .mygithooks/pre-commit
- 1
用来在提交通过前验证项目状态或提交信息
#!/bin/sh # 用 `` 可以将命令的输出结果复制给变量 # 获取当前提交的 commit msg commit_msg=`cat $1` # 获取用户 email email=`git config user.email` msg_re="^(feat|fix|docs|style|refactor|perf|test|workflow|build|ci|chore|release|workflow)(\(.+\))?: .{1,10}" if [[ ! $commit_msg =~ $msg_re]] then echo "\n不合法的 commit 消息提交格式,请使用正确的格式:\ \nfeat: add comments\ \nfix: handle events on blur (close #28)\ \n 详情请查看 git commit 提交规范" # 异常退出 exit 1 fi
yarn add husky -D
# 执行一次, 会在根目录生成一个.husky的文件夹
yarn husky install
为了以后能在npm install之后自动地启用git hooks,在package.json添加script
"scripts": {
// prepare脚本会在npm install之后自动执行,它是npm的一个生命周期脚本
"prepare": "husky install"
}
npm eslint --ext .js,.vue src
指令来去进行相关eslint检测# 注意: window上需要把 npx husky 换成 .\node_modules\.bin\husky
# 即:.\node_modules\.bin\husky add .husky/pre-commit "npx eslint --ext .js,.vue src"
npx husky add .husky/pre-commit "npx eslint --ext .js,.vue src"
yarn install lint-staged -dev
"lint-staged": {
"src/**/*.{js,vue}": [
"eslint --fix",
"git add"
]
}
...
# npx eslint --ext .js,.vue src
npx lint-staged
# 新版不需要额外安装cz-conventional-changelog,安装commitizen时会自动安装在node_modules里面
yarn add commitizen --dev
"config":{
"commitizen":{
"path":"node_modules/cz-conventional-changelog"
}
}
此时可以用 yarn cz
代替 git commit
提交
yarn add cz-customizable -D
{
"config": {
"commitizen":{
"path": "node_modules/cz-customizable" // 改动处
}
}
}
module.exports = { // 可选类型 types: [ {value: "feat", name: "feat: 新功能"}, {value: "fix", name: "fix: 修复"}, {value: "docs", name: "docs: 文档变更"}, {value: "style", name: "style: 代码格式(不影响代码运行的变动)"}, {value: "refactor", name: "refactor: 重构(既不是增加feature,也不是修复bug)"}, {value: "perf", name: "perf: 性能优化"}, {value: "test", name: "test: 增加测试"}, {value: "chore", name: "chore: 构建过程或辅助工具的变动"}, {value: "revert", name: "revert: 回退"}, {value: "build", name: "build: 打包"}, ], // 消息步骤 messages: { type: "请选择提交类型:", customScope: "请输入修改范围(可选)", subject: "请简要描述提交(必填):", body: "请输入详细描述(可选)", footer: "请输入要关闭的issue(可选)", confirmCommit: "确认使用以上信息提交?(y/n)" }, // 跳过问题 skipQuestions: ["body","footer"], // subject文字长度默认是72 subjectLimit: 72 }
yarn cz
提交是没有问题的,但是如果用git commit
提交就可以绕过规则,所以要拦截git commit
yarn add @commitlint/cli @commitlint/config-conventional -D
module.exports = { extends: ["@commitlint/config-conventional"], // 定义规则类型 rules: { // type 类型定义,表示 git 提交的 type 必须在以下类型范围内 'type-enum': [ // 当前验证的错误级别,2代表错误级别的错误 2, // 在什么情况下进行验证,always表示任何情况下都进行验证 'always', // 泛型内容, 对应cz-config定义的types [ 'feat', // 新功能 feature 'fix', // 修复 bug 'docs', // 文档注释 'style', // 代码格式(不影响代码运行的变动) 'refactor', // 重构(既不增加新功能,也不是修复bug) 'perf', // 性能优化 'test', // 增加测试 'chore', // 构建过程或辅助工具的变动 'revert', // 回退 'build' // 打包 ] ], // subject 大小写不做校验 'subject-case': [0] } };
此时可以 使用 yarn cz
按照提示步骤来 或者 用 git commit
按照标准写注释才能提交
yarn add standard-version -D
"scripts": {
"release": "standard-version"
}
# 生成changelog和打tag
npm run release
# 将tag push上去
git tag
git tag -m "info"
git push --tags
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。