关于javascript:规范你的commit-msg

12次阅读

共计 2052 个字符,预计需要花费 6 分钟才能阅读完成。

TL;DR:

👉 commitizen 应用

👉 commitlint 应用

👉 能够间接抄走的配置

👉 git cz 的原理

可参考的代码库

标准 commit msg 的意义

规范化、格式化的 commit message 能够让咱们更好的追踪需要的演进、回滚时可能疾速的找到提交、最次也能让咱们的仓库显的更业余。

团队如何标准 commit msg 呢,靠宣讲、靠文档?当然得靠工具生成和束缚。前端圈轮子那么多,这种工具不在话下。

  • commitizen 问答式生成 commit msg 格式化
  • commitlint 校验卡控 commit msg 规范化

commitizen

commitizen: simple commit conventions for internet citizens.

commitizen/cz-cli 借助它提供的 git cz 命令代替 git commit 命令,生成符合规范的 commit message。

commitizen 默认的提交标准是 angular 团队强规定的,若想自定义咱们还需配合 Adapter(适配器)此处咱们用cz-customizable。上面咱们间接来介绍我的项目级配置。

进行 commitizen 配置

执行 npm install -D commitizennpm install -D cz-customizable命令

而后在 package.json 文件中 scripts 和 config 字段进行配置

{
  "scripts": {"commit": "cz"},
  "config": {
    "commitizen": {"path": "./node_modules/cz-customizable"},
    "cz-customizable": {"config": ".cz-configrc.js"}
  }
}

增加 .cz-configrc.js 文件,可参考 cz-demo

在我的项目的根目录下执行 npm run commit试试吧

后续 t npm run commit 替换 git commit

commitlint

如何保障组内成员都应用 npm run commit 命令呢?当然是用工具。

这里咱们用 commitlint,作用和 eslint 相似。利用 git hooks 拦挡不符合规范的 commit msg。

装置依赖

npm install --save-dev @commitlint/{config-conventional,cli}

npm install yorkie --save-dev

增加 .commitlint.config.js 文件

扩大开源配置,而后再增加局部个性化 rules
参考配置

如果你和我一样对 Applicable always|never: never inverts the rule.很蛊惑请浏览链接

配置 git hooks

为了拦挡不标准的 commit msg,须要利用 git hooks 的 commit-msg 主动执行 commitlint

"gitHooks": {"commit-msg": "commitlint -e $GIT_PARAMS"}

乱输出一个 commit msg 试试,发现十分神奇。卡控失效了

依照以上步骤就能够标准你们团队的 commit msg 了。

总结一下:

step 1: 装置依赖

npm install -D commitizen cz-customizable
npm install -D @commitlint/{config-conventional,cli}
npm install -D yorkie 

step 2: 增加配置文件

自定义格局的 commitizen 配置文件 .cz-configrc.js,校验标准的 .commitlint.config.js 文件

step 2: 配置 package.json

{
  "scripts": {"commit": "cz"},
  "config": {
    "commitizen": {"path": "./node_modules/cz-customizable"},
    "cz-customizable": {"config": ".cz-configrc.js"}
  },
  "gitHooks": {"commit-msg": "commitlint -e $GIT_PARAMS"}
}

git cz 的原理

如果你是全局依照的 commitizen,你还能用 git cz命令代替 npm run commit

git cz 是什么玩意?git 的命令,还 commitizen 的命令。2 脸懵逼 ???

通过查阅材料 git cz 是 commitizen 利用 git 的文件命名标准生成的自定义 git 命令。cz-cli/issues

Git 遵循命名约定 git-<subcmd> 主动解析 PATH 中可执行文件中的子命令。这些子命令能够用 git <subcmd> 执行。

咱们看下 commitizen 仓库 package.json 的 bin 字段。真香大白了,就是 git 的自定义命令

 "bin": {
    "cz": "./bin/git-cz",
    "git-cz": "./bin/git-cz",
    "commitizen": "./bin/commitizen"
  },

最初

参考这个 demo 仓库将你的代码库武装起来吧

原文地址

正文完
 0