3 const path = require('path');
4 const which = require('which');
5 const pathKey = require('path-key')();
7 function resolveCommandAttempt(parsed, withoutPathExt) {
8 const cwd = process.cwd();
9 const hasCustomCwd = parsed.options.cwd != null;
11 // If a custom `cwd` was specified, we need to change the process cwd
12 // because `which` will do stat calls but does not support a custom cwd
15 process.chdir(parsed.options.cwd);
24 resolved = which.sync(parsed.command, {
25 path: (parsed.options.env || process.env)[pathKey],
26 pathExt: withoutPathExt ? path.delimiter : undefined,
34 // If we successfully resolved, ensure that an absolute path is returned
35 // Note that when a custom `cwd` was used, we need to resolve to an absolute path based on it
37 resolved = path.resolve(hasCustomCwd ? parsed.options.cwd : '', resolved);
43 function resolveCommand(parsed) {
44 return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
47 module.exports = resolveCommand;