husky + lint-staged + commitlint + commitizen
一、husky
1.1自动安装
npx husky-init && npm install # npmnpx husky-init && yarn # Yarn 1yarn dlx husky-init --yarn2 && yarn # Yarn 2+pnpm dlx husky-init && pnpm install # pnpm
1.2 手动安装
- 安装husky
npm install husky --save-dev - 启用 Git 挂钩
npx husky install - 要在安装后自动启用 Git 挂钩,请编辑
package.jsonnpm set-script prepare "husky install"package.json会添加一条命令// package.json{"scripts": {"prepare": "husky install"}}
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
二、lint-staged
2.1 安装
2.2 配置lint-staged
{"name": "My project","version": "0.1.0","scripts": {"my-custom-script": "linter --arg1 --arg2","lint-staged:js": "eslint --ext --fix .js,.jsx,.ts,.tsx",},"lint-staged": {"**/*.less": "stylelint --syntax less","**/*.{js,jsx,ts,tsx}": "npm run lint-staged:js","**/*.{js,jsx,tsx,ts,md,json}": ["prettier --write"]},}
2.3 创建hook
npx husky add .husky/pre-commit "npx lint-staged"
三、commitlint
3.1安装
yarn add commitlint @commitlint/config-conventional -D
ornpm i commitlint @commitlint/config-conventional -D
3.2 配置
module.exports = {extends: ['@commitlint/config-conventional'],};
3.3 创建hook
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
四、commitizen
4.1 安装交互式工具(commitizen)
# 全局安装:npm install -g commitizen# 或 本地安装:npm install --save-dev commitizen
4.2 安装适配器(cz-conventional-changelog)
全局安装:npm install -g cz-conventional-changelog
创建 ~/.czrc 文件,写入如何内容{ "path": "cz-conventional-changelog" }
本地安装: npm install --save-dev cz-conventional-changelog
在package.json中添加配置:
"config": {"commitizen": {"path": "cz-conventional-changelog"}}
此时,提交commit过程如下:
git add .git cz 或 cz
4.5 自定义提交规范(1)
npm i -D commitlint-config-cz cz-customizable
变更
commitlint.config.jsmodule.exports = {extends: ['cz']};
新增
.cz-config.js- https://github.com/leoforfree/cz-customizable/blob/master/cz-config-EXAMPLE.js
'use strict'module.exports = {types: [{ value: '✨新增', name: '新增: 新的内容' },{ value: '🐛修复', name: '修复: 修复一个Bug' },{ value: '📝文档', name: '文档: 变更的只有文档' },{ value: '💄格式', name: '格式: 空格, 分号等格式修复' },{ value: '♻️重构', name: '重构: 代码重构,注意和特性、修复区分开' },{ value: '⚡️性能', name: '性能: 提升性能' },{ value: '✅测试', name: '测试: 添加一个测试' },{ value: '🔧工具', name: '工具: 开发工具变动(构建、脚手架工具等)' },{ value: '⏪回滚', name: '回滚: 代码回退' }],scopes: [{ name: 'leetcode' },{ name: 'javascript' },{ name: 'typescript' },{ name: 'Vue' },{ name: 'node' }],// it needs to match the value for field type. Eg.: 'fix'/* scopeOverrides: {fix: [{name: 'merge'},{name: 'style'},{name: 'e2eTest'},{name: 'unitTest'}]}, */// override the messages, defaults are as followsmessages: {type: '选择一种你的提交类型:',scope: '选择一个scope (可选):',// used if allowCustomScopes is truecustomScope: 'Denote the SCOPE of this change:',subject: '短说明:\n',body: '长说明,使用"|"换行(可选):\n',breaking: '非兼容性说明 (可选):\n',footer: '关联关闭的issue,例如:#31, #34(可选):\n',confirmCommit: '确定提交说明?(yes/no)'},allowCustomScopes: true,allowBreakingChanges: ['特性', '修复'],// limit subject lengthsubjectLimit: 100}
- https://github.com/leoforfree/cz-customizable/blob/master/cz-config-EXAMPLE.js
package.json中,将原来commit配置,变更为自定义配置"config": {"commitizen": { // not needed for standlone usage"path": "node_modules/cz-customizable"},}
4.6 自定义提交规范(2)
新建脚本 ```json const chalk = require(‘chalk’) const msgPath = process.env.GIT_PARAMS const msg = require(‘fs’).readFileSync(msgPath, ‘utf-8’).trim() const commitRE = /^(revert: )?(feat|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)((.+))?: .{1,50}/
if (!commitRE.test(msg)) {
console.log()
console.error(
${chalk.bgRed.white(' ERROR ')} ${chalk.red(invalid commit message format.)}\n\n +
chalk.red(Proper commit message format is required for automated changelog generation. Examples:\n\n) +
${chalk.green(feat(compiler): add ‘comments’ option)}\n +
${chalk.green(fix(v-model): handle events on blur (close #28))}\n\n +
chalk.red(See .github/COMMIT_CONVENTION.md for more details.\n) +
chalk.red(You can also use ${chalk.cyan(npm run commit)} to interactively generate a commit message.\n)
)
process.exit(1)
}
- 创建hook`npx husky add .husky/commit-msg node ./scripts/verify-commit-msg.js`<a name="Dd6aw"></a>## 五、conventional-changelog<a name="DRMiv"></a>### 5.1 conventional-changelog-cli- 安装 `npm install -g conventional-changelog-cli`- 使用 `conventional-changelog -p angular -i CHANGELOG.md -s`<br />该命令会在CHANGELOG.md文件头部添加上次tag之后的commit信息<a name="NmKhW"></a>### 5.2 [standard-version](https://github.com/conventional-changelog/standard-version)- 安装 `npm i standard-version -D`- Add an [npm runscript](https://docs.npmjs.com/cli/run-script) to your `package.json`:```json{"scripts": {"release": "standard-version"}}
- 首次发布
