"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/which/which.js" (8 Mar 2017, 3171 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 module.exports = which
    2 which.sync = whichSync
    3 
    4 var isWindows = process.platform === 'win32' ||
    5     process.env.OSTYPE === 'cygwin' ||
    6     process.env.OSTYPE === 'msys'
    7 
    8 var path = require('path')
    9 var COLON = isWindows ? ';' : ':'
   10 var isexe = require('isexe')
   11 var fs = require('fs')
   12 
   13 function getNotFoundError (cmd) {
   14   var er = new Error('not found: ' + cmd)
   15   er.code = 'ENOENT'
   16 
   17   return er
   18 }
   19 
   20 function getPathInfo (cmd, opt) {
   21   var colon = opt.colon || COLON
   22   var pathEnv = opt.path || process.env.PATH || ''
   23   var pathExt = ['']
   24 
   25   pathEnv = pathEnv.split(colon)
   26 
   27   var pathExtExe = ''
   28   if (isWindows) {
   29     pathEnv.unshift(process.cwd())
   30     pathExtExe = (opt.pathExt || process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM')
   31     pathExt = pathExtExe.split(colon)
   32 
   33 
   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] !== '')
   37       pathExt.unshift('')
   38   }
   39 
   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(/\\/))
   43     pathEnv = ['']
   44 
   45   return {
   46     env: pathEnv,
   47     ext: pathExt,
   48     extExe: pathExtExe
   49   }
   50 }
   51 
   52 function which (cmd, opt, cb) {
   53   if (typeof opt === 'function') {
   54     cb = opt
   55     opt = {}
   56   }
   57 
   58   var info = getPathInfo(cmd, opt)
   59   var pathEnv = info.env
   60   var pathExt = info.ext
   61   var pathExtExe = info.extExe
   62   var found = []
   63 
   64   ;(function F (i, l) {
   65     if (i === l) {
   66       if (opt.all && found.length)
   67         return cb(null, found)
   68       else
   69         return cb(getNotFoundError(cmd))
   70     }
   71 
   72     var pathPart = pathEnv[i]
   73     if (pathPart.charAt(0) === '"' && pathPart.slice(-1) === '"')
   74       pathPart = pathPart.slice(1, -1)
   75 
   76     var p = path.join(pathPart, cmd)
   77     if (!pathPart && (/^\.[\\\/]/).test(cmd)) {
   78       p = cmd.slice(0, 2) + p
   79     }
   80     ;(function E (ii, ll) {
   81       if (ii === ll) return F(i + 1, l)
   82       var ext = pathExt[ii]
   83       isexe(p + ext, { pathExt: pathExtExe }, function (er, is) {
   84         if (!er && is) {
   85           if (opt.all)
   86             found.push(p + ext)
   87           else
   88             return cb(null, p + ext)
   89         }
   90         return E(ii + 1, ll)
   91       })
   92     })(0, pathExt.length)
   93   })(0, pathEnv.length)
   94 }
   95 
   96 function whichSync (cmd, opt) {
   97   opt = opt || {}
   98 
   99   var info = getPathInfo(cmd, opt)
  100   var pathEnv = info.env
  101   var pathExt = info.ext
  102   var pathExtExe = info.extExe
  103   var found = []
  104 
  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)
  109 
  110     var p = path.join(pathPart, cmd)
  111     if (!pathPart && /^\.[\\\/]/.test(cmd)) {
  112       p = cmd.slice(0, 2) + p
  113     }
  114     for (var j = 0, ll = pathExt.length; j < ll; j ++) {
  115       var cur = p + pathExt[j]
  116       var is
  117       try {
  118         is = isexe.sync(cur, { pathExt: pathExtExe })
  119         if (is) {
  120           if (opt.all)
  121             found.push(cur)
  122           else
  123             return cur
  124         }
  125       } catch (ex) {}
  126     }
  127   }
  128 
  129   if (opt.all && found.length)
  130     return found
  131 
  132   throw getNotFoundError(cmd)
  133 }