-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer.js
50 lines (43 loc) · 1.17 KB
/
peer.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
const path = require('path')
module.exports.base_from_pkg = (pkg) => {
let max_peer_depths = Infinity
let offset = ''
require_debugs = (extra_path, base_path = process.cwd()) => {
const current_path = path.join(base_path, extra_path)
const pkg = require(current_path)
if (pkg.debugs) {
const list = []
pkg.debugs.forEach(e => {
if (e.endsWith('.json')) {
// recursively
list.push(...require_debugs(e, path.dirname(current_path)))
} else {
list.push(current_path)
}
})
return list
} else {
return []
}
}
const list = require_debugs(pkg)
list.forEach(e => {
let l = e.split(path.sep).length
if (max_peer_depths > l) {
offset = path.dirname(e)
max_peer_depths = l
}
})
return offset
}
module.exports.max_depths_from_pkg = (pkg) => {
let max_peer_depths = 0
pkg.debugs.forEach(e => {
if (max_peer_depths > e.split(path.sep).length)
max_peer_depths = Math.max(max_peer_depths, e.split(path.sep).length)
})
return max_peer_depths
}
module.exports.offset_from_depths = (depths) => {
return Array(depths).fill('..').join(path.sep)
}