4 var isWindows = process.platform === 'win32' ||
5 process.env.OSTYPE === 'cygwin' ||
6 process.env.OSTYPE === 'msys'
8 var path = require('path')
9 var COLON = isWindows ? ';' : ':'
10 var isexe = require('isexe')
11 var fs = require('fs')
13 function getNotFoundError (cmd) {
14 var er = new Error('not found: ' + cmd)
20 function getPathInfo (cmd, opt) {
21 var colon = opt.colon || COLON
22 var pathEnv = opt.path || process.env.PATH || ''
25 pathEnv = pathEnv.split(colon)
29 pathEnv.unshift(process.cwd())
30 pathExtExe = (opt.pathExt || process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM')
31 pathExt = pathExtExe.split(colon)
34 // Always test the cmd itself first. isexe will check to make sure
35 // it's found in the pathExt set.
36 if (cmd.indexOf('.') !== -1 && pathExt[0] !== '')
40 // If it has a slash, then we don't bother searching the pathenv.
41 // just check the file itself, and that's it.
42 if (cmd.match(/\//) || isWindows && cmd.match(/\\/))
52 function which (cmd, opt, cb) {
53 if (typeof opt === 'function') {
58 var info = getPathInfo(cmd, opt)
59 var pathEnv = info.env
60 var pathExt = info.ext
61 var pathExtExe = info.extExe
66 if (opt.all && found.length)
67 return cb(null, found)
69 return cb(getNotFoundError(cmd))
72 var pathPart = pathEnv[i]
73 if (pathPart.charAt(0) === '"' && pathPart.slice(-1) === '"')
74 pathPart = pathPart.slice(1, -1)
76 var p = path.join(pathPart, cmd)
77 if (!pathPart && (/^\.[\\\/]/).test(cmd)) {
78 p = cmd.slice(0, 2) + p
80 ;(function E (ii, ll) {
81 if (ii === ll) return F(i + 1, l)
83 isexe(p + ext, { pathExt: pathExtExe }, function (er, is) {
88 return cb(null, p + ext)
96 function whichSync (cmd, opt) {
99 var info = getPathInfo(cmd, opt)
100 var pathEnv = info.env
101 var pathExt = info.ext
102 var pathExtExe = info.extExe
105 for (var i = 0, l = pathEnv.length; i < l; i ++) {
106 var pathPart = pathEnv[i]
107 if (pathPart.charAt(0) === '"' && pathPart.slice(-1) === '"')
108 pathPart = pathPart.slice(1, -1)
110 var p = path.join(pathPart, cmd)
111 if (!pathPart && /^\.[\\\/]/.test(cmd)) {
112 p = cmd.slice(0, 2) + p
114 for (var j = 0, ll = pathExt.length; j < ll; j ++) {
115 var cur = p + pathExt[j]
118 is = isexe.sync(cur, { pathExt: pathExtExe })
129 if (opt.all && found.length)
132 throw getNotFoundError(cmd)