Skip to content

Commit 7ac433e

Browse files
committed
find the libgs path on macOS.
1 parent a6e4d97 commit 7ac433e

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

binding.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"GS4JS_LIB%": "<!(echo $GS4JS_LIB)",
9494
"conditions": [
9595
['"<!(echo $GS4JS_HOME)" == ""', {
96-
"GS4JS_HOME%": "/usr/local/lib"
96+
"GS4JS_HOME%": "<!(node macOS-lib-path-finder.js)"
9797
}],
9898
['"<!(echo $GS4JS_LIB)" == ""', {
9999
"GS4JS_LIB%": "libgs.dylib"

macOS-lib-path-finder.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
const { promisify } = require('util')
4+
const exec = promisify(require('child_process').exec)
5+
const {stat} = require('fs').promises
6+
const path = require('path')
7+
8+
9+
const LIBRARY = 'libgs.dylib'
10+
const SYSTEM_PATH = '/usr/local/lib'
11+
12+
const isLibraryExists = async (libraryPath) => {
13+
try {
14+
await stat(libraryPath)
15+
return true
16+
} catch (err) {
17+
return false
18+
}
19+
}
20+
21+
const isInstalled = async (command) => {
22+
try {
23+
await exec(`which ${command}`)
24+
return true
25+
} catch (err) {
26+
return false
27+
}
28+
29+
}
30+
31+
32+
async function main() {
33+
try {
34+
let isGSExists = false
35+
isGSExists = await isLibraryExists(path.join(SYSTEM_PATH, LIBRARY))
36+
if (isGSExists) {
37+
process.stdout.write(SYSTEM_PATH)
38+
return
39+
} else {
40+
if (!await isInstalled('brew')) {
41+
return 1
42+
}
43+
if (!await isInstalled('gs')) {
44+
return 1
45+
}
46+
const cellarPath = (await exec('brew --cellar')).stdout
47+
const gsVersion = (await exec('gs --version')).stdout
48+
const gsLibraryPath = path.join(cellarPath.trim(), 'ghostscript', gsVersion.trim(), 'lib')
49+
isGSExists = await isLibraryExists(path.join(gsLibraryPath, LIBRARY))
50+
51+
if (isGSExists) {
52+
process.stdout.write(gsLibraryPath)
53+
return
54+
} else {
55+
return 1
56+
}
57+
}
58+
} catch (err) {
59+
process.emitWarning(err)
60+
return 1
61+
}
62+
63+
}
64+
65+
main()

0 commit comments

Comments
 (0)