@@ -21,6 +21,37 @@ command_exists() {
2121 command -v " $1 " > /dev/null 2>&1
2222}
2323
24+ # Find a project-local Node binary by walking up from a file's directory.
25+ # Falls back to the global binary if no local installation is found.
26+ find_node_bin () {
27+ local file=" $1 "
28+ local bin_name=" $2 "
29+ local dir
30+
31+ dir=$( dirname " $file " )
32+ while [ " $dir " != " ." ] && [ " $dir " != " /" ]; do
33+ if [ -x " $dir /node_modules/.bin/$bin_name " ]; then
34+ echo " $dir /node_modules/.bin/$bin_name "
35+ return 0
36+ fi
37+ dir=$( dirname " $dir " )
38+ done
39+
40+ # Check the repo root.
41+ if [ -x " node_modules/.bin/$bin_name " ]; then
42+ echo " node_modules/.bin/$bin_name "
43+ return 0
44+ fi
45+
46+ # Fall back to the global binary.
47+ if command_exists " $bin_name " ; then
48+ echo " $bin_name "
49+ return 0
50+ fi
51+
52+ return 1
53+ }
54+
2455# Format a file and re-stage it if modified.
2556format_and_restage () {
2657 local file=" $1 "
@@ -55,22 +86,22 @@ for FILE in $STAGED_FILES; do
5586
5687 case " $EXT " in
5788 js|jsx|ts|tsx|json|md|markdown)
58- if ! command_exists prettier; then
89+ PRETTIER= $( find_node_bin " $FILE " " prettier" ) || {
5990 echo " Error: prettier is not installed. Please install it with: bun add -g prettier"
6091 exit 1
61- fi
62- format_and_restage " $FILE " prettier --write " $FILE "
92+ }
93+ format_and_restage " $FILE " " $PRETTIER " --write " $FILE "
6394 ;;
6495 yaml|yml)
6596 # Skip Helm templates (contain Go template syntax that Prettier cannot parse).
6697 if [[ " $FILE " == * /templates/* .yaml || " $FILE " == * /templates/* .yml ]]; then
6798 continue
6899 fi
69- if ! command_exists prettier; then
100+ PRETTIER= $( find_node_bin " $FILE " " prettier" ) || {
70101 echo " Error: prettier is not installed. Please install it with: bun add -g prettier"
71102 exit 1
72- fi
73- format_and_restage " $FILE " prettier --write " $FILE "
103+ }
104+ format_and_restage " $FILE " " $PRETTIER " --write " $FILE "
74105 ;;
75106 java)
76107 if ! command_exists google-java-format; then
0 commit comments