forked from hyperboria/peers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (80 loc) · 2.82 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var Fs = require("fs"),
Package = require("./package.json"),
version = module.exports.version = Package.version,
Path = require("path"),
DIRNAME = __dirname,
read = function (p) {
var content = Fs.readFileSync(p, 'utf-8');
if (content.charAt(content.length - 1) !== '\n') {
throw new Error("file at " + p + " did not end with a newline character");
}
return JSON.parse(content);
},
P = module.exports.peers = (function () {
var pathFromArray = function (A) {
return A.reduce(function (a, b) { return Path.join(a, b); }, '');
};
var isDir = function (fullPath) {
return Fs.lstatSync(fullPath).isDirectory();
};
var getDir = function (A, f) {
var p = pathFromArray(A);
return Fs.readdirSync(p).filter(function (name) {
var fullPath = pathFromArray([p, name]);
return f(fullPath, A, name);
});
};
var find = function (map, path) {
/* safely search for nested values in an object via a path */
return (map && path.reduce(function (p, n) {
return typeof p[n] !== 'undefined' && p[n];
}, map)) || undefined;
}
var peers = {};
var walk = function (A) {
getDir(A, function (fullPath, A, name) {
if (/^\./.test(name)) {
// ignore hidden files
} else if (isDir(fullPath)) {
find(peers, A.slice(1))[name] = {};
walk(A.concat(name));
} else if (/\.k$/.test(name)) {
var obj = find(peers, A.slice(1))[name] = read(fullPath);
// embed the location in the object
obj.location = A.slice(1);
}
});
};
walk([DIRNAME]);
return peers;
}()),
map = module.exports.map = function (f) {
var L = [];
// t/f is the object a credential
var isCred = function (k) {
// creds end in .k
return /\.k/.test(k);
};
var walk = function (o, p, f) {
// walk the tree of objects
if (typeof(o) === 'object') {
// for each key in o, walk the key
Object.keys(o).forEach(function (k) {
var path = p.slice(0).concat(k);
if (isCred(k)) {
L.push(f(o[k], path));
}
walk(o[k], path, f);
});
}
};
walk(P, [], f);
return L;
},
filter = module.exports.filter = function (f) {
var L = [];
map(function (x, p) {
if (f(x,p)) { L.push(x); }
});
return L;
};