項目規范管理
目的
為了使團隊多人協作更加的規范,所以需要每次在 git 提交的時候,做一次硬性規范提交,規范 git 的提交信息
使用commitizen
規范git提交(交互式提交 + 自定義提示文案 + Commit規范)
- 安裝依賴
pnpm install -D commitizen cz-conventional-changelog @commitlint/config-conventional @commitlint/cli commitlint-config-cz cz-customizable
- 配置package.json
{"scripts": {"commit:comment": "引導設置規范化的提交信息","commit": "git-cz"},"config": {"commitizen": {"path": "node_modules/cz-customizable"},"cz-customizable": {// 若項目配置了type: "module", 就需要修改配置文件的后綴名為cjs, 并添加這個配置"config": ".cz-config.cjs"}}
}
- 新增配置文件
commitlint.config.js
module.exports = {extends: ['@commitlint/config-conventional', 'cz'],rules: {'type-enum': [2,'always',['feature', // 新功能(feature)'bug', // 此項特別針對bug號,用于向測試反饋bug列表的bug修改情況'fix', // 修補bug'ui', // 更新 ui'docs', // 文檔(documentation)'style', // 格式(不影響代碼運行的變動)'perf', // 性能優化'release', // 發布'deploy', // 部署'refactor', // 重構(即不是新增功能,也不是修改bug的代碼變動)'test', // 增加測試'chore', // 構建過程或輔助工具的變動'revert', // feat(pencil): add ‘graphiteWidth’ option (撤銷之前的commit)'merge', // 合并分支, 例如: merge(前端頁面): feature-xxxx修改線程地址'build', // 打包],],// <type> 格式 小寫'type-case': [2, 'always', 'lower-case'],// <type> 不能為空'type-empty': [2, 'never'],// <scope> 范圍不能為空'scope-empty': [2, 'never'],// <scope> 范圍格式'scope-case': [0],// <scope> 枚舉范圍'scope-enum': [1, 'always'],// <subject> 主要 message 不能為空'subject-empty': [2, 'never'],// <subject> 以什么為結束標志,禁用'subject-full-stop': [0, 'never'],// <subject> 格式,禁用'subject-case': [0, 'never'],// <body> 以空行開頭'body-leading-blank': [1, 'always'],'header-max-length': [0, 'always', 72],},
}
- 添加自定義提示
.cz-config.cjs
module.exports = {types: [{ value: 'feature', name: '? Features | 增加新功能' },{ value: 'bug', name: '🐛 Bug Fixes | 測試反饋bug列表中的bug號' },{ value: 'fix', name: '🐛 Bug Fixes | 修復bug' },{ value: 'ui', name: '💄 UI| 更新UI' },{ value: 'docs', name: '📝 Documentation |文檔變更' },{ value: 'style', name: '💄 Styles | 風格(不影響代碼運行的變動)' },{ value: 'perf', name: '?Performance Improvements | 性能優化' },{ value: 'refactor', name: '? Code Refactoring |重構(既不是增加feature,也不是修復bug)' },{ value: 'release', name: 'release: 發布' },{ value: 'deploy', name: '🚀 Chore |部署' },{ value: 'test', name: '? Tests |增加測試' },{ value: 'chore', name: '🚀 Chore |構建過程或輔助工具的變動(更改配置文件)' },{ value: 'revert', name: '? Revert | 回退回退' },{ value: 'build', name: '📦? Build System |打包' },],// override the messages, defaults are as followsmessages: {type: '請選擇提交類型:',customScope: '請輸入您修改的范圍(可選):',subject: '請簡要描述提交 message (必填):',body: '請輸入詳細描述(可選,待優化去除,跳過即可):',footer: '請輸入要關閉的issue(待優化去除,跳過即可):',confirmCommit: '確認使用以上信息提交?(y/n/e/h)',},// 要是同一個git下有多個項目文件家, 可以打開注釋選擇git要操作的項目// scopes: [{ name: 'h5' }, { name: 'manage'}],allowCustomScopes: true,skipQuestions: ['body', 'footer'],subjectLimit: 72, }
使用prettier
格式化代碼
- 安裝
pnpm add --save-dev --save-exact prettier
- 創建
.prettierrc
文件,并添加如下配置, 具體配置可以查看官網
{"semi": false,"tabWidth": 2,"useTabs": false,"singleQuote": true,"printWidth": 150
}
使用husky
及lint-staged
再提交代碼時格式化代碼
- 安裝(注意:這里與
prettier官網
的給出的示例不太一致, 建議看husky官網進行配置)pnpm add --save-dev husky lint-stagedpnpm exec husky initnpm pkg set scripts.prepare="husky"
- 在
package.json
添加如下配置
{"lint-staged": {"**/*": "prettier --write --ignore-unknown"}
}
- 在
.husky
新建pre-commit
文件,并添加如下配置
echo "🚀 pre-commit"echo "npx --no-install lint-staged"npx --no-install lint-staged
- 在
.husky
新建commit-msg
文件,并添加如下配置
echo "🚀 commit-msg"echo "npx --no-install commitlint --edit \"$1\""npx --no-install commitlint --edit "$1"
完結
在git add
后執行pnpm run commit
命令根據提示輸入commit信息即可。