-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathlocateNAN.js
63 lines (56 loc) · 1.3 KB
/
locateNAN.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
'use strict'
const fs = require('fs-extra')
const path = require('path')
const isNANModule = async function (dir) {
const h = path.join(dir, 'nan.h')
try {
const stat = await fs.stat(h)
return stat.isFile()
} catch (e) {
return false
}
}
async function isNodeJSProject(dir) {
const pjson = path.join(dir, 'package.json')
const node_modules = path.join(dir, 'node_modules')
try {
let stat = await fs.stat(pjson)
if (stat.isFile()) {
return true
}
stat = await fs.stat(node_modules)
if (stat.isDirectory()) {
return true
}
} catch (e) {
// Ignore
}
return false
}
const locateNAN = (module.exports = async function (projectRoot) {
if (locateNAN.__projectRoot) {
// Override for unit tests
projectRoot = locateNAN.__projectRoot
}
let result = await isNodeJSProject(projectRoot)
if (!result) {
return null
}
const nanModulePath = path.join(projectRoot, 'node_modules', 'nan')
result = await isNANModule(nanModulePath)
if (result) {
return nanModulePath
}
// Goto upper level:
return await locateNAN(goUp(projectRoot))
})
function goUp(dir) {
const items = dir.split(path.sep)
const scopeItem = items[items.length - 2]
if (scopeItem && scopeItem[0] === '@') {
// skip scope
dir = path.join(dir, '..')
}
dir = path.join(dir, '..', '..')
return path.normalize(dir)
}