12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- '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;
|