|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +INPUT=$(cat) |
| 5 | +HOOK_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) |
| 6 | +source "$HOOK_DIR/lib/project-root.sh" |
| 7 | +PROJECT_ROOT=$(resolve_project_root "$INPUT" "$HOOK_DIR") || exit 0 |
| 8 | +FILE_PATHS=$(hook_file_paths_from_input "$INPUT") |
| 9 | + |
| 10 | +if [[ -z "$FILE_PATHS" ]]; then |
| 11 | + exit 0 |
| 12 | +fi |
| 13 | + |
| 14 | +FAILED=0 |
| 15 | + |
| 16 | +mark_failed() { |
| 17 | + FAILED=1 |
| 18 | +} |
| 19 | + |
| 20 | +run_prettier() { |
| 21 | + local output exit_code |
| 22 | + |
| 23 | + output=$(cd "$PROJECT_ROOT" && pnpm exec prettier --write --ignore-unknown --no-error-on-unmatched-pattern "$FILE_PATH" 2>&1) || exit_code=$? |
| 24 | + |
| 25 | + if [[ ${exit_code:-0} -ne 0 && -n "$output" ]]; then |
| 26 | + echo "$output" >&2 |
| 27 | + mark_failed |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +run_eslint() { |
| 32 | + case "$FILE_PATH" in |
| 33 | + *.ts|*.js|*.css) ;; |
| 34 | + *) return 0 ;; |
| 35 | + esac |
| 36 | + |
| 37 | + case "$FILE_PATH" in |
| 38 | + */dist/*|*/node_modules/*|*/__screenshots__/*|*/generated/*) return 0 ;; |
| 39 | + esac |
| 40 | + |
| 41 | + local dir project_dir rel_path json_output hard_errors total_errors readable |
| 42 | + |
| 43 | + dir=$(dirname "$FILE_PATH") |
| 44 | + project_dir="" |
| 45 | + while [[ "$dir" != "/" && "$dir" != "." ]]; do |
| 46 | + if [[ -f "$dir/eslint.config.js" ]]; then |
| 47 | + project_dir="$dir" |
| 48 | + break |
| 49 | + fi |
| 50 | + dir=$(dirname "$dir") |
| 51 | + done |
| 52 | + |
| 53 | + if [[ -z "$project_dir" ]]; then |
| 54 | + return 0 |
| 55 | + fi |
| 56 | + |
| 57 | + rel_path=$(hook_relative_path "$project_dir" "$FILE_PATH") |
| 58 | + |
| 59 | + local soft_rules="no-unused-vars|@typescript-eslint/no-unused-vars" |
| 60 | + |
| 61 | + json_output=$(cd "$project_dir" && pnpm exec eslint -c ./eslint.config.js --no-warn-ignored --cache --cache-location .eslintcache/ --format json "$rel_path" 2>/dev/null) || true |
| 62 | + |
| 63 | + hard_errors=$(echo "$json_output" | jq -r --arg soft "$soft_rules" ' |
| 64 | + [.[].messages[] | select(.severity == 2) | select(.ruleId | test($soft) | not)] | length |
| 65 | + ') 2>/dev/null || hard_errors="0" |
| 66 | + |
| 67 | + total_errors=$(echo "$json_output" | jq -r ' |
| 68 | + [.[].messages[] | select(.severity == 2)] | length |
| 69 | + ') 2>/dev/null || total_errors="0" |
| 70 | + |
| 71 | + if [[ "$total_errors" == "0" ]]; then |
| 72 | + return 0 |
| 73 | + fi |
| 74 | + |
| 75 | + readable=$(cd "$project_dir" && pnpm exec eslint -c ./eslint.config.js --no-warn-ignored --color --cache --cache-location .eslintcache/ "$rel_path" 2>&1) || true |
| 76 | + |
| 77 | + if [[ "$hard_errors" != "0" ]]; then |
| 78 | + echo "$readable" >&2 |
| 79 | + mark_failed |
| 80 | + else |
| 81 | + echo "$readable" >&2 |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +run_vale() { |
| 86 | + case "$FILE_PATH" in |
| 87 | + *.md|*.ts) ;; |
| 88 | + *) return 0 ;; |
| 89 | + esac |
| 90 | + |
| 91 | + case "$FILE_PATH" in |
| 92 | + *.test.*|*/starters/*|*/404/*|*/vendor/*|*/changelog/*|*/icons/*|*/generated/*|*/dist/*|*/LICENSE*|*/CHANGELOG*|*/NOTICE*) return 0 ;; |
| 93 | + esac |
| 94 | + |
| 95 | + case "$FILE_PATH" in |
| 96 | + */.claude/plans/*|*/.claude/projects/*) return 0 ;; |
| 97 | + esac |
| 98 | + |
| 99 | + local output exit_code |
| 100 | + |
| 101 | + output=$(cd "$PROJECT_ROOT" && config/vale/bin/vale --config .vale.ini "$FILE_PATH" 2>&1) || exit_code=$? |
| 102 | + |
| 103 | + if [[ ${exit_code:-0} -ne 0 && -n "$output" ]]; then |
| 104 | + echo "$output" >&2 |
| 105 | + mark_failed |
| 106 | + fi |
| 107 | +} |
| 108 | + |
| 109 | +run_stylelint() { |
| 110 | + case "$FILE_PATH" in |
| 111 | + *.css) ;; |
| 112 | + *) return 0 ;; |
| 113 | + esac |
| 114 | + |
| 115 | + case "$FILE_PATH" in |
| 116 | + */dist/*|*/node_modules/*|*/vendor/*) return 0 ;; |
| 117 | + esac |
| 118 | + |
| 119 | + local repo_root dir project_dir rel_path output exit_code |
| 120 | + |
| 121 | + repo_root="$PROJECT_ROOT" |
| 122 | + dir=$(dirname "$FILE_PATH") |
| 123 | + project_dir="" |
| 124 | + while [[ "$dir" != "/" && "$dir" != "." ]]; do |
| 125 | + if [[ -f "$dir/package.json" ]] && jq -e '.wireit["lint:style"]' "$dir/package.json" >/dev/null 2>&1; then |
| 126 | + project_dir="$dir" |
| 127 | + break |
| 128 | + fi |
| 129 | + dir=$(dirname "$dir") |
| 130 | + done |
| 131 | + |
| 132 | + if [[ -z "$project_dir" ]]; then |
| 133 | + return 0 |
| 134 | + fi |
| 135 | + |
| 136 | + rel_path=$(hook_relative_path "$project_dir" "$FILE_PATH") |
| 137 | + |
| 138 | + output=$(cd "$project_dir" && pnpm exec stylelint --config="$repo_root/stylelint.config.mjs" --color "$rel_path" 2>&1) || exit_code=$? |
| 139 | + |
| 140 | + if [[ ${exit_code:-0} -ne 0 && -n "$output" ]]; then |
| 141 | + echo "$output" >&2 |
| 142 | + mark_failed |
| 143 | + fi |
| 144 | +} |
| 145 | + |
| 146 | +while IFS= read -r FILE_PATH; do |
| 147 | + if [[ -z "$FILE_PATH" ]]; then |
| 148 | + continue |
| 149 | + fi |
| 150 | + |
| 151 | + FILE_PATH=$(resolve_hook_path "$PROJECT_ROOT" "$FILE_PATH") |
| 152 | + |
| 153 | + if [[ ! -e "$FILE_PATH" || -d "$FILE_PATH" ]]; then |
| 154 | + continue |
| 155 | + fi |
| 156 | + |
| 157 | + run_prettier |
| 158 | + run_eslint |
| 159 | + run_vale |
| 160 | + run_stylelint |
| 161 | +done <<<"$FILE_PATHS" |
| 162 | + |
| 163 | +if [[ "$FAILED" -ne 0 ]]; then |
| 164 | + exit 2 |
| 165 | +fi |
| 166 | + |
| 167 | +exit 0 |
0 commit comments