提交规范工具:https://github.com/commitizen/cz-cli
git hooks husky:https://github.com/typicode/husky
commitlint 规范提交信息:https://github.com/conventional-changelog/commitlint
prettier格式化代码: https://prettier.io/docs/en/install.html
npm install --save-dev commitizen
npx commitizen init cz-conventional-changelog --save-dev --save-exact
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
"scripts": {
"cm": "cz"
}
git add .
npm run cm //或者 npx cz
值 | 描述 |
---|---|
feat | 新增一个功能 |
fix | 修复一个bug |
docs | 文档变更 |
style | 代码格式(不影响功能) |
refactor | 代码重构 |
perf | 改善性能 |
test | 测试 |
build | 构建或新增依赖 |
ci | 更改持续集成软件的配置文件和package中的scripts命令,例如scopes: Travis, Circle等 |
chore | 变更构建流程或辅助工具 |
revert | 代码回退 |
安装插件:
npm install conventional-changelog-cli --save-dev
"scripts": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
},
npm install --save-dev @commitlint/config-conventional @commitlint/cli
项目根目录下新增配置文件:commitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional']
}
方式一:
npx husky-init && npm install # npm
npx husky-init && yarn # Yarn 1
yarn dlx husky-init --yarn2 && yarn # Yarn 2
方式二(推荐):
npm install husky --save-dev
# or
yarn add husky --dev
# 激活hooks
npx husky install
# or
yarn husky install
添加commit-msg
# 添加 提交信息hook
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
# Sometimes above command doesn't work in some command interpreters
# You can try other commands below to write npx --no -- commitlint --edit $1
# in the commit-msg file.
npx husky add .husky/commit-msg \"npx --no -- commitlint --edit $1\"
# or
npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1"
# or
yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'
添加pre-commit,并配置eslint检查
# 添加 pre-commit hook(提交之前用eslint进行fix)
npx husky add .husky/pre-commit \"npm run lint --fix\"
npm install --save-dev --save-exact prettier
echo {}> .prettierrc.json
{
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"jsxSingleQuote":true,
"bracketSameLine":true,
"jsxBracketSameLine":true,
"trailingComma": "none",
"arrowParens":"avoid"
}
dist
node_modules
.husky
public
安装lint-staged
npm install --save-dev lint-staged
在pre-commit中添加
npx husky add .husky/pre-commit \"npx lint-staged\"
在package.json中添加
{
"lint-staged": {
"*.{vue,js,ts,jsx,tsx}": "prettier --write --ignore-unknown"
}
}
若项目使用eslint,安装:eslint-config-prettier ,并修改eslint配置,解决与prettier的冲突问题
npm install eslint-config-prettier --save-dev
.eslintrc.js
module.exports = {
root: true,
env: {
node: true
},
extends: [
'plugin:vue/vue3-essential',
'@vue/standard',
'@vue/typescript/recommended',
'prettier'//最后添加prettier,处理与eslint的冲突
],
parserOptions: {
ecmaVersion: 2020
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}
vscode 安装插件: ESlint,Prettier - Code formatter,Prettier Eslint
项目根目录添加vscode配置文件 .vscode/setting.json
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
可能遇到的2个问题:
1.在保存文件时,vscode提示报错提示: Couldn't start client ESlint
解决方法:vscode全局配置文件中删除 'eslint.trace.server: null'
参考:https://stackoverflow.com/questions/61165901/couldnt-start-client-eslint-message-in-vs-code
2.保存文件时只有js,和ts文件会自动格式化
解决: 在文件中点击鼠标右键->格式化文档方式->配置默认格式化程序->Prettier - Code formatter