diff options
Diffstat (limited to 'deps/npm/node_modules/which/which.js')
-rw-r--r-- | deps/npm/node_modules/which/which.js | 148 |
1 files changed, 92 insertions, 56 deletions
diff --git a/deps/npm/node_modules/which/which.js b/deps/npm/node_modules/which/which.js index 2a45417da..13fc26dcf 100644 --- a/deps/npm/node_modules/which/which.js +++ b/deps/npm/node_modules/which/which.js @@ -1,54 +1,83 @@ module.exports = which which.sync = whichSync -var path = require("path") - , fs - , COLON = process.platform === "win32" ? ";" : ":" - , isExe - , fs = require("fs") +var isWindows = process.platform === 'win32' || + process.env.OSTYPE === 'cygwin' || + process.env.OSTYPE === 'msys' -if (process.platform == "win32") { +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) { - //console.error(mod, uid, gid); - //console.error("isExe?", (mod & 0111).toString(8)) var ret = (mod & 0001) || (mod & 0010) && process.getgid && gid === process.getgid() - || (mod & 0010) && process.getuid && 0 === process.getuid() || (mod & 0100) && process.getuid && uid === process.getuid() - || (mod & 0100) && process.getuid && 0 === process.getuid() - //console.error("isExe?", ret) + || (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 = [''] -function which (cmd, cb) { - if (isAbsolute(cmd)) return cb(null, cmd) - var pathEnv = (process.env.PATH || "").split(COLON) - , pathExt = [""] - if (process.platform === "win32") { - pathEnv.push(process.cwd()) - pathExt = (process.env.PATHEXT || ".EXE").split(COLON) - if (cmd.indexOf(".") !== -1) pathExt.unshift("") + // 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 + } + } } - //console.error("pathEnv", pathEnv) + + 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)) + 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] - //console.error(p + ext) fs.stat(p + ext, function (er, stat) { if (!er && - stat && stat.isFile() && isExe(stat.mode, stat.uid, stat.gid)) { - //console.error("yes, exe!", p + ext) return cb(null, p + ext) } return E(ii + 1, ll) @@ -57,45 +86,52 @@ function which (cmd, cb) { })(0, pathEnv.length) } -function whichSync (cmd) { - if (isAbsolute(cmd)) return cmd - var pathEnv = (process.env.PATH || "").split(COLON) - , pathExt = [""] - if (process.platform === "win32") { - pathEnv.push(process.cwd()) - pathExt = (process.env.PATHEXT || ".EXE").split(COLON) - if (cmd.indexOf(".") !== -1) pathExt.unshift("") +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) } catch (ex) {} - if (stat && - stat.isFile() && - isExe(stat.mode, stat.uid, stat.gid)) return cur + 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) -} - -var isAbsolute = process.platform === "win32" ? absWin : absUnix - -function absWin (p) { - if (absUnix(p)) return true - // pull off the device/UNC bit from a windows path. - // from node's lib/path.js - var splitDeviceRe = - /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?/ - , result = splitDeviceRe.exec(p) - , device = result[1] || '' - , isUnc = device && device.charAt(1) !== ':' - , isAbsolute = !!result[2] || isUnc // UNC paths are always absolute - - return isAbsolute -} -function absUnix (p) { - return p.charAt(0) === "/" || p === "" + throw new Error('not found: '+cmd) } |