1
1
import process from 'node:process' ;
2
2
import path from 'node:path' ;
3
- import { fileURLToPath } from 'node:url' ;
4
3
import pathKey from 'path-key' ;
4
+ import { toPath , traversePathUp } from 'unicorn-magic' ;
5
5
6
6
export const npmRunPath = ( {
7
7
cwd = process . cwd ( ) ,
@@ -10,8 +10,7 @@ export const npmRunPath = ({
10
10
execPath = process . execPath ,
11
11
addExecPath = true ,
12
12
} = { } ) => {
13
- const cwdString = cwd instanceof URL ? fileURLToPath ( cwd ) : cwd ;
14
- const cwdPath = path . resolve ( cwdString ) ;
13
+ const cwdPath = path . resolve ( toPath ( cwd ) ) ;
15
14
const result = [ ] ;
16
15
17
16
if ( preferLocal ) {
@@ -26,19 +25,14 @@ export const npmRunPath = ({
26
25
} ;
27
26
28
27
const applyPreferLocal = ( result , cwdPath ) => {
29
- let previous ;
30
-
31
- while ( previous !== cwdPath ) {
32
- result . push ( path . join ( cwdPath , 'node_modules/.bin' ) ) ;
33
- previous = cwdPath ;
34
- cwdPath = path . resolve ( cwdPath , '..' ) ;
28
+ for ( const directory of traversePathUp ( cwdPath ) ) {
29
+ result . push ( path . join ( directory , 'node_modules/.bin' ) ) ;
35
30
}
36
31
} ;
37
32
38
33
// Ensure the running `node` binary is used
39
34
const applyExecPath = ( result , execPath , cwdPath ) => {
40
- const execPathString = execPath instanceof URL ? fileURLToPath ( execPath ) : execPath ;
41
- result . push ( path . resolve ( cwdPath , execPathString , '..' ) ) ;
35
+ result . push ( path . resolve ( cwdPath , toPath ( execPath ) , '..' ) ) ;
42
36
} ;
43
37
44
38
export const npmRunPathEnv = ( { env = process . env , ...options } = { } ) => {
0 commit comments