diff --git a/get-prefix.js b/get-prefix.js index 71bedff..3bb6780 100644 --- a/get-prefix.js +++ b/get-prefix.js @@ -6,20 +6,21 @@ const path = require('path') const statAsync = promisify(require('fs').stat) module.exports = getPrefix -function getPrefix (current, root) { - if (!root) { - const original = root = path.resolve(current) - while (path.basename(root) === 'node_modules') { - root = path.dirname(root) - } - if (original !== root) { - return Promise.resolve(root) - } else { - return getPrefix(root, root) - } +function getPrefix (root) { + const original = root = path.resolve(root) + while (path.basename(root) === 'node_modules') { + root = path.dirname(root) } - if (isRootPath(current, process.platform)) { + if (original !== root) { return Promise.resolve(root) + } else { + return Promise.resolve(getPrefixFromTree(root)) + } +} + +function getPrefixFromTree (current) { + if (isRootPath(current, process.platform)) { + return false } else { return Promise.all([ fileExists(path.join(current, 'package.json')), @@ -30,8 +31,7 @@ function getPrefix (current, root) { if (hasPkg || hasModules) { return current } else { - const parent = path.dirname(current) - return getPrefix(parent, root) + return getPrefixFromTree(path.dirname(current)) } }) } diff --git a/index.js b/index.js index 3033e9c..a1faee1 100644 --- a/index.js +++ b/index.js @@ -116,11 +116,7 @@ function npx (argv) { module.exports._localBinPath = localBinPath function localBinPath (cwd) { return require('./get-prefix.js')(cwd).then(prefix => { - const pkgjson = path.join(prefix, 'package.json') - return promisify(fs.stat)(pkgjson).then( - () => path.join(prefix, 'node_modules', '.bin'), - err => { if (err.code !== 'ENOENT') throw err } - ) + return prefix && path.join(prefix, 'node_modules', '.bin') }) } diff --git a/test/get-prefix.js b/test/get-prefix.js index df1dcd1..6fd71a2 100644 --- a/test/get-prefix.js +++ b/test/get-prefix.js @@ -40,11 +40,11 @@ test('detects if currently in an npm package using node_modules', t => { }) }) -test('returns the same path if no package was found in parent dirs', t => { +test('returns false if no package was found in parent dirs', t => { // Hopefully folks' tmpdir isn't inside an npm package ;) const tmp = os.tmpdir() return getPrefix(tmp).then(prefix => { - t.equal(prefix, tmp, 'returned the same path') + t.equal(prefix, false, 'returned the false') }) }) @@ -70,17 +70,6 @@ test('doesn\'t go too far while navigating up', t => { }) }) -test('returns root if we get there', t => { - let root = '/' - if (process.platform === 'win32') { - const currentDrive = process.cwd().match(/^([a-z]+):/i)[1] - root = `${currentDrive}:\\` - } - return getPrefix(root).then(prefix => { - t.equal(prefix, root, 'used the same root') - }) -}) - test('fileExists unit', t => { const fileExists = requireInject('../get-prefix.js', { fs: {