From 500bb9f44f8ff12efa6f6f5fa35264c63db8cfc2 Mon Sep 17 00:00:00 2001 From: yaoweiprc <6896642+yaoweiprc@users.noreply.github.com> Date: Thu, 9 Jul 2026 08:59:24 +0800 Subject: [PATCH] feat: add Prettier formatting hook for file edits --- .claude/hooks/prettier-format.js | 36 ++++++++++++++++++++++++++++++++ .claude/settings.json | 14 +++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 .claude/hooks/prettier-format.js diff --git a/.claude/hooks/prettier-format.js b/.claude/hooks/prettier-format.js new file mode 100644 index 0000000000..b81a95bfcf --- /dev/null +++ b/.claude/hooks/prettier-format.js @@ -0,0 +1,36 @@ +#!/usr/bin/env node +// PostToolUse hook (Write|Edit): formats the touched file with the repo's local Prettier. +const fs = require('fs'); +const path = require('path'); +const { execFileSync } = require('child_process'); + +let raw = ''; +try { + raw = fs.readFileSync(0, 'utf8'); +} catch { + process.exit(0); +} + +let input; +try { + input = JSON.parse(raw); +} catch { + process.exit(0); +} + +const filePath = input.tool_response?.filePath || input.tool_input?.file_path; +if (!filePath || !fs.existsSync(filePath)) { + process.exit(0); +} + +const repoRoot = path.join(__dirname, '..', '..'); +const prettierCli = path.join(repoRoot, 'node_modules', 'prettier', 'bin', 'prettier.cjs'); + +try { + execFileSync(process.execPath, [prettierCli, '--ignore-unknown', '--write', filePath], { + cwd: repoRoot, + stdio: 'ignore', + }); +} catch { + // Ignore formatting failures (e.g. syntax errors) so the hook never blocks the agent. +} diff --git a/.claude/settings.json b/.claude/settings.json index 93246cffc1..ec2873793f 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -5,5 +5,19 @@ "Edit(packages/insomnia/bin/yarn-standalone.js)", "Write(packages/insomnia/bin/yarn-standalone.js)" ] + }, + "hooks": { + "PostToolUse": [ + { + "matcher": "Write|Edit", + "hooks": [ + { + "type": "command", + "command": "node .claude/hooks/prettier-format.js", + "timeout": 30 + } + ] + } + ] } }