1.1.0 • Published 2 years ago
find-bin v1.1.0
find-bin
To check if the command exists and where the executable file is, WITHOUT using child_process.
npm install find-bin
yarn add find-binExample usage
const { lookpath, lookpathSync } = require('find-bin');
const p = await lookpath('bash');
const pSync = lookpathSync('bash');
// "/bin/bash", otherwise "undefined"Advanced usage
const p = await lookpath('bash', {
include: ['/home/hiromu/.bin'],
exclude: ['/mnt'],
});
// include: Do scan also under `~/.bin`
// exclude: Do NOT scan under `/mnt`const p = await lookpath('bash', {
includeCommonPaths: true,
});
// includeCommonPaths: Will scan for bash in many common binary directories, not just those specified in PATHconst p = await lookpath('bash', {
findAll: true,
});
// ["/bin/bash"], an array containing all the bash binaries found in any path
// includeCommonPaths: Will scan for bash in many common binary directories, not just those specified in PATHMotivation
- I don't want to spawn
child_processin order to kickwhich,where,whereis, orcommand -v. - then I checked Go implementation of
exec.LookPath. - so I concluded that scanning under
$PATHor$Pathis the best straightforward way to check if the command exists.