Skip to content

Commit 6fe4a28

Browse files
committed
Unified and simplified checking functions at has_lib.js
1 parent 09f2f31 commit 6fe4a28

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

util/has_lib.js

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var childProcess = require('child_process')
1+
var execSync = require('child_process').execSync
22
var fs = require('fs')
33

44
var SYSTEM_PATHS = [
@@ -10,6 +10,16 @@ var SYSTEM_PATHS = [
1010
'/usr/lib/i386-linux-gnu'
1111
]
1212

13+
function _hasQuery (query) {
14+
try {
15+
// execSync throws on nonzero exit
16+
execSync(query)
17+
return true
18+
} catch (err) {
19+
return false
20+
}
21+
}
22+
1323
/**
1424
* Checks for lib using ldconfig if present, or searching SYSTEM_PATHS
1525
* otherwise.
@@ -18,21 +28,15 @@ var SYSTEM_PATHS = [
1828
*/
1929
function hasSystemLib (lib) {
2030
var libName = 'lib' + lib + '.+(so|dylib)'
21-
var libNameRegex = new RegExp(libName)
2231

2332
// Try using ldconfig on linux systems
24-
if (hasLdconfig()) {
25-
try {
26-
if (childProcess.execSync('ldconfig -p 2>/dev/null | grep -E "' +
27-
libName + '"').length) {
28-
return true
29-
}
30-
} catch (err) {
31-
// noop -- proceed to other search methods
32-
}
33+
if (_hasQuery('ldconfig -p 2>/dev/null | grep -E "' + libName + '"')) {
34+
return true
3335
}
3436

35-
// Try checking common library locations
37+
// Try checking common library locations
38+
var libNameRegex = new RegExp(libName)
39+
3640
return SYSTEM_PATHS.some(function (systemPath) {
3741
try {
3842
var dirListing = fs.readdirSync(systemPath)
@@ -45,37 +49,13 @@ function hasSystemLib (lib) {
4549
})
4650
}
4751

48-
/**
49-
* Checks for ldconfig on the path and /sbin
50-
* @return Boolean exists
51-
*/
52-
function hasLdconfig () {
53-
try {
54-
// Add /sbin to path as ldconfig is located there on some systems -- e.g.
55-
// Debian (and it can still be used by unprivileged users):
56-
childProcess.execSync('export PATH="$PATH:/sbin"', {stdio: 'ignore'})
57-
process.env.PATH = '...'
58-
// execSync throws on nonzero exit
59-
childProcess.execSync('hash ldconfig 2>/dev/null')
60-
return true
61-
} catch (err) {
62-
return false
63-
}
64-
}
65-
6652
/**
6753
* Checks for freetype2 with --cflags-only-I
6854
* @return Boolean exists
6955
*/
7056
function hasFreetype () {
71-
try {
72-
if (childProcess.execSync('pkg-config cairo --cflags-only-I 2>/dev/null | ' +
73-
'grep freetype2').length) {
74-
return true
75-
}
76-
} catch (err) {
77-
return false
78-
}
57+
return _hasQuery('pkg-config cairo --cflags-only-I 2>/dev/null | ' +
58+
'grep freetype2')
7959
}
8060

8161
/**
@@ -84,13 +64,7 @@ function hasFreetype () {
8464
* @return Boolean exists
8565
*/
8666
function hasPkgconfigLib (lib) {
87-
try {
88-
// execSync throws on nonzero exit
89-
childProcess.execSync('pkg-config --exists "' + lib + '" 2>/dev/null')
90-
return true
91-
} catch (err) {
92-
return false
93-
}
67+
return _hasQuery('pkg-config --exists "' + lib + '" 2>/dev/null')
9468
}
9569

9670
function main (query) {

0 commit comments

Comments
 (0)