.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / os-locale / node_modules / cross-spawn / lib / util / resolveCommand.js
1 'use strict';
2
3 const path = require('path');
4 const which = require('which');
5 const pathKey = require('path-key')();
6
7 function resolveCommandAttempt(parsed, withoutPathExt) {
8     const cwd = process.cwd();
9     const hasCustomCwd = parsed.options.cwd != null;
10
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
13     if (hasCustomCwd) {
14         try {
15             process.chdir(parsed.options.cwd);
16         } catch (err) {
17             /* Empty */
18         }
19     }
20
21     let resolved;
22
23     try {
24         resolved = which.sync(parsed.command, {
25             path: (parsed.options.env || process.env)[pathKey],
26             pathExt: withoutPathExt ? path.delimiter : undefined,
27         });
28     } catch (e) {
29         /* Empty */
30     } finally {
31         process.chdir(cwd);
32     }
33
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
36     if (resolved) {
37         resolved = path.resolve(hasCustomCwd ? parsed.options.cwd : '', resolved);
38     }
39
40     return resolved;
41 }
42
43 function resolveCommand(parsed) {
44     return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
45 }
46
47 module.exports = resolveCommand;