|
'use strict'; |
|
|
|
const path = require('path'); |
|
const which = require('which'); |
|
const getPathKey = require('path-key'); |
|
|
|
function resolveCommandAttempt(parsed, withoutPathExt) { |
|
const env = parsed.options.env || process.env; |
|
const cwd = process.cwd(); |
|
const hasCustomCwd = parsed.options.cwd != null; |
|
|
|
const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined && !process.chdir.disabled; |
|
|
|
|
|
|
|
if (shouldSwitchCwd) { |
|
try { |
|
process.chdir(parsed.options.cwd); |
|
} catch (err) { |
|
|
|
} |
|
} |
|
|
|
let resolved; |
|
|
|
try { |
|
resolved = which.sync(parsed.command, { |
|
path: env[getPathKey({ env })], |
|
pathExt: withoutPathExt ? path.delimiter : undefined, |
|
}); |
|
} catch (e) { |
|
|
|
} finally { |
|
if (shouldSwitchCwd) { |
|
process.chdir(cwd); |
|
} |
|
} |
|
|
|
|
|
|
|
if (resolved) { |
|
resolved = path.resolve(hasCustomCwd ? parsed.options.cwd : '', resolved); |
|
} |
|
|
|
return resolved; |
|
} |
|
|
|
function resolveCommand(parsed) { |
|
return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true); |
|
} |
|
|
|
module.exports = resolveCommand; |
|
|