From 02df7985fd6f643cb4ff48da10d7fe4c01c89934 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Sun, 25 Aug 2013 16:23:31 -0400 Subject: [PATCH] which --- shell.js | 62 +----------------------------------------------- src/which.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 src/which.js diff --git a/shell.js b/shell.js index 02665c7..5ef68b2 100644 --- a/shell.js +++ b/shell.js @@ -268,56 +268,7 @@ exports.grep = wrap('grep', _grep); //@ //@ Searches for `command` in the system's PATH. On Windows looks for `.exe`, `.cmd`, and `.bat` extensions. //@ Returns string containing the absolute path to the command. -function _which(options, cmd) { - if (!cmd) - error('must specify command'); - - var pathEnv = process.env.path || process.env.Path || process.env.PATH, - pathArray = splitPath(pathEnv), - where = null; - - // No relative/absolute paths provided? - if (cmd.search(/\//) === -1) { - // Search for command in PATH - pathArray.forEach(function(dir) { - if (where) - return; // already found it - - var attempt = path.resolve(dir + '/' + cmd); - if (fs.existsSync(attempt)) { - where = attempt; - return; - } - - if (platform === 'win') { - var baseAttempt = attempt; - attempt = baseAttempt + '.exe'; - if (fs.existsSync(attempt)) { - where = attempt; - return; - } - attempt = baseAttempt + '.cmd'; - if (fs.existsSync(attempt)) { - where = attempt; - return; - } - attempt = baseAttempt + '.bat'; - if (fs.existsSync(attempt)) { - where = attempt; - return; - } - } // if 'win' - }); - } - - // Command not found anywhere? - if (!fs.existsSync(cmd) && !where) - return null; - - where = where || path.resolve(cmd); - - return ShellString(where); -} +var _which = require('./src/which'); exports.which = wrap('which', _which); //@ @@ -1085,17 +1036,6 @@ function execSync(cmd, opts) { return obj; } // execSync() -// Cross-platform method for splitting environment PATH variables -function splitPath(p) { - if (!p) - return []; - - if (platform === 'win') - return p.split(';'); - else - return p.split(':'); -} - // extend(target_obj, source_obj1 [, source_obj2 ...]) // Shallow extend, e.g.: // extend({A:1}, {b:2}, {c:3}) returns {A:1, b:2, c:3} diff --git a/src/which.js b/src/which.js new file mode 100644 index 0000000..61b9f62 --- /dev/null +++ b/src/which.js @@ -0,0 +1,66 @@ +var common = require('./common'); +var fs = require('fs'); +var path = require('path'); + +// Cross-platform method for splitting environment PATH variables +function splitPath(p) { + if (!p) + return []; + + if (common.platform === 'win') + return p.split(';'); + else + return p.split(':'); +} + +function _which(options, cmd) { + if (!cmd) + common.error('must specify command'); + + var pathEnv = process.env.path || process.env.Path || process.env.PATH, + pathArray = splitPath(pathEnv), + where = null; + + // No relative/absolute paths provided? + if (cmd.search(/\//) === -1) { + // Search for command in PATH + pathArray.forEach(function(dir) { + if (where) + return; // already found it + + var attempt = path.resolve(dir + '/' + cmd); + if (fs.existsSync(attempt)) { + where = attempt; + return; + } + + if (common.platform === 'win') { + var baseAttempt = attempt; + attempt = baseAttempt + '.exe'; + if (fs.existsSync(attempt)) { + where = attempt; + return; + } + attempt = baseAttempt + '.cmd'; + if (fs.existsSync(attempt)) { + where = attempt; + return; + } + attempt = baseAttempt + '.bat'; + if (fs.existsSync(attempt)) { + where = attempt; + return; + } + } // if 'win' + }); + } + + // Command not found anywhere? + if (!fs.existsSync(cmd) && !where) + return null; + + where = where || path.resolve(cmd); + + return common.ShellString(where); +} +module.exports = _which;