index.js (938B)
1 import process from 'node:process'; 2 import path from 'node:path'; 3 import url from 'node:url'; 4 import pathKey from 'path-key'; 5 6 export function npmRunPath(options = {}) { 7 const { 8 cwd = process.cwd(), 9 path: path_ = process.env[pathKey()], 10 execPath = process.execPath, 11 } = options; 12 13 let previous; 14 const cwdString = cwd instanceof URL ? url.fileURLToPath(cwd) : cwd; 15 let cwdPath = path.resolve(cwdString); 16 const result = []; 17 18 while (previous !== cwdPath) { 19 result.push(path.join(cwdPath, 'node_modules/.bin')); 20 previous = cwdPath; 21 cwdPath = path.resolve(cwdPath, '..'); 22 } 23 24 // Ensure the running `node` binary is used. 25 result.push(path.resolve(cwdString, execPath, '..')); 26 27 return [...result, path_].join(path.delimiter); 28 } 29 30 export function npmRunPathEnv({env = process.env, ...options} = {}) { 31 env = {...env}; 32 33 const path = pathKey({env}); 34 options.path = env[path]; 35 env[path] = npmRunPath(options); 36 37 return env; 38 }