@@ -57,6 +57,20 @@ execute_command() {
5757 exit 0
5858}
5959
60+ # Navigate up one directory. The command is successful if this is possible
61+ # without leaving the current project, entering the home directory, or entering
62+ # the root directory.
63+ go_up () {
64+ if [ " $PWD " = " $PROJECT_ROOT " ]; then
65+ return 1
66+ fi
67+ cd ..
68+ if [ " $PWD " = " $HOME " ] || [ " $PWD " = / ]; then
69+ return 1
70+ fi
71+ return 0
72+ }
73+
6074# Every supported tool requires a function `try_tool` where `tool` is a name
6175# indicating what tool the function tries to detect. The function should:
6276#
@@ -70,19 +84,39 @@ execute_command() {
7084
7185# JavaScript + NodeJS
7286
87+ # After a `package.json` has been found this detects the tool to use based on
88+ # any present lock file. In monorepo setups the lockfile might be in a parent
89+ # directory and not right next to the `package.json` file. This is handled by
90+ # looking up the directory hierarchy.
91+ detect_nodejs_tool () {
92+ while :
93+ do
94+ # echo looking for nodejs tool
95+ if [ -f package-lock.json ]; then
96+ echo yarn
97+ return
98+ elif [ -f bun.lock ] || [ -f bun.lockb ]; then
99+ echo bun
100+ return
101+ elif [ -f yarn.lock ]; then
102+ echo yarn
103+ return
104+ elif [ -f pnpm-lock.yaml ]; then
105+ echo pnpm
106+ return
107+ fi
108+ if ! go_up; then
109+ echo npm
110+ return
111+ fi
112+ done
113+ }
114+
73115try_nodejs () {
74116 if [ ! -f package.json ]; then
75117 return 1
76118 fi
77- if [ -f bun.lock ] || [ -f bun.lockb ]; then
78- tool=bun
79- elif [ -f yarn.lock ]; then
80- tool=yarn
81- elif [ -f pnpm-lock.yaml ]; then
82- tool=pnpm
83- else
84- tool=npm
85- fi
119+ tool=$( detect_nodejs_tool)
86120 if ! has_command $tool ; then
87121 echo " Found a package.json file but '$tool ' is not installed."
88122 exit 1
@@ -507,15 +541,9 @@ set_project_root
507541
508542while :
509543do
510- # We don't want to do anything if we are in the home or root directory
511- if [ " $PWD " = " $HOME " ] || [ " $PWD " = / ]; then
512- nothing_found
513- fi
514544 detect_and_run
515545 # If we didn't detect a tool to run in this directory we go up one directory
516- # while ensuring that we don't leave the project root
517- if [ " $PWD " = " $PROJECT_ROOT " ]; then
546+ if ! go_up; then
518547 nothing_found
519548 fi
520- cd ..
521549done
0 commit comments