Taiyi's Blog

Husky

February 03, 2023

幫助使用者在commit階段做一些預先檢查,例如要通過測試才能commit, 程式碼排版, lint檢查等等。

安裝

yarn init # 產生package.json
npx husky-init # 產生.husky
yarn install # 安裝husky相關套件

插入shell script

npx husky add .husky/pre-commit 'echo "pre-commit"' 
# 在.husky/pre-commit檔案插入後方指令。也可以直接修改pre-commit檔案

npx husky add .husky/commit-msg 'echo "commit-msg"'
# 如果原本不存在commit-msg, 產生一個新的.husky/commit-msg

在commit時,就會依照不同階段執行pre-commit,commit-msg等動作

git add .
git commit -m "feat: add pre-commit hooks"             
pre-commit
commit-msg
[master af64edc] feat: add pre-commit hooks
 1 file changed, 1 insertion(+), 1 deletion(-)

中斷Commit檢查

exit 1 # 執行到這行會跳出,不能commit

怎麼運作的

  1. husky install

    • .husky/.git/config塞入hooksPath=.husky (git config -e 或是git config --get core.hooksPath查看), 將git hooks和husky做連動
  2. .husky目錄下會有一些檔案例如pre-commit,commit-msg

    • 對應到不同生命週期執行對應的shell檔案, 名稱好像不能自己定?, 要依據.git/hooks裡面給的範例命名, e.g. commit-msg,post-update,prepare-commit-msg,…

Reference


Taiyi 目前正在鉅亨擔任前端打工仔