-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflatten.js
93 lines (87 loc) · 2.2 KB
/
flatten.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
91
92
93
/**
* Converts npm registry `byField` response into serialized braph
*/
var readJSON = require('./lib/readJSON');
var packages = readJSON(process.argv[2] || 'byField').rows;
var nodeToPosition = getNodePositions(process.argv[3] || '60.pos');
var wantTests = process.argv[4];
var nodes = [];
var nodeIdToIdx = {};
var haveTests = [];
packages.forEach(function (pkg, idx) {
var pos = nodeToPosition[pkg.id];
if (!pos) {
throw new Error('Unknown node: ', pkg.id);
}
var pkgInfo = pkg.value;
var nodeInfo = {
id: pkg.id,
pos: pos
};
var license = getLicense(pkgInfo.license);
if (license) {
nodeInfo.l = license;
}
var author = getAuthor(pkgInfo.author);
if (author) {
nodeInfo.a = author;
}
if (hasTests(pkgInfo.scripts && pkgInfo.scripts.test)) {
haveTests.push(idx);
}
nodes.push(nodeInfo);
nodeIdToIdx[pkg.id] = idx;
});
packages.forEach(function (pkg, srcIdx) {
var deps = pkg.value.dependencies;
if (deps) {
var resolvedIds = [];
for(var key in deps) {
if (deps.hasOwnProperty(key)) {
var idx = nodeIdToIdx[key];
if (typeof idx !== 'undefined') {
resolvedIds.push(idx);
}
}
}
if (resolvedIds.length) {
nodes[srcIdx].d = resolvedIds;
}
}
});
if (wantTests) {
console.log(JSON.stringify(haveTests));
} else {
console.log(JSON.stringify(nodes));
}
function getNodePositions(fileName) {
var positions = readJSON(fileName);
var nodeToPosition = {};
positions.forEach(function (descriptor) {
nodeToPosition[descriptor.node] = descriptor.pos;
});
return nodeToPosition;
}
function getLicense(license) {
if (!license) return;
if (typeof license === 'string') {
return license.toUpperCase();
} else if (license[0]) {
return getLicense(license[0]);
} else {
var str = license.type || license.name || license.license;
if (typeof str === 'string') {
return str.toUpperCase();
}
}
}
function getAuthor(author) {
if (!author) return;
if (typeof author === 'string') return author;
if (typeof author.name === 'string') return author.name;
}
function hasTests(test) {
if (typeof test === 'string') {
return test.indexOf('no test specified') === -1;
}
}