-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Find the instances of an executable in the system path, in JavaScript.
Install the latest version of Which for JS with npm package manager:
npm install @cedx/whichFor detailed instructions, see the installation guide.
This package provides the which(command: string) function, allowing to locate a command in the system path.
This function takes the name of the command to locate, and returns a ResultSet instance.
The ResultSet class implements the async iterable protocol.
It is therefore possible to iterate over the results using a for await...of loop:
import {which} from "@cedx/which";
import console from "node:console";
try {
console.log('The "foobar" command is available at these locations:');
for await (const path of which("foobar")) console.log(`- ${path}`);
}
catch (error) {
console.error(error instanceof Error ? error.message : error);
}The ResultSet class also provides two convenient properties:
-
all: get all instances of the searched command. -
first: get the first instance of the searched command.
The ResultSet.all property returns a Promise that resolves with the absolute paths of all instances of an executable found in the system path.
If the executable could not be located, the promise rejects.
import console from "node:console";
import {which} from "@cedx/which";
try {
const paths = await which("foobar").all;
console.log('The "foobar" command is available at these locations:');
for (const path of paths) console.log(`- ${path}`);
}
catch (error) {
console.error(error instanceof Error ? error.message : error);
}The ResultSet.first property returns a Promise that resolves with the absolute path of the first instance of an executable found in the system path.
If the executable could not be located, the promise rejects.
import console from "node:console";
import {which} from "@cedx/which";
try {
const path = await which("foobar").first;
console.log(`The "foobar" command is located at: ${path}`);
}
catch (error) {
console.error(error instanceof Error ? error.message : error);
}The behavior of the which(command: string, options?: FinderOptions) function can be customized using the following options.
An array of strings specifying the list of executable file extensions.
On Windows, defaults to the list of extensions provided by the PATHEXT environment variable.
which("foobar", {extensions: [".foo", ".exe", ".cmd"]});Note
The extensions option is only meaningful on the Windows platform,
where the executability of a file is determined from its extension.
An array of strings specifying the system paths from which the given command will be searched.
Defaults to the list of directories provided by the PATH environment variable.
which("foobar", {paths: ["/usr/local/bin", "/usr/bin"]});