Package Exports
- @cedx/which
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@cedx/which) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Which for JS
Find the instances of an executable in the system path, implemented in JavaScript.
Requirements
The latest Node.js and npm versions. If you plan to play with the sources, you will also need the latest Gulp.js version.
Installing via npm
From a command prompt, run:
$ npm install --save @cedx/whichUsage
This package has an API based on Observables.
It provides a single function, which(), allowing to locate a command in the system path:
const {which} = require('@cedx/which');
which('foobar').subscribe(
path => {
// "path" is the absolute path to the executable.
console.log(`The "foobar" command is located at: ${path}`);
},
error => {
// The command was not found on the system path.
console.log('The "foobar" command is not found.');
}
);Options
The which\which() function accepts three parameters:
command: string: The command to be resolved.all: boolean = false: A value indicating whether to return all executables found, instead of just the first one.options: object = {}: The options to be passed to the underlying finder.
If you pass the true value as the second parameter, the function will return an array of all paths found, instead of only the first path found:
which('foobar', true).subscribe(paths => {
console.log('The "foobar" command is located at:');
for (let path of paths) console.log(path);
});You can pass an options object as the third parameter:
path: string|string[]: The system path, provided as a string or an array of directories. Defaults to thePATHenvironment variable.extensions: string|string[]: The executable file extensions, provided as a string or an array of file extensions. Defaults to thePATHEXTenvironment variable.pathSeparator: string: The character used to separate paths in the system path. Defaults to thepath.delimiterconstant.
The extensions option is only meaningful on the Windows platform, where the executability of a file is determined from its extension:
let options = {extensions: '.FOO;.EXE;.CMD'};
which('foobar', false, options).subscribe(path =>
console.log(`The "foobar" command is located at: ${path}`);
);Promise support
If you require it, an Observable can be converted to a Promise by using the toPromise() method:
let path = await which('foobar').toPromise();
console.log(`The "foobar" command is located at: ${path}`);Command line interface
From a command prompt, install the which executable:
$ npm install --global @cedx/whichThen use it to find the instances of an executable:
$ which --help
Usage: which [options] <command>
Find the instances of an executable in the system path.
Options:
-v, --version output the version number
-a, --all list all instances of executables found (instead of just the first one)
-s, --silent silence the output, just return the exit code (0 if any executable is found, otherwise 1)
-h, --help output usage informationFor example:
$ which --all nodeSee also
License
Which for JS is distributed under the Apache License, version 2.0.