-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
67 lines (56 loc) · 1.47 KB
/
index.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
66
67
var fs = require("fs")
var path = require("path")
var node_modules = "node_modules"
function Found() {}
function search(dir, scope) {
dir = dir || "."
var context = path.resolve(dir)
if (!fs.existsSync(context)) return []
var contents = fs.readdirSync(context)
return contents.reduce(function (accumulated, name) {
var relative = path.join(dir, name)
var isScoped = name.charAt(0) === "@"
if (isScoped) return accumulated.concat(search(relative, name))
var found = new Found
found.name = scope ? scope + "/" + name : name
found.path = path.resolve(relative)
if (scope) found.scope = scope
if (is(relative)) found.link = read(relative)
accumulated.push(found)
return accumulated
}, [])
}
function read(p) {
return path.resolve(fs.readlinkSync(path.resolve(p)))
}
function pluck(found) {
return found[this]
}
function root(dir) {
return path.join(dir || ".", node_modules)
}
function roots(dir) {
return paths(dir).map(root)
}
function names(dir) {
return search(root(dir)).filter(is).map(pluck, "name")
}
function paths(dir) {
return search(root(dir)).filter(is).map(pluck, "path")
}
function links(dir) {
return search(root(dir)).filter(is).map(pluck, "link")
}
function is(p) {
if (p instanceof Found) return p.hasOwnProperty("link")
return fs.existsSync(p) && fs.lstatSync(p).isSymbolicLink()
}
module.exports = {
links: links,
names: names,
paths: paths,
roots: roots,
read: read,
is: is,
search: search
};