Sha256: e525fe739aba01cf5eee6473df449cdda93df6266effa8eae0fcfb9a4e6c5de6

Contents?: true

Size: 1.52 KB

Versions: 76

Compression:

Stored size: 1.52 KB

Contents

'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;
    // Worker threads do not have process.chdir()
    const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined && !process.chdir.disabled;

    // If a custom `cwd` was specified, we need to change the process cwd
    // because `which` will do stat calls but does not support a custom cwd
    if (shouldSwitchCwd) {
        try {
            process.chdir(parsed.options.cwd);
        } catch (err) {
            /* Empty */
        }
    }

    let resolved;

    try {
        resolved = which.sync(parsed.command, {
            path: env[getPathKey({ env })],
            pathExt: withoutPathExt ? path.delimiter : undefined,
        });
    } catch (e) {
        /* Empty */
    } finally {
        if (shouldSwitchCwd) {
            process.chdir(cwd);
        }
    }

    // If we successfully resolved, ensure that an absolute path is returned
    // Note that when a custom `cwd` was used, we need to resolve to an absolute path based on it
    if (resolved) {
        resolved = path.resolve(hasCustomCwd ? parsed.options.cwd : '', resolved);
    }

    return resolved;
}

function resolveCommand(parsed) {
    return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
}

module.exports = resolveCommand;

Version data entries

76 entries across 76 versions & 5 rubygems

Version Path
immosquare-cleaner-0.1.62 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.61 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.60 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.59 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.58 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.57 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.56 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.55 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.54 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.53 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.52 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.51 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.50 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.49 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.48 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.47 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.46 node_modules/cross-spawn/lib/util/resolveCommand.js
pcp-server-ruby-sdk-0.0.6 node_modules/cross-spawn/lib/util/resolveCommand.js
pcp-server-ruby-sdk-0.1.0 node_modules/cross-spawn/lib/util/resolveCommand.js
immosquare-cleaner-0.1.45 node_modules/cross-spawn/lib/util/resolveCommand.js