1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 'use strict';
-
- const path = require('path');
- const which = require('which');
- const pathKey = require('path-key')();
-
- function resolveCommandAttempt(parsed, withoutPathExt) {
- const cwd = process.cwd();
- const hasCustomCwd = parsed.options.cwd != null;
-
-
-
- if (hasCustomCwd) {
- try {
- process.chdir(parsed.options.cwd);
- } catch (err) {
-
- }
- }
-
- let resolved;
-
- try {
- resolved = which.sync(parsed.command, {
- path: (parsed.options.env || process.env)[pathKey],
- pathExt: withoutPathExt ? path.delimiter : undefined,
- });
- } catch (e) {
-
- } finally {
- 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;
|