Skip to content

hookCode quality

Format After Edit

A Claude Code hook that runs Prettier on each file the agent edits, and quietly does nothing when the project has no Prettier installed.

npx workspacetemplates add format-after-edit --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 734 BFile
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)}catch(e){process.exit(0)}const i=j.tool_input||{};const f=i.file_path;const fs=require('fs');if(!f||!/[.](js|jsx|ts|tsx|json|css|scss|html|md|mdx|vue|svelte|yml|yaml)$/i.test(f)||!fs.existsSync(f)||!fs.existsSync('node_modules/prettier'))process.exit(0);require('child_process').spawnSync('npx',['--no-install','prettier','--write',f],{stdio:'ignore',shell:process.platform==='win32'});process.exit(0)})\"",
            "timeout": 30
          }
        ]
      }
    ]
  }
}

What it does

Agent edits often drift from your formatting. This PostToolUse hook runs Prettier on the single file that was just written, so diffs stay clean without a separate formatting step.

It only acts when node_modules/prettier exists and the file type is one Prettier handles. In any other project it exits silently.

Example

you › Add a retry helper to src/http.ts.

agent › The helper is written, then Prettier rewrites src/http.ts with your quote and width settings. The diff shows no formatting noise.

Questions

What if I have no Prettier?
Nothing happens. The hook checks for node_modules/prettier and exits quietly without it.
Which files does it format?
JavaScript, TypeScript, JSON, CSS, HTML, Markdown, Vue, Svelte and YAML files.
  • Block Generated File Edits

    HookCode quality

    Free
  • Block Large Writes

    HookCode quality

    Free
  • Block Vendor Edits

    HookCode quality

    Free
  • Ensure Final Newline

    HookCode quality

    Free