-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
64 lines (48 loc) · 1.43 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
"use strict";
var format = require('util').format;
var EOL = require('os').EOL;
var NODE_NAME_PREFIX = "N_";
/**
*
* @param textContent {String}
* @param outputStream {WritableStream}
*/
module.exports = function (textContent, outputStream) {
if (typeof textContent !== "string") {
throw new Error('Param jsonTree must be a string');
}
if (!outputStream) {
outputStream = process.stdout;
}
// generate id for each module
var id = 0;
var tree = JSON.parse(textContent);
/**
* recursive
*
* @param tree {Object}
* @param prev {String}
* @param level {Number}
*
*/
function show(tree, prev, level) {
level = level + 1;
if (tree) {
//FIXME: prototype check
for (var i in tree.dependencies) {
if (tree.dependencies.hasOwnProperty(i)) {
id++;
var nodeName = NODE_NAME_PREFIX + id;
var child = i + '@' + tree.dependencies[i].version;
outputStream.write(format(' "%s" -> "%s"%s', prev, nodeName, EOL));
outputStream.write(format(' "%s"[label="%s",style="filled",fillcolor="%d 1 1"]%s', nodeName, child, level / 15, EOL));
show(tree.dependencies[i], nodeName, level);
}
}
}
}
outputStream.write(format('digraph{%s', EOL));
outputStream.write(format(' root="%s@%s"%s', tree.name, tree.version, EOL));
show(tree, tree.name + "@" + tree.version, 0);
outputStream.write(format('}%s', EOL));
};