which.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var fs = require('fs');
  2. var path = require('path');
  3. var cst = require('../../constants.js')
  4. // XP's system default value for `PATHEXT` system variable, just in case it's not
  5. // set on Windows.
  6. var XP_DEFAULT_PATHEXT = '.com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh';
  7. // For earlier versions of NodeJS that doesn't have a list of constants (< v6)
  8. var FILE_EXECUTABLE_MODE = 1;
  9. function statFollowLinks() {
  10. return fs.statSync.apply(fs, arguments);
  11. }
  12. function isWindowsPlatform() {
  13. return cst.IS_WINDOWS;
  14. }
  15. // Cross-platform method for splitting environment `PATH` variables
  16. function splitPath(p) {
  17. return p ? p.split(path.delimiter) : [];
  18. }
  19. // Tests are running all cases for this func but it stays uncovered by codecov due to unknown reason
  20. /* istanbul ignore next */
  21. function isExecutable(pathName) {
  22. try {
  23. // TODO(node-support): replace with fs.constants.X_OK once remove support for node < v6
  24. fs.accessSync(pathName, FILE_EXECUTABLE_MODE);
  25. } catch (err) {
  26. return false;
  27. }
  28. return true;
  29. }
  30. function checkPath(pathName) {
  31. return fs.existsSync(pathName) && !statFollowLinks(pathName).isDirectory()
  32. && (isWindowsPlatform() || isExecutable(pathName));
  33. }
  34. //@
  35. //@ ### which(command)
  36. //@
  37. //@ Examples:
  38. //@
  39. //@ ```javascript
  40. //@ var nodeExec = which('node');
  41. //@ ```
  42. //@
  43. //@ Searches for `command` in the system's `PATH`. On Windows, this uses the
  44. //@ `PATHEXT` variable to append the extension if it's not already executable.
  45. //@ Returns a [ShellString](#shellstringstr) containing the absolute path to
  46. //@ `command`.
  47. function _which(cmd) {
  48. if (!cmd) console.error('must specify command');
  49. var options = {}
  50. var isWindows = isWindowsPlatform();
  51. var pathArray = splitPath(process.env.PATH);
  52. var queryMatches = [];
  53. // No relative/absolute paths provided?
  54. if (cmd.indexOf('/') === -1) {
  55. // Assume that there are no extensions to append to queries (this is the
  56. // case for unix)
  57. var pathExtArray = [''];
  58. if (isWindows) {
  59. // In case the PATHEXT variable is somehow not set (e.g.
  60. // child_process.spawn with an empty environment), use the XP default.
  61. var pathExtEnv = process.env.PATHEXT || XP_DEFAULT_PATHEXT;
  62. pathExtArray = splitPath(pathExtEnv.toUpperCase());
  63. }
  64. // Search for command in PATH
  65. for (var k = 0; k < pathArray.length; k++) {
  66. // already found it
  67. if (queryMatches.length > 0 && !options.all) break;
  68. var attempt = path.resolve(pathArray[k], cmd);
  69. if (isWindows) {
  70. attempt = attempt.toUpperCase();
  71. }
  72. var match = attempt.match(/\.[^<>:"/|?*.]+$/);
  73. if (match && pathExtArray.indexOf(match[0]) >= 0) { // this is Windows-only
  74. // The user typed a query with the file extension, like
  75. // `which('node.exe')`
  76. if (checkPath(attempt)) {
  77. queryMatches.push(attempt);
  78. break;
  79. }
  80. } else { // All-platforms
  81. // Cycle through the PATHEXT array, and check each extension
  82. // Note: the array is always [''] on Unix
  83. for (var i = 0; i < pathExtArray.length; i++) {
  84. var ext = pathExtArray[i];
  85. var newAttempt = attempt + ext;
  86. if (checkPath(newAttempt)) {
  87. queryMatches.push(newAttempt);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. } else if (checkPath(cmd)) { // a valid absolute or relative path
  94. queryMatches.push(path.resolve(cmd));
  95. }
  96. if (queryMatches.length > 0) {
  97. return options.all ? queryMatches : queryMatches[0];
  98. }
  99. return options.all ? [] : null;
  100. }
  101. module.exports = _which;