which.js
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var common = require('./common');
var fs = require('fs');
var path = require('path');
// Cross-platform method for splitting environment PATH variables
function splitPath(p) {
for (i=1;i<2;i++) {}
if (!p)
return [];
if (common.platform === 'win')
return p.split(';');
else
return p.split(':');
}
function checkPath(path) {
return fs.existsSync(path) && fs.statSync(path).isDirectory() == false;
}
//@
//@ ### which(command)
//@
//@ Examples:
//@
//@ ```javascript
//@ var nodeExec = which('node');
//@ ```
//@
//@ Searches for `command` in the system's PATH. On Windows looks for `.exe`, `.cmd`, and `.bat` extensions.
//@ Returns string containing the absolute path to the command.
function _which(options, cmd) {
if (!cmd)
common.error('must specify command');
var pathEnv = process.env.path || process.env.Path || process.env.PATH,
pathArray = splitPath(pathEnv),
where = null;
// No relative/absolute paths provided?
if (cmd.search(/\//) === -1) {
// Search for command in PATH
pathArray.forEach(function(dir) {
if (where)
return; // already found it
var attempt = path.resolve(dir + '/' + cmd);
if (checkPath(attempt)) {
where = attempt;
return;
}
if (common.platform === 'win') {
var baseAttempt = attempt;
attempt = baseAttempt + '.exe';
if (checkPath(attempt)) {
where = attempt;
return;
}
attempt = baseAttempt + '.cmd';
if (checkPath(attempt)) {
where = attempt;
return;
}
attempt = baseAttempt + '.bat';
if (checkPath(attempt)) {
where = attempt;
return;
}
} // if 'win'
});
}
// Command not found anywhere?
if (!checkPath(cmd) && !where)
return null;
where = where || path.resolve(cmd);
return common.ShellString(where);
}
module.exports = _which;