-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
69 lines (56 loc) · 1.37 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
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const fs = require('fs');
const path = require('path');
let bashPath;
/**
* Default paths to search
*/
const DEFAULT_PATHS = [
'/bin/bash',
'/sbin/bash',
'/usr/bin/bash',
'/usr/local/bin/bash',
'/usr/local/sbin/bash',
'/usr/sbin/bash',
'/usr/X11/bin/bash',
'/opt/local/bin',
'/opt/local/sbin',
'bash'
];
module.exports = paths => {
if (bashPath !== void 0) return bashPath;
const pathList = getPaths(paths || DEFAULT_PATHS);
for (let i = 0; i < pathList.length; i++) {
let filepath = path.resolve(pathList[i], 'bash');
if (fs.existsSync(filepath)) {
bashPath = filepath;
break;
}
if (process.platform === 'win32') {
if (fs.existsSync(filepath + '.cmd')) {
bashPath = filepath + '.cmd';
break;
}
if (fs.existsSync(filepath + '.exe')) {
bashPath = filepath + '.exe';
break;
}
if (fs.existsSync(filepath + '.bat')) {
bashPath = filepath + '.bat';
break;
}
}
}
// Set to null to avoid checking again later if path was not found
if (!bashPath) {
bashPath = null;
}
return bashPath;
};
function getPaths(paths) {
const compare = val => /\/use?r\//i.test(val) ? -1 : 1;
const list = paths.concat(process.env.PATH.split(path.delimiter));
const unique = [...new Set(list)];
unique.sort(compare);
return unique;
}