Are you an LLM? You can read better optimized documentation at /notes/biome.md for this page in Markdown format
为项目添加 Biome 及规范提交信息工具
1. 配置 Biome 格式化代码
安装 Biome
shell
pnpm add --save-dev @biomejs/biomeshell
npm install --save-dev @biomejs/biomeshell
yarn add --dev @biomejs/biomeBiome 配置
新建 biome.json 配置如下
biome.json
json
{
"$schema": "https://biomejs.dev/schemas/2.5.3/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignoreUnknown": true,
"includes": ["**", "!node_modules", "!.vitepress/cache", "!.vitepress/dist", "!dist", "!cache", "!temp", "!.temp"]
},
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 120
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": "on"
}
}
},
"linter": {
"enabled": false
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"semicolons": "asNeeded",
"trailingCommas": "none"
}
},
"json": {
"formatter": {
"trailingCommas": "none"
}
},
"css": {
"formatter": {
"quoteStyle": "single"
}
}
}使用 Biome 格式化所有文件
shell
pnpm exec biome check --write .shell
npx @biomejs/biome check --write .shell
yarn biome check --write .配置 commit 自动格式化
安装 simple-git-hooks 和 lint-staged 插件
zsh
pnpm install simple-git-hooks lint-staged配置 package.json 示例
json
{
"lint-staged": {
"*": "biome check --write --no-errors-on-unmatched"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
}
}2. 安装 cz-vinyl 和 commitizen 并配置
shell
pnpm add --save-dev cz-vinyl commitizen
# 或者本地全局安装
pnpm add -g cz-vinyl commitizenshell
npm install --save-dev cz-vinyl commitizen
# 或者本地全局安装
npm install -g cz-vinyl commitizenshell
yarn add --dev cz-vinyl commitizen
# 或者本地全局安装
yarn global add cz-vinyl commitizen查看本地全局安装配置
- 在用户主目录创建
.czrc配置文件,指定适配器路径
sh
echo '{ "path": "cz-vinyl" }' > ~/.czrc- 给
cz命令设置别名,指向git-cz(cz-vinyl 的执行命令):
sh
echo 'alias cz="git-cz"' >> ~/.zshrc
source ~/.zshrc这样你可以直接通过 cz 命令启动交互式提交
配置 package.json 示例
json
{
"config": {
"commitizen": {
"path": "cz-vinyl"
}
},
"scripts": {
"cz": "git-cz"
}
}cz-vinyl 选项配置示例
czvinyl.config.ts
ts
import type { CzVinylConfig } from 'cz-vinyl'
const config: CzVinylConfig = {
headerFormat: '{type}{scope}: {subject}',
bodyFormat: '{body}',
commitTypes: [
{ value: 'feat', description: '新增功能' },
{ value: 'fix', description: '修复缺陷' },
{ value: 'docs', description: '文档更新' },
{ value: 'style', description: '代码格式(不影响功能,例如空格、分号等)' },
{
value: 'refactor',
description: '重构代码(既不是新增功能,也不是修复 bug)'
},
{ value: 'perf', description: '性能优化' },
{ value: 'test', description: '增加测试' },
{ value: 'chore', description: '构建过程或辅助工具变动' }
],
maxCommitLineWidth: 72,
typeQuestion: '请选择提交类型:',
scopeQuestion: '请选择修改范围(可选):',
skipScope: false,
scopes: ['', 'types', 'hooks', 'utils', 'components', 'views', 'store'],
ticketIdQuestion: '请输入关联的任务号(可选):',
skipTicketId: true,
subjectQuestion: '请简要描述提交内容(必填):',
subjectMaxLength: 50,
subjectMinLength: 5,
bodyQuestion: '请输入详细描述(可选):',
skipBody: true,
skipBreakingChanges: true,
issuesQuestion: '请输入要关闭的issue(可选,例如:#1):',
skipIssues: false,
openAiToken: null
}
export default config3. 安装及配置 commitlint 校验工具
安装 commitlint 及 Husky 集成
shell
pnpm add --save-dev @commitlint/cli @commitlint/config-conventional @commitlint/types husky
pnpm husky install
# 在package.json中配置脚本
npm pkg set scripts.commitlint="commitlint --edit"
echo "pnpm commitlint \${1}" > .husky/commit-msgshell
npm install --save-dev @commitlint/cli @commitlint/config-conventional @commitlint/types husky
npx husky install
# 在package.json中配置脚本
npm pkg set scripts.commitlint="commitlint --edit"
echo "npm run commitlint \${1}" > .husky/commit-msgshell
yarn add --dev @commitlint/cli @commitlint/config-conventional @commitlint/types husky
yarn husky install
# 在package.json中配置脚本
npm pkg set scripts.commitlint="commitlint --edit"
echo "yarn commitlint \${1}" > .husky/commit-msgcommitlint 配置示例
新建 commitlint.config.ts 文件
ts
import type { UserConfig } from '@commitlint/types'
const config: UserConfig = {
extends: ['@commitlint/config-conventional']
}
export default configjs
export default { extends: ['@commitlint/config-conventional'] }CI 校验提交信息
GitHub Actions 示例,自动检查提交信息规范
.github/workflows/commitlint.yml
yml
name: CI
on: [push, pull_request]
jobs:
commitlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install required dependencies
run: |
sudo apt update
sudo apt install -y sudo
sudo apt install -y git curl
curl -sL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo DEBIAN_FRONTEND=noninteractive apt install -y nodejs
- name: Print versions
run: |
git --version
node --version
npm --version
npx commitlint --version
- name: Install commitlint
run: |
npm install conventional-changelog-conventionalcommits
npm install commitlint@latest
- name: Validate current commit (last commit) with commitlint
if: github.event_name == 'push'
run: npx commitlint --last --verbose
- name: Validate PR commits with commitlint
if: github.event_name == 'pull_request'
run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose