代碼檢查工作流以及commit規範化

最近再看Vue技術內幕,就把Vue項目fork了一份,然後邊看文章邊在代碼里加上註釋。看了幾節內容,做了一些筆記,準備提交一次,然後就直接:

git add .
git commit -m '添加代碼註釋'
……

然後就彈出這麼個內容:

   ERROR  invalid commit message format.

  Proper commit message format is required for automated changelog generation. Examples:

    feat(compiler): add 'comments' option
    fix(v-model): handle events on blur (close #28)

  See .github/COMMIT_CONVENTION.md for more details.
  You can also use npm run commit to interactively generate a commit message.


commit-msg hook failed (add --no-verify to bypass)

怎麼提交還會報錯?這看上去不像衝突啥的啊。npm run commit 又是啥?
鍵入npm run commit


[email protected], [email protected]


Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.

? 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, format
ting, 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 
(Move up and down to reveal more choices)

選擇這次提交的類型?類型有很多種,除了這些還可以用方向鍵查看更多的:

  build:    Changes that affect the build system or external dependencies (example 
scopes: gulp, broccoli, npm) 
  ci:       Changes to our CI configuration files and scripts (example scopes: Trav
is, Circle, BrowserStack, SauceLabs) 
  chore:    Other changes that don't modify src or test files 
  revert:   Reverts a previous commit 

選擇對應的類型可以清楚明瞭的說明本次提交的目的。
接下來還會問一些問題:

? What is the scope of this change (e.g. component or file name)? (press enter to s
kip)
 
? Write a short, imperative tense description of the change:
 添加筆記以及註釋
? Provide a longer description of the change: (press enter to skip)
 
? Are there any breaking changes? No
? Does this change affect any open issues? No

按着這個一步一步來,一個規範的的commit就出來了。
嗨呀,真的好啊,還能強制性的規範commit,所有項目上都弄這麼個東西,這麼一來所有項目的commit都會變得清晰明瞭。
這個命令到底執行了啥呢?跑去package.json文件中去看,有這麼一句"commit": "git-cz"git-cz又是啥呢?接着又去google。


第一條點進去,是一個插件,叫commitizen
切到自己的項目,npm安裝,package.jsonscripts添加指令,再在package.jsonconfig裏添加:

  "config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
  }

然後隨便改點東西,先用

git add .
git commit -m 'x'

測試下。哈?怎麼直接提交了?怎麼沒報錯?這東西沒用?npm run commit,這玩意兒有用呀,爲啥直接提交不規範commit還能成功?
再看看vue的package.json怎麼寫的。

  "gitHooks": {
    "pre-commit": "lint-staged",
    "commit-msg": "node scripts/verify-commit-msg.js"
  },
  "lint-staged": {
    "*.js": [
      "eslint --fix",
      "git add"
    ]
  },

gitHooks:顧名思義,git相關的鉤子,可以在git的各個時間做不同的處理。實際上想這麼用得裝個尤大寫的yorkie,實際上就是改了一丟丟的husky

  • commit之前,執行lint-staged
  • 對commit的說明進行判定是否是符合規範的commit

lint-staged :對代碼進行靜態分析,試圖找出潛在的問題。使用 lint-staged只會對每次提交文件進行檢查。

  • 對所有提交的js文件進行eslint修復,修復後重新添加該文件到暫存區

加上這兩句之後,直接提交不規範的commit就會報錯啦,而且還會對提交的js文件進行eslint驗證,以及嘗試修復eslint。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章