Skip to content

Commit 13565c6

Browse files
committed
feat(project): migrate from husky and lint-staged to lefthook
1 parent 17a18fc commit 13565c6

File tree

17 files changed

+307
-116
lines changed

17 files changed

+307
-116
lines changed

.husky/commit-msg

-6
This file was deleted.

.husky/post-merge

-3
This file was deleted.

.husky/pre-commit

-7
This file was deleted.

.lintstagedrc.mjs

-20
This file was deleted.

.npmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
registry = "https://registry.npmmirror.com"
2-
public-hoist-pattern[]=husky
2+
public-hoist-pattern[]=lefthook
33
public-hoist-pattern[]=eslint
44
public-hoist-pattern[]=prettier
55
public-hoist-pattern[]=prettier-plugin-tailwindcss

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@
219219
"*.env": "$(capture).env.*",
220220
"README.md": "README*,CHANGELOG*,LICENSE,CNAME",
221221
"package.json": "pnpm-lock.yaml,pnpm-workspace.yaml,.gitattributes,.gitignore,.gitpod.yml,.npmrc,.browserslistrc,.node-version,.git*,.tazerc.json",
222-
"eslint.config.mjs": ".eslintignore,.prettierignore,.stylelintignore,.commitlintrc.*,.prettierrc.*,stylelint.config.*,.lintstagedrc.mjs,cspell.json",
222+
"eslint.config.mjs": ".eslintignore,.prettierignore,.stylelintignore,.commitlintrc.*,.prettierrc.*,stylelint.config.*,.lintstagedrc.mjs,cspell.json,lefthook.yml",
223223
"tailwind.config.mjs": "postcss.*"
224224
},
225225
"commentTranslate.hover.enabled": false,

docs/src/en/guide/essentials/development.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ The execution command is: `pnpm run [script]` or `npm run [script]`.
9898
"postinstall": "pnpm -r run stub --if-present",
9999
// Only allow using pnpm
100100
"preinstall": "npx only-allow pnpm",
101-
// Install husky
102-
"prepare": "is-ci || husky",
101+
// Install lefthook
102+
"prepare": "is-ci || lefthook install",
103103
// Preview the application
104104
"preview": "turbo-run preview",
105105
// Package specification check

docs/src/en/guide/other/faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ If you encounter a problem, you can start looking from the following aspects:
1818

1919
## Dependency Issues
2020

21-
In a `Monorepo` project, it is necessary to develop the habit of executing `pnpm install` every time you `git pull` the code, as new dependency packages are often added. The project has already configured automatic execution of `pnpm install` in `.husky/git-merge`, but sometimes there might be issues. If it does not execute automatically, it is recommended to execute it manually once.
21+
In a `Monorepo` project, it's important to get into the habit of running `pnpm install` after every `git pull` because new dependencies are often added. The project has configured automatic execution of `pnpm install` in `lefthook.yml`, but sometimes there might be issues. If it does not execute automatically, it is recommended to execute it manually once.
2222

2323
## About Cache Update Issues
2424

docs/src/en/guide/project/standard.md

