forked from xiemaisi/acg.js
- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrequireJsGraph.js
More file actions
Latest commit
70 lines (62 loc) · 2.72 KB
/
Copy pathrequireJsGraph.js
File metadata and controls
70 lines (62 loc) · 2.72 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
if(typeofdefine!=='function'){
vardefine=require('amdefine')(module);
}
define(function(require,exports){
varassert=require('assert'),
astutil=require('./astutil'),
_=require('./underscore'),
fs=require('fs');
functionmakeRequireJsGraph(ast){
assert.equal(1,ast.programs.length,"Can only have one starting point at the moment.");
varrx=/^.*\\(.+\\)*(.+)\.(.+)$/g;
varregexParse=rx.exec(ast.programs[0].attr.filename);
varpartialFileName=regexParse[2]+".js",
fileName="./"+partialFileName,
folder=regexParse[0].split(/[a-zA-Z]+\.js/)[0].replace(/\/$/,"\\");
vardependencyGraph=[];
astutil.visit(ast,function(node){
switch(node.type){
case'CallExpression' :
if(node.callee.name==="define"||node.callee.name==="require"){
vardependencies=[],argument=node.arguments[0];
if(argument.type==="ArrayExpression"){
argument.elements.forEach(function(element){
dependencies.push(element.value+".js");
});
}elseif(argument.type==="Literal"){
dependencies.push(argument.value+".js");
}
dependencies.forEach(function(dependency){
dependencyGraph.push(newDependency(fileName,dependency));
});
}
break;
}
});
dependencyGraph.map(function(dep){returndep.to}).forEach(function(outgoingDep){
varnormOutgoingDep=outgoingDep.replace(/^.\//,"");
normOutgoingDep=normOutgoingDep.replace(/^\//,"");
normOutgoingDep=normOutgoingDep.replace(/\//,"\\");
varnewStart=folder+normOutgoingDep;
if(fs.existsSync(newStart)){
varreferencedAST=astutil.buildAST([newStart]);
dependencyGraph=dependencyGraph.concat(makeRequireJsGraph(referencedAST))
}
});
return_.uniq(dependencyGraph,function(edge){
returnedge.toString();
});
}
functionDependency(from,to){
this.from=from;
this.to=to;
this.toString=function(){
returnremoveLeadingPointSlash(this.from)+" -> "+removeLeadingPointSlash(this.to);
};
functionremoveLeadingPointSlash(path){
returnpath.replace(/^\.?\//,"");
}
}
exports.makeRequireJsGraph=makeRequireJsGraph;
returnexports;
});