一、pre-commit预提交格式化代码
- 安装相关插件如下:
npm install husky --save-dev
npm install prettier pretty-quick --save-dev
- 添加脚本执行如下命令(package.json文件中):
"scripts": {
"prepare": "husky install",
},
# 然后执行如下命令:
npm run prepare
- 添加pre-commit钩子
npx husky add .husky/pre-commit "npx pretty-quick --staged"
- 接下来需要添加相关配置项,控制格式化代码的范围
项目根目录下新增如下2个文件:
4.1.prettierignore
not format
.husky
.vscode
dist
node_modules
package-lock.json
yarn.lock
4.2 .prettierrc.json
{
"singleQuote": true,
"semi": false,
"trailingComma": "all",
"arrowParens": "always",
}
=========================== ok ===========================
二、commit-msg提交格式化规范
- 安装相关插件如下:
npm install @commitlint/cli @commitlint/config-conventional --save-dev
- 添加提交钩子校验提交规范
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
- 添加标准化配置文件,执行如下命令
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
提交方式大致如下:
git commit -m 'fix(组件名称): 登录页面bug修复'
git commit -m 'style: 代码格式化'
具体使用请参考本地文件:/node_modules/@commitlint/config-conventional/README.md
三、命令窗式提交,执行如下命令
npm install cz-git commitizen --save-dev
在`package.json`文件中添加如下配置
"config": {
"commitizen": {
"path": "./node_modules/cz-git"
}
}
npx git-cz
或者如下:
"scripts": {
"start": "npm run dev",
"dev": "webpack serve --config ./build/webpack.dev.js --progress",
"build": "webpack --config ./build/webpack.prod.js --progress",
"prepare": "husky install",
"format": "prettier --write .",
"cz": "git-cz"
},
npm run cz 亦可