+57-9
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ The project integrates the following code verification tools:
3333
- [Prettier](https://prettier.io/) for code formatting
3434
- [Commitlint](https://commitlint.js.org/) for checking the standard of git commit messages
3535
- [Publint](https://publint.dev/) for checking the standard of npm packages
36-
- [Lint Staged](https://github.com/lint-staged/lint-staged) for running code verification before git commits
3736
- [Cspell](https://cspell.org/) for checking spelling errors
37+
- [lefthook](https://github.com/evilmartians/lefthook) for managing Git hooks, automatically running code checks and formatting before commits
3838

3939
## ESLint
4040

@@ -148,18 +148,66 @@ The cspell configuration file is `cspell.json`, which can be modified according
148148

149149
Git hooks are generally combined with various lints to check code style during git commits. If the check fails, the commit will not proceed. Developers need to modify and resubmit.
150150

151-
### husky
151+
### lefthook
152152

153-
One issue is that the check will verify all code, but we only want to check the code we are committing. This is where husky comes in.
153+
One issue is that the check will verify all code, but we only want to check the code we are committing. This is where lefthook comes in.
154154

155-
The most effective solution is to perform Lint checks locally before committing. A common practice is to use husky or pre-commit to perform a Lint check before local submission.
155+
The most effective solution is to perform Lint checks locally before committing. A common practice is to use lefthook to perform a Lint check before local submission.
156156

157-
The project defines corresponding hooks inside `.husky`.
157+
The project defines corresponding hooks inside `lefthook.yml`:
158158

159-
#### How to Disable Husky
159+
- `pre-commit`: Runs before commit, used for code formatting and checking
160160

161-
If you want to disable Husky, simply delete the .husky directory.
161+
- `code-workspace`: Updates VSCode workspace configuration
162+
- `lint-md`: Formats Markdown files
163+
- `lint-vue`: Formats and checks Vue files
164+
- `lint-js`: Formats and checks JavaScript/TypeScript files
165+
- `lint-style`: Formats and checks style files
166+
- `lint-package`: Formats package.json
167+
- `lint-json`: Formats other JSON files
162168

163-
### lint-staged
169+
- `post-merge`: Runs after merge, used for automatic dependency installation
164170

165-
Used for automatically fixing style issues of committed files. Its configuration file is `.lintstagedrc.mjs`, which can be modified according to project needs.
171+
- `install`: Runs `pnpm install` to install new dependencies
172+
173+
- `commit-msg`: Runs during commit, used for checking commit message format
174+
- `commitlint`: Uses commitlint to check commit messages
175+
176+
#### How to Disable lefthook
177+
178+
If you want to disable lefthook, there are two ways:
179+
180+
::: code-group
181+
182+
```bash [Temporary disable]
183+
git commit -m 'feat: add home page' --no-verify
184+
```
185+
186+
```bash [Permanent disable]
187+
# Simply delete the lefthook.yml file
188+
rm lefthook.yml
189+
```
190+
191+
:::
192+
193+
#### How to Modify lefthook Configuration
194+
195+
If you want to modify lefthook's configuration, you can edit the `lefthook.yml` file. For example:
196+
197+
```yaml
198+
pre-commit:
199+
parallel: true # Execute tasks in parallel
200+
jobs:
201+
- name: lint-js
202+
run: pnpm prettier --cache --ignore-unknown --write {staged_files}
203+
glob: '*.{js,jsx,ts,tsx}'
204+
```
205+
206+
Where:
207+
208+
- `parallel`: Whether to execute tasks in parallel
209+
- `jobs`: Defines the list of tasks to execute
210+
- `name`: Task name
211+
- `run`: Command to execute
212+
- `glob`: File pattern to match
213+
- `{staged_files}`: Represents the list of staged files

docs/src/guide/essentials/development.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ npm 脚本是项目常见的配置,用于执行一些常见的任务,比如
9898
"postinstall": "pnpm -r run stub --if-present",
9999
// 只允许使用pnpm
100100
"preinstall": "npx only-allow pnpm",
101-
// husky的安装
102-
"prepare": "is-ci || husky",
101+
// lefthook的安装
102+
"prepare": "is-ci || lefthook install",
103103
// 预览应用
104104
"preview": "turbo-run preview",
105105
// 包规范检查

docs/src/guide/other/faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
## 依赖问题
2020

21-
`Monorepo` 项目下,需要养成每次 `git pull`代码都要执行`pnpm install`的习惯,因为经常会有新的依赖包加入,项目在`.husky/git-merge`已经配置了自动执行`pnpm install`,但是有时候会出现问题,如果没有自动执行,建议手动执行一次。
21+
`Monorepo` 项目下,需要养成每次 `git pull`代码都要执行`pnpm install`的习惯,因为经常会有新的依赖包加入,项目在`lefthook.yml`已经配置了自动执行`pnpm install`,但是有时候会出现问题,如果没有自动执行,建议手动执行一次。
2222

2323
## 关于缓存更新问题
2424

docs/src/guide/project/standard.md

+57-9
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
- [Prettier](https://prettier.io/) 用于代码格式化
3434
- [Commitlint](https://commitlint.js.org/) 用于检查 git 提交信息的规范
3535
- [Publint](https://publint.dev/) 用于检查 npm 包的规范
36-
- [Lint Staged](https://github.com/lint-staged/lint-staged) 用于在 git 提交前运行代码校验
3736
- [Cspell](https://cspell.org/) 用于检查拼写错误
37+
- [lefthook](https://github.com/evilmartians/lefthook) 用于管理 Git hooks,在提交前自动运行代码校验和格式化
3838

3939
## ESLint
4040

@@ -148,18 +148,66 @@ cspell 配置文件为 `cspell.json`,可以根据项目需求进行修改。
148148

149149
git hook 一般结合各种 lint,在 git 提交代码的时候进行代码风格校验,如果校验没通过,则不会进行提交。需要开发者自行修改后再次进行提交
150150

151-
### husky
151+
### lefthook
152152

153-
有一个问题就是校验会校验全部代码,但是我们只想校验我们自己提交的代码,这个时候就可以使用 husky
153+
有一个问题就是校验会校验全部代码,但是我们只想校验我们自己提交的代码,这个时候就可以使用 lefthook
154154

155-
最有效的解决方案就是将 Lint 校验放到本地,常见做法是使用 husky 或者 pre-commit 在本地提交之前先做一次 Lint 校验。
155+
最有效的解决方案就是将 Lint 校验放到本地,常见做法是使用 lefthook 在本地提交之前先做一次 Lint 校验。
156156

157-
项目在 `.husky` 内部定义了相应的 hooks
157+
项目在 `lefthook.yml` 内部定义了相应的 hooks
158158

159-
#### 如何关闭 Husky
159+
- `pre-commit`: 在提交前运行,用于代码格式化和检查
160160

161-
如果你想关闭 Husky,直接删除 `.husky` 目录即可。
161+
- `code-workspace`: 更新 VSCode 工作区配置
162+
- `lint-md`: 格式化 Markdown 文件
163+
- `lint-vue`: 格式化并检查 Vue 文件
164+
- `lint-js`: 格式化并检查 JavaScript/TypeScript 文件
165+
- `lint-style`: 格式化并检查样式文件
166+
- `lint-package`: 格式化 package.json
167+
- `lint-json`: 格式化其他 JSON 文件
162168

163-
### lint-staged
169+
- `post-merge`: 在合并后运行,用于自动安装依赖
164170

165-
用于自动修复提交文件风格问题,其配置文件为 `.lintstagedrc.mjs`,可以根据项目需求进行修改。
171+
- `install`: 运行 `pnpm install` 安装新依赖
172+
173+
- `commit-msg`: 在提交时运行,用于检查提交信息格式
174+
- `commitlint`: 使用 commitlint 检查提交信息
175+
176+
#### 如何关闭 lefthook
177+
178+
如果你想关闭 lefthook,有两种方式:
179+
180+
::: code-group
181+
182+
```bash [临时关闭]
183+
git commit -m 'feat: add home page' --no-verify
184+
```
185+
186+
```bash [永久关闭]
187+
# 删除 lefthook.yml 文件即可
188+
rm lefthook.yml
189+
```
190+
191+
:::
192+
193+
#### 如何修改 lefthook 配置
194+
195+
如果你想修改 lefthook 的配置,可以编辑 `lefthook.yml` 文件。例如:
196+
197+
```yaml
198+
pre-commit:
199+
parallel: true # 并行执行任务
200+
jobs:
201+
- name: lint-js
202+
run: pnpm prettier --cache --ignore-unknown --write {staged_files}
203+
glob: '*.{js,jsx,ts,tsx}'
204+
```
205+
206+
其中:
207+
208+
- `parallel`: 是否并行执行任务
209+
- `jobs`: 定义要执行的任务列表
210+
- `name`: 任务名称
211+
- `run`: 要执行的命令
212+
- `glob`: 匹配的文件模式
213+
- `{staged_files}`: 表示暂存的文件列表

lefthook.yml

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# EXAMPLE USAGE:
2+
#
3+
# Refer for explanation to following link:
4+
# https://lefthook.dev/configuration/
5+
#
6+
# pre-push:
7+
# jobs:
8+
# - name: packages audit
9+
# tags:
10+
# - frontend
11+
# - security
12+
# run: yarn audit
13+
#
14+
# - name: gems audit
15+
# tags:
16+
# - backend
17+
# - security
18+
# run: bundle audit
19+
#
20+
# pre-commit:
21+
# parallel: true
22+
# jobs:
23+
# - run: yarn eslint {staged_files}
24+
# glob: "*.{js,ts,jsx,tsx}"
25+
#
26+
# - name: rubocop
27+
# glob: "*.rb"
28+
# exclude:
29+
# - config/application.rb
30+
# - config/routes.rb
31+
# run: bundle exec rubocop --force-exclusion {all_files}
32+
#
33+
# - name: govet
34+
# files: git ls-files -m
35+
# glob: "*.go"
36+
# run: go vet {files}
37+
#
38+
# - script: "hello.js"
39+
# runner: node
40+
#
41+
# - script: "hello.go"
42+
# runner: go run
43+
44+
pre-commit:
45+
parallel: true
46+
commands:
47+
code-workspace:
48+
run: pnpm vsh code-workspace --auto-commit
49+
lint-md:
50+
run: pnpm prettier --cache --ignore-unknown --write {staged_files}
51+
glob: '*.md'
52+
lint-vue:
53+
run: pnpm prettier --write {staged_files} && pnpm eslint --cache --fix {staged_files} && pnpm stylelint --fix --allow-empty-input {staged_files}
54+
glob: '*.vue'
55+
lint-js:
56+
run: pnpm prettier --cache --ignore-unknown --write {staged_files} && pnpm eslint --cache --fix {staged_files}
57+
glob: '*.{js,jsx,ts,tsx}'
58+
lint-style:
59+
run: pnpm prettier --cache --ignore-unknown --write {staged_files} && pnpm stylelint --fix --allow-empty-input {staged_files}
60+
glob: '*.{scss,less,styl,html,vue,css}'
61+
lint-package:
62+
run: pnpm prettier --cache --write {staged_files}
63+
glob: 'package.json'
64+
lint-json:
65+
run: pnpm prettier --cache --write --parser json {staged_files}
66+
glob: '{!(package)*.json,*.code-snippets,.!(browserslist)*rc}'
67+
68+
post-merge:
69+
commands:
70+
install:
71+
run: pnpm install
72+
73+
commit-msg:
74+
commands:
75+
commitlint:
76+
run: pnpm exec commitlint --edit $1

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
"lint": "vsh lint",
5252
"postinstall": "pnpm -r run stub --if-present",
5353
"preinstall": "npx only-allow pnpm",
54-
"prepare": "is-ci || husky",
54+
"prepare": "is-ci || lefthook install",
5555
"preview": "turbo-run preview",
5656
"publint": "vsh publint",
5757
"reinstall": "pnpm clean --del-lock && pnpm install",
5858
"test:unit": "vitest run --dom",
5959
"test:e2e": "turbo run test:e2e",
6060
"update:deps": "npx taze -r -w",
61-
"version": "pnpm exec changeset version && pnpm install --no-frozen-lockfile"
61+
"version": "pnpm exec changeset version && pnpm install --no-frozen-lockfile",
62+
"catalog": "pnpx codemod pnpm/catalog"
6263
},
6364
"devDependencies": {
6465
"@changesets/changelog-github": "catalog:",
@@ -81,9 +82,8 @@
8182
"cross-env": "catalog:",
8283
"cspell": "catalog:",
8384
"happy-dom": "catalog:",
84-
"husky": "catalog:",
8585
"is-ci": "catalog:",
86-
"lint-staged": "catalog:",
86+
"lefthook": "catalog:",
8787
"playwright": "catalog:",
8888
"rimraf": "catalog:",
8989
"tailwindcss": "catalog:",

0 commit comments

Comments
 (0)