Repository navigation
Expand file tree
/
Copy pathnode-objects-diagram.js
More file actions
155 lines (137 loc) · 4.25 KB
/
Copy pathnode-objects-diagram.js
File metadata and controls
155 lines (137 loc) · 4.25 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var graphviz = require('graphviz');
var fs = require('fs');
var path = require('path');
var natives = process.binding('natives');
var moduleName;
var NODE_SRC_DIR = process.argv[2]; // path to target source code like '/Users/nsy/node/src/HEAD/lib'
var regexStr = '(util\\.)?inherits\\(([a-zA-Z.]+), \\s*([a-zA-Z.]+)\\)';
var regex = new RegExp(regexStr);
var regexG = new RegExp(regexStr, 'g');
var withAllProperties = process.argv[3] === '-a'; //outputs all properties in public, not only Classes
var fileFormat = 'png';
var fileName = (withAllProperties ? 'node-objects-all' : 'node-objects') +'.'+fileFormat;
var moduleAttr = {
shape: 'box',
fontsize: 12,
style: 'bold'
};
var classAttr = {
shape: 'box',
fontsize: 20,
style: 'bold'
};
var funcAttr = {
shape: 'plaintext',
fontsize: 12,
style: 'bold'
};
var hasAttr = {
dir: 'back',
color: quote('#0D3349'),
fontsize: 20
};
var inheritAttr = {
dir: 'back',
color: quote('#0D3349'),
arrowtail: 'empty',
fontsize: 20
};
//標準モジュールを検索
(function main() {
var graph = graphviz.digraph('G');
graph.set('rankdir', 'LR');
for (moduleName in natives) {
graph.addNode(moduleName, moduleAttr);
searchObject(graph, moduleName, require(moduleName));
searchParent(graph, moduleName);
}
graph.addNode('process', moduleAttr);
searchObject(graph, 'process', process);
//console.log(graph.to_dot());
graph.output(fileFormat, fileName);
})()
//モジュール配下のオブジェクトと関数を検索
function searchObject(graph, targetName, targetObj) {
var obj, objName;
for (objName in targetObj) {
if (targetObj.hasOwnProperty(objName)) {
var attr = null;
obj = targetObj[objName];
if (typeof obj === 'function' && isClass(objName)) {
attr = classAttr;
}
else if (withAllProperties && isPublic(objName)) {
if (typeof obj === 'function') {
attr = funcAttr;
objName += '()';
}
else {
attr = funcAttr;
}
}
if (attr) {
var classFullName = targetName+'.'+objName;
if (!exists(graph, classFullName)) {
graph.addNode(quote(classFullName), attr);
}
graph.addEdge(quote(targetName), quote(classFullName), hasAttr)
}
}
}
}
//モジュール内のinherits宣言を検索して継承関係を取得
function searchParent(graph, moduleName) {
var data = fs.readFileSync(path.join(NODE_SRC_DIR, moduleName+'.js'), 'utf-8');
var results = data.match(regexG);
if (results && results.length > 0) {
results.forEach(function(result) {
//console.log(moduleName+': '+result);
var res = result.match(regex);
if (res.length == 4) {
var child = res[2].indexOf('.') > -1 ? res[2] : moduleName+'.'+res[2];
//hack
var parent = res[3].indexOf('.') > -1 ? res[3] :
'Error' === res[3] ? 'Error' :
'Stream' === res[3] ? 'stream.Stream' :
'EventEmitter' === res[3] ? 'events.EventEmitter' :
moduleName+'.'+res[3];
if (!exists(graph, child)) {
graph.addNode(quote(child), classAttr);
}
if (!exists(graph, parent)) {
graph.addNode(quote(parent), classAttr);
}
graph.addEdge(quote(parent), quote(child), inheritAttr)
}
});
}
}
//親クラスを再帰的に検索
/** super_だと親クラス名だけでmodule名が取得できないので断念
super_ has only its parent class name. not sure how to get "moduleName.parentClassName".
function searchParent(obj, childName) {
//var parentName = childName+'.'+obj.name;
var parentName = normalize(childName, obj.name);
if (!exists(g, parentName)) {
g.addNode(quote(parentName), nodeAttr);
}
g.addEdge(quote(parentName), quote(childName), inheritAttr);
if (obj.super_) searchParent(obj.super_, parentName);
}
**/
function isClass(objName) {
return objName.match(/^[A-Z][a-z].+$/);
}
function isPublic(objName) {
return !objName.match(/^_.+$/);
}
function exists(graph, node) {
var nodes = graph.nodes.items;
nodes.forEach(function(i, n) {
if (n === node) return true;
});
return false;
}
function quote(str) {
return '"'+str+'"';
}