11#!/usr/bin/env node
22/**
3- * PostToolUse Hook: Auto-format JS/TS files with Prettier after edits
3+ * PostToolUse Hook: Auto-format JS/TS files after edits
44 *
55 * Cross-platform (Windows, macOS, Linux)
66 *
77 * Runs after Edit tool use. If the edited file is a JS/TS file,
8- * formats it with Prettier. Fails silently if Prettier isn't installed.
8+ * auto-detects the project formatter (Biome or Prettier) by looking
9+ * for config files, then formats accordingly.
10+ * Fails silently if no formatter is found or installed.
911 */
1012
1113const { execFileSync } = require ( 'child_process' ) ;
14+ const fs = require ( 'fs' ) ;
1215const path = require ( 'path' ) ;
1316
1417const MAX_STDIN = 1024 * 1024 ; // 1MB limit
@@ -22,22 +25,72 @@ process.stdin.on('data', chunk => {
2225 }
2326} ) ;
2427
28+ function findProjectRoot ( startDir ) {
29+ let dir = startDir ;
30+ while ( dir !== path . dirname ( dir ) ) {
31+ if ( fs . existsSync ( path . join ( dir , 'package.json' ) ) ) return dir ;
32+ dir = path . dirname ( dir ) ;
33+ }
34+ return startDir ;
35+ }
36+
37+ function detectFormatter ( projectRoot ) {
38+ const biomeConfigs = [ 'biome.json' , 'biome.jsonc' ] ;
39+ for ( const cfg of biomeConfigs ) {
40+ if ( fs . existsSync ( path . join ( projectRoot , cfg ) ) ) return 'biome' ;
41+ }
42+
43+ const prettierConfigs = [
44+ '.prettierrc' ,
45+ '.prettierrc.json' ,
46+ '.prettierrc.js' ,
47+ '.prettierrc.cjs' ,
48+ '.prettierrc.mjs' ,
49+ '.prettierrc.yml' ,
50+ '.prettierrc.yaml' ,
51+ '.prettierrc.toml' ,
52+ 'prettier.config.js' ,
53+ 'prettier.config.cjs' ,
54+ 'prettier.config.mjs' ,
55+ ] ;
56+ for ( const cfg of prettierConfigs ) {
57+ if ( fs . existsSync ( path . join ( projectRoot , cfg ) ) ) return 'prettier' ;
58+ }
59+
60+ return null ;
61+ }
62+
63+ function getFormatterCommand ( formatter , filePath ) {
64+ const npxBin = process . platform === 'win32' ? 'npx.cmd' : 'npx' ;
65+ if ( formatter === 'biome' ) {
66+ return { bin : npxBin , args : [ '@biomejs/biome' , 'format' , '--write' , filePath ] } ;
67+ }
68+ if ( formatter === 'prettier' ) {
69+ return { bin : npxBin , args : [ 'prettier' , '--write' , filePath ] } ;
70+ }
71+ return null ;
72+ }
73+
2574process . stdin . on ( 'end' , ( ) => {
2675 try {
2776 const input = JSON . parse ( data ) ;
2877 const filePath = input . tool_input ?. file_path ;
2978
3079 if ( filePath && / \. ( t s | t s x | j s | j s x ) $ / . test ( filePath ) ) {
3180 try {
32- // Use npx.cmd on Windows to avoid shell: true which enables command injection
33- const npxBin = process . platform === 'win32' ? 'npx.cmd' : 'npx' ;
34- execFileSync ( npxBin , [ 'prettier' , '--write' , filePath ] , {
35- cwd : path . dirname ( path . resolve ( filePath ) ) ,
36- stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
37- timeout : 15000
38- } ) ;
81+ const projectRoot = findProjectRoot ( path . dirname ( path . resolve ( filePath ) ) ) ;
82+ const formatter = detectFormatter ( projectRoot ) ;
83+ const cmd = getFormatterCommand ( formatter , filePath ) ;
84+
85+ if ( cmd ) {
86+ execFileSync ( cmd . bin , cmd . args , {
87+ cwd : projectRoot ,
88+ stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
89+ timeout : 15000
90+ } ) ;
91+ }
3992 } catch {
40- // Prettier not installed, file missing, or failed — non-blocking
93+ // Formatter not installed, file missing, or failed — non-blocking
4194 }
4295 }
4396 } catch {
0 commit comments