Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.

Handle node_modules without package.json #128

Merged
merged 1 commit into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions get-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand All @@ -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))
}
})
}
Expand Down
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
}

Expand Down
15 changes: 2 additions & 13 deletions test/get-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

Expand All @@ -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: {
Expand Down