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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
module.exports = which
which.sync = whichSync
var isWindows = process.platform === 'win32' ||
process.env.OSTYPE === 'cygwin' ||
process.env.OSTYPE === 'msys'
var path = require('path')
var COLON = isWindows ? ';' : ':'
var isExe
var fs = require('fs')
var isAbsolute = require('is-absolute')
if (isWindows) {
// On windows, there is no good way to check that a file is executable
isExe = function isExe () { return true }
} else {
isExe = function isExe (mod, uid, gid) {
var ret = (mod & 0001)
|| (mod & 0010) && process.getgid && gid === process.getgid()
|| (mod & 0100) && process.getuid && uid === process.getuid()
|| (mod & 0110) && process.getuid && 0 === process.getuid()
if (process.getgroups && (mod & 0010)) {
var groups = process.getgroups()
for (var g = 0; g < groups.length; g++) {
if (groups[g] === gid)
return true
}
}
return ret
}
}
function which (cmd, opt, cb) {
if (typeof opt === 'function') {
cb = opt
opt = {}
}
var colon = opt.colon || COLON
var pathEnv = opt.path || process.env.PATH || ''
var pathExt = ['']
// On windows, env.Path is common.
if (isWindows && !pathEnv) {
var k = Object.keys(process.env)
for (var p = 0; p < k.length; p++) {
if (p.toLowerCase() === 'path') {
pathEnv = process.env[p]
break
}
}
}
pathEnv = pathEnv.split(colon)
if (isWindows) {
pathEnv.unshift(process.cwd())
pathExt = (opt.pathExt || process.env.PATHEXT || '.EXE').split(colon)
if (cmd.indexOf('.') !== -1 && pathExt[0] !== '')
pathExt.unshift('')
}
// If it's absolute, then we don't bother searching the pathenv.
// just check the file itself, and that's it.
if (isAbsolute(cmd))
pathEnv = ['']
;(function F (i, l) {
if (i === l) return cb(new Error('not found: '+cmd))
var p = path.resolve(pathEnv[i], cmd)
;(function E (ii, ll) {
if (ii === ll) return F(i + 1, l)
var ext = pathExt[ii]
fs.stat(p + ext, function (er, stat) {
if (!er &&
stat.isFile() &&
isExe(stat.mode, stat.uid, stat.gid)) {
return cb(null, p + ext)
}
return E(ii + 1, ll)
})
})(0, pathExt.length)
})(0, pathEnv.length)
}
function whichSync (cmd, opt) {
if (!opt)
opt = {}
var colon = opt.colon || COLON
var pathEnv = opt.path || process.env.PATH || ''
var pathExt = ['']
// On windows, env.Path is common.
if (isWindows && !pathEnv) {
var k = Object.keys(process.env)
for (var p = 0; p < k.length; p++) {
if (p.toLowerCase() === 'path') {
pathEnv = process.env[p]
break
}
}
}
pathEnv = pathEnv.split(colon)
if (isWindows) {
pathEnv.unshift(process.cwd())
pathExt = (opt.pathExt || process.env.PATHEXT || '.EXE').split(colon)
if (cmd.indexOf('.') !== -1 && pathExt[0] !== '')
pathExt.unshift('')
}
// If it's absolute, then we don't bother searching the pathenv.
// just check the file itself, and that's it.
if (isAbsolute(cmd))
pathEnv = ['']
for (var i = 0, l = pathEnv.length; i < l; i ++) {
var p = path.join(pathEnv[i], cmd)
for (var j = 0, ll = pathExt.length; j < ll; j ++) {
var cur = p + pathExt[j]
var stat
try {
stat = fs.statSync(cur)
if (stat.isFile() && isExe(stat.mode, stat.uid, stat.gid))
return cur
} catch (ex) {}
}
}
throw new Error('not found: '+cmd)
}
|