UNPKG

3.58 kBJavaScriptView Raw
1
2var fs = require('fs');
3var path = require('path');
4var cst = require('../../constants.js')
5
6// XP's system default value for `PATHEXT` system variable, just in case it's not
7// set on Windows.
8var XP_DEFAULT_PATHEXT = '.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh';
9
10// For earlier versions of NodeJS that doesn't have a list of constants (< v6)
11var FILE_EXECUTABLE_MODE = 1;
12
13function statFollowLinks() {
14 return fs.statSync.apply(fs, arguments);
15}
16
17function isWindowsPlatform() {
18 return cst.IS_WINDOWS;
19}
20
21// Cross-platform method for splitting environment `PATH` variables
22function splitPath(p) {
23 return p ? p.split(path.delimiter) : [];
24}
25
26// Tests are running all cases for this func but it stays uncovered by codecov due to unknown reason
27/* istanbul ignore next */
28function isExecutable(pathName) {
29 try {
30 // TODO(node-support): replace with fs.constants.X_OK once remove support for node < v6
31 fs.accessSync(pathName, FILE_EXECUTABLE_MODE);
32 } catch (err) {
33 return false;
34 }
35 return true;
36}
37
38function checkPath(pathName) {
39 return fs.existsSync(pathName) && !statFollowLinks(pathName).isDirectory()
40 && (isWindowsPlatform() || isExecutable(pathName));
41}
42
43//@
44//@ ### which(command)
45//@
46//@ Examples:
47//@
48//@ ```javascript
49//@ var nodeExec = which('node');
50//@ ```
51//@
52//@ Searches for `command` in the system's `PATH`. On Windows, this uses the
53//@ `PATHEXT` variable to append the extension if it's not already executable.
54//@ Returns a [ShellString](#shellstringstr) containing the absolute path to
55//@ `command`.
56function _which(cmd) {
57 if (!cmd) console.error('must specify command');
58
59 var options = {}
60
61 var isWindows = isWindowsPlatform();
62 var pathArray = splitPath(process.env.PATH);
63
64 var queryMatches = [];
65
66 // No relative/absolute paths provided?
67 if (cmd.indexOf('/') === -1) {
68 // Assume that there are no extensions to append to queries (this is the
69 // case for unix)
70 var pathExtArray = [''];
71 if (isWindows) {
72 // In case the PATHEXT variable is somehow not set (e.g.
73 // child_process.spawn with an empty environment), use the XP default.
74 var pathExtEnv = process.env.PATHEXT || XP_DEFAULT_PATHEXT;
75 pathExtArray = splitPath(pathExtEnv.toUpperCase());
76 }
77
78 // Search for command in PATH
79 for (var k = 0; k < pathArray.length; k++) {
80 // already found it
81 if (queryMatches.length > 0 && !options.all) break;
82
83 var attempt = path.resolve(pathArray[k], cmd);
84
85 if (isWindows) {
86 attempt = attempt.toUpperCase();
87 }
88
89 var match = attempt.match(/\.[^<>:"/|?*.]+$/);
90 if (match && pathExtArray.indexOf(match[0]) >= 0) { // this is Windows-only
91 // The user typed a query with the file extension, like
92 // `which('node.exe')`
93 if (checkPath(attempt)) {
94 queryMatches.push(attempt);
95 break;
96 }
97 } else { // All-platforms
98 // Cycle through the PATHEXT array, and check each extension
99 // Note: the array is always [''] on Unix
100 for (var i = 0; i < pathExtArray.length; i++) {
101 var ext = pathExtArray[i];
102 var newAttempt = attempt + ext;
103 if (checkPath(newAttempt)) {
104 queryMatches.push(newAttempt);
105 break;
106 }
107 }
108 }
109 }
110 } else if (checkPath(cmd)) { // a valid absolute or relative path
111 queryMatches.push(path.resolve(cmd));
112 }
113
114 if (queryMatches.length > 0) {
115 return options.all ? queryMatches : queryMatches[0];
116 }
117 return options.all ? [] : null;
118}
119
120module.exports = _which;