#!/usr/bin/env node import { spawnSync } from 'node:child_process'; import { existsSync, readdirSync, realpathSync } from 'node:fs'; import { isAbsolute, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const HOOKS = '.githooks'; const packageRoot = fileURLToPath(new URL('..', import.meta.url)); function git(args) { const result = spawnSync('git', args, { cwd: packageRoot, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }); return result.status === 0 ? result.stdout.trim() : undefined; } function hasRealHooks(dir) { if (!existsSync(dir)) return false; return readdirSync(dir).some((name) => !name.endsWith('.sample') && !name.startsWith('.')); } try { const topLevel = process.env.CI ? undefined : git(['rev-parse', '--show-toplevel']); if (topLevel !== undefined && realpathSync(topLevel) === realpathSync(packageRoot)) { const configured = git(['config', '--type=path', '--get', 'core.hooksPath']); if (configured !== HOOKS) { const commonDir = resolve(packageRoot, git(['rev-parse', '--git-common-dir']) ?? '.git'); const effective = configured === undefined ? join(commonDir, 'hooks') : isAbsolute(configured) ? configured : resolve(packageRoot, configured); if (hasRealHooks(effective)) { console.warn( `[browserclaw] Git hooks in ${effective} are kept; the ${HOOKS}/pre-push check (format:check and lint) is not installed.`, ); } else { if (git(['config', '--local', 'core.hooksPath', HOOKS]) === undefined) throw new Error(`git config core.hooksPath ${HOOKS} failed`); console.warn(`[browserclaw] Set core.hooksPath to ${HOOKS}: git push now runs format:check and lint.`); } } } } catch (err) { console.warn(`[browserclaw] Skipped installing git hooks: ${err instanceof Error ? err.message : String(err)}`); }