-
Notifications
You must be signed in to change notification settings - Fork 19
/
macOS-lib-path-finder.js
65 lines (54 loc) · 1.53 KB
/
macOS-lib-path-finder.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
'use strict'
const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const {stat} = require('fs').promises
const path = require('path')
const LIBRARY = 'libgs.dylib'
const SYSTEM_PATH = '/usr/local/lib'
const isLibraryExists = async (libraryPath) => {
try {
await stat(libraryPath)
return true
} catch (err) {
return false
}
}
const isInstalled = async (command) => {
try {
await exec(`which ${command}`)
return true
} catch (err) {
return false
}
}
async function main() {
try {
let isGSExists = false
isGSExists = await isLibraryExists(path.join(SYSTEM_PATH, LIBRARY))
if (isGSExists) {
process.stdout.write(SYSTEM_PATH)
return
} else {
if (!await isInstalled('brew')) {
return 1
}
if (!await isInstalled('gs')) {
return 1
}
const cellarPath = (await exec('brew --cellar')).stdout
const gsVersion = (await exec('gs --version')).stdout
const gsLibraryPath = path.join(cellarPath.trim(), 'ghostscript', gsVersion.trim(), 'lib')
isGSExists = await isLibraryExists(path.join(gsLibraryPath, LIBRARY))
if (isGSExists) {
process.stdout.write(gsLibraryPath)
return
} else {
return 1
}
}
} catch (err) {
process.emitWarning(err)
return 1
}
}
main()