Skip to content

Write HTML. Render video. Built for agents.

2.8 KB
Raw Blame
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "timeout": 180,
            "statusMessage": "Running build + lint + typecheck before commit…",
            "command": "node -e \"\nconst chunks = [];\nprocess.stdin.on('data', d => chunks.push(d));\nprocess.stdin.on('end', () => {\n  const input = JSON.parse(Buffer.concat(chunks).toString());\n  const cmd = input.tool_input?.command || '';\n  if (!/git\\s+commit\\b/.test(cmd)) process.exit(0);\n  const { execSync } = require('child_process');\n  const fs = require('fs');\n  const path = require('path');\n  const os = require('os');\n  // A leading 'cd <path>' in the command is the real target directory:\n  // Claude Code prefixes this whenever the session's tracked cwd differs\n  // from this hook process's own cwd (e.g. a sibling repo worktree) — the\n  // hook process itself always inherits the primary working directory,\n  // never the command's actual target.\n  function resolveTargetDir() {\n    const m = cmd.match(/^\\s*cd\\s+('([^']+)'|(\\S+))\\s*(?:&&|;|\\n|$)/);\n    if (m) {\n      let dir = m[2] || m[3];\n      if (dir.startsWith('~')) dir = path.join(os.homedir(), dir.slice(1));\n      dir = path.resolve(process.cwd(), dir);\n      if (fs.existsSync(dir)) return dir;\n    }\n    return process.cwd();\n  }\n  const targetDir = resolveTargetDir();\n  let repoRoot;\n  try {\n    repoRoot = execSync('git rev-parse --show-toplevel', { cwd: targetDir, encoding: 'utf8' }).trim();\n  } catch {\n    process.exit(0);\n  }\n  // Only this repo's own bun build/lint/typecheck applies — skip silently\n  // for any other repo (e.g. a sibling Go/Python repo touched in the same session).\n  const pkgPath = path.join(repoRoot, 'package.json');\n  if (!fs.existsSync(pkgPath)) process.exit(0);\n  let pkg;\n  try {\n    pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));\n  } catch {\n    process.exit(0);\n  }\n  if (pkg.name !== 'hyperframes-monorepo') process.exit(0);\n  const steps = [\n    ['bun run build', 'Build'],\n    ['bun run lint', 'Lint'],\n    ['bun run --filter \\'*\\' typecheck 2>&1 | grep -v \\'vitest\\\\|test\\\\.ts\\' || true', 'Typecheck'],\n  ];\n  const failures = [];\n  for (const [script, label] of steps) {\n    try {\n      execSync(script, { cwd: repoRoot, stdio: 'pipe' });\n    } catch (e) {\n      failures.push(label + ':\\n' + (e.stdout?.toString() || e.message).slice(0, 400));\n    }\n  }\n  if (failures.length > 0) {\n    process.stdout.write(JSON.stringify({\n      hookSpecificOutput: {\n        hookEventName: 'PreToolUse',\n        permissionDecision: 'deny',\n        permissionDecisionReason: '❌ Pre-commit checks failed:\\n\\n' + failures.join('\\n\\n') + '\\n\\nFix the issues above, then retry the commit.',\n      },\n    }));\n  }\n});\""
          }
        ]
      }
    ]
  }
}