mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
which
This commit is contained in:
parent
715c79238e
commit
02df7985fd
62
shell.js
62
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}
|
||||
|
||||
66
src/which.js
Normal file
66
src/which.js
Normal file
@ -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;
|
||||
Loading…
x
Reference in New Issue
Block a user