Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
fixup! feat: add walk up dir lookup to satisfy local bins
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Apr 29, 2021
1 parent bd576ae commit 82a72ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ await libexec({
- `call`: An alternative command to run when using `packages` option **String**, defaults to empty string.
- `cache`: The path location to where the npm cache folder is placed **String**
- `color`: Output should use color? **Boolean**, defaults to `false`
- `localBin`: Location to the `node_modules/.bin` folder of the local project **String** to start scanning for bin files, defaults to `./node_modules/.bin`. **libexec** will walk up the directory structure looking for `node_modules/.bin` folders in parent folders that might satisfy the current `arg` and will use that bin if found.
- `localBin`: Location to the `node_modules/.bin` folder of the local project to start scanning for bin files **String**, defaults to `./node_modules/.bin`. **libexec** will walk up the directory structure looking for `node_modules/.bin` folders in parent folders that might satisfy the current `arg` and will use that bin if found.
- `locationMsg`: Overrides "at location" message when entering interactive mode **String**
- `log`: Sets an optional logger **Object**, defaults to `proc-log` module usage.
- `globalBin`: Location to the global space bin folder, same as: `$(npm bin -g)` **String**, defaults to empty string.
Expand Down
23 changes: 13 additions & 10 deletions lib/file-exists.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
const { dirname, resolve } = require('path')
const { resolve } = require('path')
const { promisify } = require('util')
const stat = promisify(require('fs').stat)
const walkUp = require('walk-up-path')

const fileExists = (file) => stat(file)
.then((stat) => stat.isFile())
.catch(() => false)

const localFileExists = async (dir, binName) => {
const binDir = resolve(dir, 'node_modules', '.bin')
const localFileExists = async (dir, binName, root = '/') => {
root = resolve(root).toLowerCase()

// return localBin if existing file is found
if (await fileExists(resolve(binDir, binName)))
return binDir
for (const path of walkUp(resolve(dir))) {
const binDir = resolve(path, 'node_modules', '.bin')

// no more dirs left to walk up, file just does not exist
if (dir === dirname(dir))
return false
if (await fileExists(resolve(binDir, binName)))
return binDir

return localFileExists(dirname(dir), binName)
if (path.toLowerCase() === root)
return false
}

return false
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"pacote": "^11.3.1",
"proc-log": "^1.0.0",
"read": "^1.0.7",
"read-package-json-fast": "^2.0.2"
"read-package-json-fast": "^2.0.2",
"walk-up-path": "^1.0.0"
}
}
14 changes: 14 additions & 0 deletions test/file-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const t = require('tap')
const { localFileExists } = require('../lib/file-exists.js')

t.test('missing root value', async t => {
const dir = t.testdir({
b: {
c: {},
},
})

// root value a is not part of the file system hierarchy
const fileExists = await localFileExists(dir, 'foo', 'a')
t.equal(fileExists, false, 'should return false on missing root')
})

0 comments on commit 82a72ae

Please sign in to comment.