Blame view

node_modules/shelljs/src/which.js 2.95 KB
aaac7fed   liuqimichale   add
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
  var common = require('./common');
  var fs = require('fs');
  var path = require('path');
  
  common.register('which', _which, {
    allowGlobbing: false,
    cmdOptions: {
      'a': 'all',
    },
  });
  
  // XP's system default value for PATHEXT system variable, just in case it's not
  // set on Windows.
  var XP_DEFAULT_PATHEXT = '.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh';
  
  // Cross-platform method for splitting environment PATH variables
  function splitPath(p) {
    return p ? p.split(path.delimiter) : [];
  }
  
  function checkPath(pathName) {
    return fs.existsSync(pathName) && !fs.statSync(pathName).isDirectory();
  }
  
  //@
  //@ ### which(command)
  //@
  //@ Examples:
  //@
  //@ ```javascript
  //@ var nodeExec = which('node');
  //@ ```
  //@
  //@ Searches for `command` in the system's PATH. On Windows, this uses the
  //@ `PATHEXT` variable to append the extension if it's not already executable.
  //@ Returns string containing the absolute path to the command.
  function _which(options, cmd) {
    if (!cmd) common.error('must specify command');
  
    var isWindows = process.platform === 'win32';
    var pathEnv = process.env.path || process.env.Path || process.env.PATH;
    var pathArray = splitPath(pathEnv);
  
    var queryMatches = [];
  
    // No relative/absolute paths provided?
    if (cmd.indexOf('/') === -1) {
      // Assume that there are no extensions to append to queries (this is the
      // case for unix)
      var pathExtArray = [''];
      if (isWindows) {
        // In case the PATHEXT variable is somehow not set (e.g.
        // child_process.spawn with an empty environment), use the XP default.
        var pathExtEnv = process.env.PATHEXT || XP_DEFAULT_PATHEXT;
        pathExtArray = splitPath(pathExtEnv.toUpperCase());
      }
  
      // Search for command in PATH
      for (var k = 0; k < pathArray.length; k++) {
        // already found it
        if (queryMatches.length > 0 && !options.all) break;
  
        var attempt = path.resolve(pathArray[k], cmd);
  
        if (isWindows) {
          attempt = attempt.toUpperCase();
        }
  
        var match = attempt.match(/\.[^<>:"/\|?*.]+$/);
        if (match && pathExtArray.indexOf(match[0]) >= 0) { // this is Windows-only
          // The user typed a query with the file extension, like
          // `which('node.exe')`
          if (checkPath(attempt)) {
            queryMatches.push(attempt);
            break;
          }
        } else { // All-platforms
          // Cycle through the PATHEXT array, and check each extension
          // Note: the array is always [''] on Unix
          for (var i = 0; i < pathExtArray.length; i++) {
            var ext = pathExtArray[i];
            var newAttempt = attempt + ext;
            if (checkPath(newAttempt)) {
              queryMatches.push(newAttempt);
              break;
            }
          }
        }
      }
    } else if (checkPath(cmd)) { // a valid absolute or relative path
      queryMatches.push(path.resolve(cmd));
    }
  
    if (queryMatches.length > 0) {
      return options.all ? queryMatches : queryMatches[0];
    }
    return options.all ? [] : null;
  }
  module.exports = _which;