JavaScript開發(fā)過程中規(guī)范commit?msg意義詳解
規(guī)范 commit msg 的意義
規(guī)范化、格式化的 commit message 可以讓我們更好的追蹤需求的演進(jìn)、回滾時(shí)能夠快速的找到提交、最次也能讓我們的倉庫顯的更專業(yè)。
團(tuán)隊(duì)如何規(guī)范 commit msg呢,靠宣講、靠文檔?當(dāng)然得靠工具生成和約束。前端圈輪子那么多,這種工具不在話下。
- commitizen 問答式生成 commit msg?格式化
- commitlint 校驗(yàn)卡控 commit msg?規(guī)范化
commitizen
commitizen: simple commit conventions for internet citizens.
commitizen/cz-cli 借助它提供的 git cz 命令替代 git commit 命令,生成符合規(guī)范的 commit message。
commitizen 默認(rèn)的提交規(guī)范是 angular 團(tuán)隊(duì)強(qiáng)規(guī)定的,若想自定義我們還需配合 Adapter(適配器)此處我們用cz-customizable
。
下面我們直接來介紹項(xiàng)目級(jí)配置。
進(jìn)行 commitizen 配置
執(zhí)行?npm install -D commitizen、npm install -D cz-customizable命令
然后在 package.json文件中 scripts 和 config 字段進(jìn)行配置
{ "scripts": { "commit": "cz" }, "config": { "commitizen": { "path": "./node_modules/cz-customizable" }, "cz-customizable": { "config": ".cz-configrc.js" } } }
添加 .cz-configrc.js文件
在項(xiàng)目的根目錄下執(zhí)行?npm run commit
試試吧
后續(xù)t?npm run commit
?替換?git commit
commitlint
如何保證組內(nèi)成員都使用npm run commit
命令呢?當(dāng)然是用工具。
這里我們用 commitlint,作用和 eslint 類似。利用 git hooks 攔截不符合規(guī)范的 commit msg。
安裝依賴
npm install --save-dev @commitlint/{config-conventional,cli} npm install yorkie --save-dev
添加 .commitlint.config.js 文件
擴(kuò)展開源配置,然后再添加部分個(gè)性化 rules
配置 git hooks
為了攔截不規(guī)范的 commit msg,需要利用 git hooks 的?commit-msg
?自動(dòng)執(zhí)行 commitlint
"gitHooks": { "commit-msg": "commitlint -e $GIT_PARAMS" }
亂輸入一個(gè) commit msg 試試,發(fā)現(xiàn)非常神奇??厣Я恕?/p>
按照以上步驟就可以規(guī)范你們團(tuán)隊(duì)的 commit msg了。
總結(jié)一下:
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 ,校驗(yàn)規(guī)范的 .commitlint.config.js 文件
配置 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 的命令。
通過查閱資料 git cz 是 commitizen 利用 git 的文件命名規(guī)范生成的自定義 git 命令。
Git遵循命名約定git-<subcmd>
自動(dòng)解析PATH中可執(zhí)行文件中的子命令。 這些子命令可以用git <subcmd>
執(zhí)行。
我們看下 commitizen 倉庫 package.json 的 bin 字段。真香大白了,就是 git 的自定義命令
"bin": { "cz": "./bin/git-cz", "git-cz": "./bin/git-cz", "commitizen": "./bin/commitizen" },
以上就是JavaScript開發(fā)過程中規(guī)范commit msg意義詳解的詳細(xì)內(nèi)容,更多關(guān)于開發(fā)規(guī)范commit msg意義的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
javascript函數(shù)聲明和函數(shù)表達(dá)式區(qū)別分析
本文向大家展示了javascript中函數(shù)聲明和函數(shù)表達(dá)式的概念及區(qū)別,介紹的非常全面,也很詳盡,這里推薦給大家2014-12-12