Skip to content

Commit 10cf3dd

Browse files
committed
build(deps-dev): Migrate husky and lint-staged to lefthook (#2988)
1 parent 6571c11 commit 10cf3dd

File tree

8 files changed

+536
-233
lines changed

8 files changed

+536
-233
lines changed

.husky/_/pre-commit

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/sh
2+
3+
if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then
4+
set -x
5+
fi
6+
7+
if [ "$LEFTHOOK" = "0" ]; then
8+
exit 0
9+
fi
10+
11+
call_lefthook()
12+
{
13+
if test -n "$LEFTHOOK_BIN"
14+
then
15+
"$LEFTHOOK_BIN" "$@"
16+
elif lefthook -h >/dev/null 2>&1
17+
then
18+
lefthook "$@"
19+
elif /usr/src/app/node_modules/.pnpm/[email protected]/node_modules/lefthook-linux-arm64/bin/lefthook -h >/dev/null 2>&1
20+
then
21+
/usr/src/app/node_modules/.pnpm/[email protected]/node_modules/lefthook-linux-arm64/bin/lefthook "$@"
22+
else
23+
dir="$(git rev-parse --show-toplevel)"
24+
osArch=$(uname | tr '[:upper:]' '[:lower:]')
25+
cpuArch=$(uname -m | sed 's/aarch64/arm64/;s/x86_64/x64/')
26+
if test -f "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook"
27+
then
28+
"$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook" "$@"
29+
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook"
30+
then
31+
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook" "$@"
32+
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook"
33+
then
34+
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook" "$@"
35+
elif test -f "$dir/node_modules/lefthook/bin/index.js"
36+
then
37+
"$dir/node_modules/lefthook/bin/index.js" "$@"
38+
elif go tool lefthook -h >/dev/null 2>&1
39+
then
40+
go tool lefthook "$@"
41+
elif bundle exec lefthook -h >/dev/null 2>&1
42+
then
43+
bundle exec lefthook "$@"
44+
elif yarn lefthook -h >/dev/null 2>&1
45+
then
46+
yarn lefthook "$@"
47+
elif pnpm lefthook -h >/dev/null 2>&1
48+
then
49+
pnpm lefthook "$@"
50+
elif swift package lefthook >/dev/null 2>&1
51+
then
52+
swift package --build-path .build/lefthook --disable-sandbox lefthook "$@"
53+
elif command -v mint >/dev/null 2>&1
54+
then
55+
mint run csjones/lefthook-plugin "$@"
56+
elif uv run lefthook -h >/dev/null 2>&1
57+
then
58+
uv run lefthook "$@"
59+
elif mise exec -- lefthook -h >/dev/null 2>&1
60+
then
61+
mise exec -- lefthook "$@"
62+
elif devbox run lefthook -h >/dev/null 2>&1
63+
then
64+
devbox run lefthook "$@"
65+
else
66+
echo "Can't find lefthook in PATH"
67+
fi
68+
fi
69+
}
70+
71+
call_lefthook run "pre-commit" "$@"

.husky/_/prepare-commit-msg

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/sh
2+
3+
if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then
4+
set -x
5+
fi
6+
7+
if [ "$LEFTHOOK" = "0" ]; then
8+
exit 0
9+
fi
10+
11+
call_lefthook()
12+
{
13+
if test -n "$LEFTHOOK_BIN"
14+
then
15+
"$LEFTHOOK_BIN" "$@"
16+
elif lefthook -h >/dev/null 2>&1
17+
then
18+
lefthook "$@"
19+
elif /usr/src/app/node_modules/.pnpm/[email protected]/node_modules/lefthook-linux-arm64/bin/lefthook -h >/dev/null 2>&1
20+
then
21+
/usr/src/app/node_modules/.pnpm/[email protected]/node_modules/lefthook-linux-arm64/bin/lefthook "$@"
22+
else
23+
dir="$(git rev-parse --show-toplevel)"
24+
osArch=$(uname | tr '[:upper:]' '[:lower:]')
25+
cpuArch=$(uname -m | sed 's/aarch64/arm64/;s/x86_64/x64/')
26+
if test -f "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook"
27+
then
28+
"$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook" "$@"
29+
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook"
30+
then
31+
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook" "$@"
32+
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook"
33+
then
34+
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook" "$@"
35+
elif test -f "$dir/node_modules/lefthook/bin/index.js"
36+
then
37+
"$dir/node_modules/lefthook/bin/index.js" "$@"
38+
elif go tool lefthook -h >/dev/null 2>&1
39+
then
40+
go tool lefthook "$@"
41+
elif bundle exec lefthook -h >/dev/null 2>&1
42+
then
43+
bundle exec lefthook "$@"
44+
elif yarn lefthook -h >/dev/null 2>&1
45+
then
46+
yarn lefthook "$@"
47+
elif pnpm lefthook -h >/dev/null 2>&1
48+
then
49+
pnpm lefthook "$@"
50+
elif swift package lefthook >/dev/null 2>&1
51+
then
52+
swift package --build-path .build/lefthook --disable-sandbox lefthook "$@"
53+
elif command -v mint >/dev/null 2>&1
54+
then
55+
mint run csjones/lefthook-plugin "$@"
56+
elif uv run lefthook -h >/dev/null 2>&1
57+
then
58+
uv run lefthook "$@"
59+
elif mise exec -- lefthook -h >/dev/null 2>&1
60+
then
61+
mise exec -- lefthook "$@"
62+
elif devbox run lefthook -h >/dev/null 2>&1
63+
then
64+
devbox run lefthook "$@"
65+
else
66+
echo "Can't find lefthook in PATH"
67+
fi
68+
fi
69+
}
70+
71+
call_lefthook run "prepare-commit-msg" "$@"

.husky/pre-commit

Lines changed: 0 additions & 1 deletion
This file was deleted.

CONTRIBUTING.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- 文法およびフォーマットチェッカー
5050
- [ESLint](https://eslint.org/)
5151
- [Prettier](https://prettier.io/)
52+
- [lefthook](https://github.com/evilmartians/lefthook): Git hooks 管理ツール(コミット前の自動フォーマット・リント)
5253
- Search Engine Optimization (SEO) 対策
5354
- [Svelte Meta Tags](https://github.com/oekazuma/svelte-meta-tags): メタタグ、Open Graph などの設定
5455
- [super-sitemap](https://github.com/jasongitmail/super-sitemap): SvelteKit 専用の sitemap ジェネレータ
@@ -268,6 +269,35 @@
268269

269270
6. プルリクエストを作成します。
270271

272+
### Git Hooks(フォーマット・リント)
273+
274+
本プロジェクトでは、[lefthook](https://github.com/evilmartians/lefthook)を使用して、コミット前に自動的にコードの書式チェック・フォーマットを行います。
275+
276+
- **Pre-commit Hook**: ステージ済みファイルのみに対して以下を実行
277+
- `prettier --write`: コード書式の自動修正(JavaScript、TypeScript、Markdown、Svelte)
278+
- `eslint`: リント(JavaScript、TypeScript、Svelte)
279+
280+
Hook は自動的にセットアップされるため、特別な操作は不要です。
281+
282+
#### Hook を実行したくない場合
283+
284+
環境変数 `LEFTHOOK=0` を設定して commit してください。
285+
286+
```bash
287+
LEFTHOOK=0 git commit -m "コミットメッセージ"
288+
```
289+
290+
#### (既存ユーザ向け) husky から lefthook への移行
291+
292+
husky でセットアップ済みの開発環境で新しい PR をマージした場合、以下を実行してください:
293+
294+
```bash
295+
git config --unset core.hooksPath
296+
pnpm exec lefthook install
297+
```
298+
299+
これにより、古い husky の設定をクリアして lefthook に切り替わります。
300+
271301
### トラブルシューティング
272302

273303
- エラー: ローカル環境で開発用サーバを立ち上げても、ブラウザに表示されない
@@ -283,6 +313,3 @@
283313
- エラー: Docker Desktop で Vite を利用したときに Segmentation Fault が発生
284314
- 対処方法: Docker Desktopで「Use Visualization Framework」のチェックを外す
285315
- 参考資料: https://qiita.com/naoto24kawa/items/160aad0ca58642216a0a
286-
287-
- エラー: コミットを実行したときに、`hint: The '.husky/pre-commit' hook was ignored because it's not set as executable.`と表示される
288-
- 対処方法: ターミナルで`chmod ug+x .husky/*`を実行する

0 commit comments

Comments
 (0)