-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexecutables.js
More file actions
73 lines (66 loc) · 2.24 KB
/
executables.js
File metadata and controls
73 lines (66 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @overview Provides functionality related to working with executables.
* @license MPL-2.0
*/
import * as path from "node:path";
import { hasOwn } from "./reflection.js";
/**
* Build error messages for when executables cannot be found.
*
* @param {string} executable The executable being looked up.
* @returns {string} The executable not found error message.
*/
function notFoundError(executable) {
return `No executable could be found for ${executable}`;
}
/**
* Resolves the location of an executable given an arbitrary valid string
* representation of that executable.
*
* To obtain the location of the executable this function (if necessary):
* - Expands the provided string to an absolute path.
* - Follows symbolic links.
*
* @param {object} args The arguments for this function.
* @param {Object<string, string>} args.env The environment variables.
* @param {string} args.executable A string representation of the executable.
* @param {object} deps The dependencies for this function.
* @param {function(): boolean} deps.exists A function to check if a file exists.
* @param {function(): string} deps.readlink A function to resolve (sym)links.
* @param {function(): string} deps.which A function to perform a `which(1)`-like lookup.
* @returns {string} The full path to the binary of the executable.
* @throws {Error} If the executable could not be found.
*/
export function resolveExecutable(
{ env, executable },
{ exists, readlink, which },
) {
let resolved = executable;
try {
const path = hasOwn(env, "PATH")
? env.PATH
: hasOwn(env, "Path")
? env.Path
: undefined;
resolved = which(resolved, { path });
} catch {
throw new Error(notFoundError(executable));
}
if (!exists(resolved)) {
throw new Error(notFoundError(executable));
}
try {
const seen = {};
while (!hasOwn(seen, resolved)) {
seen[resolved] = null;
const link = readlink(resolved);
const base = path.dirname(resolved);
resolved = path.resolve(base, link);
}
} catch {
// An error is thrown if the argument is not a (sym)link, this is what we
// want so we return.
return resolved;
}
throw new Error(`${executable} points to a link loop, cannot resolve shell`);
}