Repository navigation
feat: ファイル行数チェック機能を追加 #484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,12 +1,13 @@ | ||||||||||||||||||
| Husky + lint-staged + commitlint 最小構成 | ||||||||||||||||||
| Husky + lint-staged + commitlint + file-length 最小構成 | ||||||||||||||||||
|
|
||||||||||||||||||
| コミット前に軽量な自動整形と静的チェックを行い、プッシュ前とCIで重い検証を実行します。 | ||||||||||||||||||
|
|
||||||||||||||||||
| 方針 | ||||||||||||||||||
| • pre-commit: ステージ済みファイルに対して ESLint 自動修正と Prettier 整形を高速実行 | ||||||||||||||||||
| • pre-commit: ステージ済みファイルに対して ESLint 自動修正、Prettier 整形、ファイル行数チェックを高速実行 | ||||||||||||||||||
| • pre-push: 型チェックとテストを実行 | ||||||||||||||||||
| • commit-msg: Conventional Commits を commitlint で検証 | ||||||||||||||||||
| • npm --prefix next で一貫して next/ の依存を使用 | ||||||||||||||||||
| • file-length: 500行以上のファイルはコミットをブロック、350行以上は警告 | ||||||||||||||||||
|
|
||||||||||||||||||
| ⸻ | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -15,10 +16,26 @@ Husky + lint-staged + commitlint 最小構成 | |||||||||||||||||
| npm i -D husky lint-staged @commitlint/cli @commitlint/config-conventional | ||||||||||||||||||
| npm pkg set scripts.prepare="husky" | ||||||||||||||||||
| npm run prepare | ||||||||||||||||||
| npx husky add .husky/pre-commit "npx lint-staged" | ||||||||||||||||||
| npx husky add .husky/pre-commit "npx lint-staged && bash scripts/check-file-length.sh" | ||||||||||||||||||
| npx husky add .husky/commit-msg "npx commitlint --edit \$1" | ||||||||||||||||||
| npx husky add .husky/pre-push "npm --prefix next run type-check && npm --prefix next run test:ci" | ||||||||||||||||||
|
|
||||||||||||||||||
| # ファイル行数チェックの設定 | ||||||||||||||||||
|
|
||||||||||||||||||
| mkdir -p scripts | ||||||||||||||||||
|
|
||||||||||||||||||
| # DevContainer 環境の場合 | ||||||||||||||||||
|
|
||||||||||||||||||
| if [ -f /usr/local/script/check-file-length.sh ]; then | ||||||||||||||||||
| cp /usr/local/script/check-file-length.sh scripts/ | ||||||||||||||||||
| fi | ||||||||||||||||||
|
|
||||||||||||||||||
| # テンプレートから .filelengthignore をコピー | ||||||||||||||||||
|
|
||||||||||||||||||
| if [ -f /usr/local/share/config-templates/.filelengthignore.template ]; then | ||||||||||||||||||
| cp /usr/local/share/config-templates/.filelengthignore.template .filelengthignore | ||||||||||||||||||
| fi | ||||||||||||||||||
|
|
||||||||||||||||||
| ⸻ | ||||||||||||||||||
|
|
||||||||||||||||||
| 必要な npm scripts(next/package.json) | ||||||||||||||||||
|
|
@@ -119,10 +136,29 @@ Husky の再初期化 | |||||||||||||||||
|
|
||||||||||||||||||
| rm -rf .husky | ||||||||||||||||||
| npm run prepare | ||||||||||||||||||
| npx husky add .husky/pre-commit "npx lint-staged" | ||||||||||||||||||
| npx husky add .husky/pre-commit "npx lint-staged && bash scripts/check-file-length.sh" | ||||||||||||||||||
| npx husky add .husky/commit-msg "npx commitlint --edit \$1" | ||||||||||||||||||
| npx husky add .husky/pre-push "npm --prefix next run type-check && npm --prefix next run test:ci" | ||||||||||||||||||
|
|
||||||||||||||||||
| ⸻ | ||||||||||||||||||
|
|
||||||||||||||||||
| ファイル行数チェックの設定 | ||||||||||||||||||
|
|
||||||||||||||||||
| • 警告閾値: FILE_LENGTH_WARN_LIMIT=350 (デフォルト) | ||||||||||||||||||
| • エラー閾値: FILE_LENGTH_HARD_LIMIT=500 (デフォルト) | ||||||||||||||||||
| • 除外設定: .filelengthignore(.gitignore と同じ記法) | ||||||||||||||||||
|
|
||||||||||||||||||
| .filelengthignore の例: | ||||||||||||||||||
|
|
||||||||||||||||||
| # 自動生成ファイル | ||||||||||||||||||
|
|
||||||||||||||||||
| **/_.generated._ | ||||||||||||||||||
| **/database.types.ts | ||||||||||||||||||
|
Comment on lines
+153
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Glob pattern corrupted by Markdown italic rendering.
🔧 Proposed fix-**/_.generated._
+**/*.generated.*Or wrap the entire example in a fenced code block to prevent Markdown interpretation: 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| # 複雑なステートマシン | ||||||||||||||||||
|
|
||||||||||||||||||
| src/contexts/complex-context.tsx | ||||||||||||||||||
|
|
||||||||||||||||||
| 一時的にフックをスキップ | ||||||||||||||||||
|
|
||||||||||||||||||
| git commit --no-verify -m "urgent: emergency fix" | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../.agents/skills/find-skills |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../.agents/skills/react-doctor |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../.agents/skills/supabase-postgres-best-practices |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # .filelengthignore - 行数チェック (check-file-length.sh) の除外パターン | ||
| # | ||
| # .gitignore と同じ記法でパターンを指定できます。 | ||
| # - 空行とコメント行 (#) は無視されます | ||
| # - glob パターンが使えます(*, **, ? など) | ||
| # - パスはリポジトリルートからの相対パスで評価されます | ||
| # | ||
| # 例: | ||
| # src/lib/types/database.ts # 完全パス指定 | ||
| # **/*.generated.ts # glob パターン | ||
| # **/schema.sql # 任意の階層 | ||
|
|
||
| # 自動生成ファイル | ||
| **/*.generated.* | ||
| **/*.gen.* | ||
| **/database.types.ts | ||
|
|
||
| # ビルド出力 | ||
| dist/** | ||
| build/** | ||
| .next/** | ||
| node_modules/** | ||
|
|
||
| # テスト関連 | ||
| **/*.test.ts | ||
| **/*.spec.ts | ||
| **/coverage/** | ||
|
|
||
| # 設定ファイル | ||
| **/package-lock.json | ||
| **/pnpm-lock.yaml |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,21 +1,22 @@ | ||||||
| #!/usr/bin/env bash | ||||||
| # | ||||||
| # ============================================================================ | ||||||
| # check-file-length.sh - ステージされたファイルの行数チェック | ||||||
| # | ||||||
| # ファイルが長すぎる場合に警告またはエラーを出力します。 | ||||||
| # 除外パターンは .filelengthignore で管理(.gitignore と同じ記法) | ||||||
| # | ||||||
| # ============================================================================ | ||||||
|
|
||||||
| set -euo pipefail | ||||||
|
|
||||||
| HARD_LIMIT=500 | ||||||
| WARN_LIMIT=350 | ||||||
| REPO_ROOT="$(git rev-parse --show-toplevel)" | ||||||
| HARD_LIMIT=${FILE_LENGTH_HARD_LIMIT:-500} | ||||||
| WARN_LIMIT=${FILE_LENGTH_WARN_LIMIT:-350} | ||||||
| REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)" | ||||||
| IGNORE_FILE="$REPO_ROOT/.filelengthignore" | ||||||
|
|
||||||
| # ステージされた TS/JS ファイル(削除以外)を取得 | ||||||
| staged_files() { | ||||||
| git diff --cached --name-only --diff-filter=d -- \ | ||||||
| '*.ts' '*.tsx' '*.js' '*.jsx' | ||||||
| '*.ts' '*.tsx' '*.js' '*.jsx' 2>/dev/null || true | ||||||
| } | ||||||
|
|
||||||
| # .filelengthignore のパターンで除外判定(git check-ignore を .gitignore 記法で流用) | ||||||
|
|
@@ -33,7 +34,7 @@ while IFS= read -r file; do | |||||
| [ -z "$file" ] && continue | ||||||
| is_ignored "$file" && continue | ||||||
|
|
||||||
| line_count=$(git show ":$file" 2>/dev/null | wc -l) | ||||||
| line_count=$(git show ":$file" 2>/dev/null | wc -l | tr -d ' ') | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With Adding 🔧 Proposed fix- line_count=$(git show ":$file" 2>/dev/null | wc -l | tr -d ' ')
+ line_count=$(git show ":$file" 2>/dev/null | wc -l | tr -d ' ') || continue📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
| if [ "$line_count" -ge "$HARD_LIMIT" ]; then | ||||||
| errors+=(" ❌ ${file} (${line_count}行)") | ||||||
|
|
@@ -60,3 +61,5 @@ if [ ${#errors[@]} -gt 0 ]; then | |||||
| echo " 2. .filelengthignore に除外パターンを追加する" | ||||||
| exit 1 | ||||||
| fi | ||||||
|
|
||||||
| exit 0 | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #!/usr/bin/env bash | ||
| # ============================================================================ | ||
| # setup-file-length-check.sh - ファイル行数チェックのセットアップ | ||
| # | ||
| # 使用方法: | ||
| # ./setup-file-length-check.sh # カレントディレクトリにセットアップ | ||
| # ./setup-file-length-check.sh /path # 指定ディレクトリにセットアップ | ||
| # ============================================================================ | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| TARGET_DIR="${1:-.}" | ||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| TEMPLATE_DIR="/usr/local/share/config-templates" | ||
|
|
||
| echo "[INFO] ファイル行数チェックをセットアップします..." | ||
|
|
||
| # ターゲットディレクトリに移動 | ||
| cd "$TARGET_DIR" | ||
|
|
||
| # scripts ディレクトリを作成 | ||
| mkdir -p scripts | ||
|
|
||
| # check-file-length.sh をコピー | ||
| if [ -f "$SCRIPT_DIR/check-file-length.sh" ]; then | ||
| cp "$SCRIPT_DIR/check-file-length.sh" scripts/ | ||
| chmod +x scripts/check-file-length.sh | ||
| echo "[SUCCESS] scripts/check-file-length.sh をコピーしました" | ||
| elif [ -f /usr/local/script/check-file-length.sh ]; then | ||
| cp /usr/local/script/check-file-length.sh scripts/ | ||
| chmod +x scripts/check-file-length.sh | ||
| echo "[SUCCESS] scripts/check-file-length.sh をコピーしました" | ||
| else | ||
| echo "[ERROR] check-file-length.sh が見つかりません" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # .filelengthignore をコピー(存在しない場合のみ) | ||
| if [ ! -f .filelengthignore ]; then | ||
| if [ -f "$TEMPLATE_DIR/.filelengthignore.template" ]; then | ||
| cp "$TEMPLATE_DIR/.filelengthignore.template" .filelengthignore | ||
|
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| echo "[SUCCESS] .filelengthignore を作成しました" | ||
| else | ||
| # テンプレートがない場合は最小限の内容で作成 | ||
| cat > .filelengthignore << 'IGNORE' | ||
| # .filelengthignore - 行数チェックの除外パターン | ||
| # .gitignore と同じ記法でパターンを指定 | ||
|
|
||
| # 自動生成ファイル | ||
| **/*.generated.* | ||
| **/*.gen.* | ||
|
|
||
| # ビルド出力 | ||
| dist/** | ||
| build/** | ||
| .next/** | ||
| node_modules/** | ||
| IGNORE | ||
| echo "[SUCCESS] .filelengthignore を作成しました(デフォルト)" | ||
| fi | ||
| else | ||
| echo "[INFO] .filelengthignore は既に存在します" | ||
| fi | ||
|
|
||
| # Husky pre-commit に追加(.husky/pre-commit が存在する場合) | ||
| if [ -f .husky/pre-commit ]; then | ||
| if ! grep -q "check-file-length.sh" .husky/pre-commit; then | ||
| { | ||
| echo "" | ||
| echo "echo \"[husky] Running file length check...\"" | ||
| echo "bash scripts/check-file-length.sh || exit 1" | ||
| } >> .husky/pre-commit | ||
| echo "[SUCCESS] .husky/pre-commit に filelength チェックを追加しました" | ||
| else | ||
| echo "[INFO] .husky/pre-commit には既に filelength チェックが含まれています" | ||
| fi | ||
| else | ||
| echo "[INFO] .husky/pre-commit が見つかりません(手動で追加してください)" | ||
| echo " npx husky add .husky/pre-commit \"bash scripts/check-file-length.sh\"" | ||
| fi | ||
|
|
||
| echo "" | ||
| echo "[SUCCESS] ファイル行数チェックのセットアップが完了しました" | ||
| echo "" | ||
| echo "設定:" | ||
| echo " - 警告閾値: 350行(FILE_LENGTH_WARN_LIMIT で変更可)" | ||
| echo " - エラー閾値: 500行(FILE_LENGTH_HARD_LIMIT で変更可)" | ||
| echo " - 除外設定: .filelengthignore" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
chmod +xafter copyingcheck-file-length.sh.cpdoes not reliably preserve the execute bit (it is subject to the process's umask). Without an explicitchmod +x, the copied script may not be executable, causing the pre-commit hook to fail withPermission denied. The corresponding automated path insetup-file-length-check.shdoes includechmod +x scripts/check-file-length.sh; the manual snippet should match:🔧 Proposed fix
if [ -f /usr/local/script/check-file-length.sh ]; then cp /usr/local/script/check-file-length.sh scripts/ +chmod +x scripts/check-file-length.sh fi📝 Committable suggestion
🤖 Prompt for AI Agents