-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
56 lines (47 loc) · 1.31 KB
/
index.js
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
import {execa} from 'execa';
import findVersions from 'find-versions';
const oneMegabyte = 1000 * 1000;
const knownBinaryArguments = new Map([
...[
'ffmpeg',
'ffprobe',
'ffplay',
].map(name => [name, ['-version']]),
['openssl', ['version']],
]);
const defaultPossibleArguments = [
['--version'],
['version'],
];
export default async function binaryVersion(binary, options = {}) {
let possibleArguments;
if (options.args === undefined) {
const customArguments = knownBinaryArguments.get(binary);
possibleArguments = customArguments === undefined ? defaultPossibleArguments : [customArguments];
} else {
possibleArguments = [options.args];
}
for (const arguments_ of possibleArguments) {
try {
// eslint-disable-next-line no-await-in-loop
const {all} = await execa(binary, arguments_, {
all: true,
maxBuffer: oneMegabyte,
});
const [version] = findVersions(all, {loose: true});
if (version !== undefined) {
return version;
}
} catch (error) {
if (error.code === 'ENOENT') {
const newError = new Error(`Couldn't find the \`${binary}\` binary. Make sure it's installed and in your $PATH.`);
newError.sourceError = error;
throw newError;
}
if (error.code === 'EACCES') {
throw error;
}
}
}
throw new Error(`Couldn't find version of \`${binary}\``);
}