-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (203 loc) · 6.17 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var babel = require('babel-core');
var fs = require('fs');
var path = require('path');
function getStringLiteralValue(node, context) {
if (node.type == 'StringLiteral') {
return node.value;
} else {
throw(`${context}: expected StringLiteral, got ${node.type}`);
}
}
function getIdentifierName(node, context) {
if (node.type == 'Identifier') {
return node.name;
} else {
throw(`${context}: expected Identifier, got ${node.type}`);
}
}
function isDirectory(path) {
try {
stats = fs.lstatSync(path);
if (stats.isDirectory()) return true;
return false;
}
catch (e) {
return false;
}
}
function convertSourceReference(fileRelPath, filePath, projectPath) {
if (fileRelPath.substr(0, 2) == './' || fileRelPath.substr(0, 3) == '../') {
var targetFullPath = path.resolve(path.dirname(filePath), fileRelPath);
if (isDirectory(targetFullPath)) {
targetFullPath = targetFullPath.concat("/index");
}
return path.relative(projectPath, targetFullPath);
}
else {
return fileRelPath;
}
}
function parseCode(contents) {
return babel.transform(contents, {
presets: ['stage-0'],
plugins: ['transform-react-jsx']
});
}
function parseCodeForImports(contents) {
try {
return parseCode(contents);
}
catch (e) {
// Syntax Error => slice out import statements only
var code = "";
var regex = /import[^;]+;/g;
var match;
while (match = regex.exec(contents)) {
code = code.concat(match[0] + "\n");
}
try {
return parseCode(code);
}
catch (e) {
return null;
}
}
}
function getImportsFromFile(filePath, projectPath, callback) {
fs.readFile(filePath, 'utf8', function(err, contents) {
if (err) throw err;
var result = parseCodeForImports(contents);
if (!result) {
callback(null);
return
}
var imports = [];
result.ast.program.body.forEach(function(node){
if (node.type == 'ImportDeclaration') {
var importedElements = [];
node.specifiers.forEach(function(specifierNode){
if (specifierNode.type == 'ImportDefaultSpecifier') {
// import defaultMember from "module-name";
importedElements.push('*');
} else if (specifierNode.type == 'ImportSpecifier') {
// import { member } from "module-name";
// import { member as alias } from "module-name";
// import { member1 , member2 } from "module-name";
// import { member1 , member2 as alias2 } from "module-name";
importedElements.push(getIdentifierName(specifierNode.imported, 'ImportDeclaration specifier'));
} else if (specifierNode.type == 'ImportNamespaceSpecifier') {
// import * as name from "module-name";
importedElements.push('*');
} else throw('Unhandled specifier type: ' + specifierNode.type + ' in ' + filePath);
});
imports.push(
[
importedElements,
convertSourceReference(
getStringLiteralValue(node.source, 'ImportDeclaration source'),
filePath,
projectPath
)
]
);
}
else if (node.type == 'ExportNamedDeclaration' && node.source !== null) {
var importedElements = [];
node.specifiers.forEach(function(specifierNode){
if (specifierNode.type == 'ExportSpecifier') {
identifierName = getIdentifierName(specifierNode.local);
importedElements.push(identifierName == 'default' ? '*' : identifierName);
} else throw('Unhandled specifier type: ' + specifierNode.type + ' in ' + filePath);
});
imports.push(
[
importedElements,
convertSourceReference(
getStringLiteralValue(node.source, 'ImportDeclaration source'),
filePath,
projectPath
)
]
);
}
});
callback(imports);
});
}
function fileExistsSync(path) {
try {
fs.accessSync(path, fs.F_OK);
return true;
} catch (e) {
return false;
}
}
function getIgnores(path) {
var parser = require('gitignore-parser'),
fs = require('fs');
var gitignore = null;
if (fileExistsSync(path + '/.gitignore')) {
gitignore = parser.compile(fs.readFileSync(path + '/.gitignore', 'utf8'));
}
var myignore = parser.compile(`
example
examples
node_modules
doc
docs
site
`.trim());
var accepts = function(path) {
if (gitignore && gitignore.denies(path)) return false;
if (myignore.denies(path)) return false;
return true;
}
return {
accepts: accepts,
denies: function(path) { return !accepts(path); }
}
}
function getModuleDependenciesInProject(projectPath, callback) {
var walk = require('walk')
, fs = require('fs')
, path = require('path')
, walker = walk.walk(projectPath, { followLinks: false, filters: ['node_modules', 'tmp', 'temp'] })
;
var graph = {};
var ignores = getIgnores(projectPath);
function fileHandler(root, fileStat, next) {
var filePath = root + '/' + fileStat.name
var fileRelPath = filePath.substr(projectPath.length + 1)
var fileRef = null;
var ext = path.extname(fileRelPath);
if (ignores.denies(fileRelPath)) {
return next();
}
if (ext != '.js') return next();
fileRef = fileRelPath.substr(0, fileRelPath.length - ext.length);
// console.log(fileRelPath);
getImportsFromFile(filePath, projectPath, function(imports) {
// console.log(imports);
// console.log();
if (!imports) return next();
graph[fileRef] = imports.map((pair) => pair[1]);
next();
});
}
function errorsHandler(root, nodeStatsArray, next) {
nodeStatsArray.forEach(function (n) {
console.error("[ERROR] " + n.name)
console.error(n.error.message || (n.error.code + ": " + n.error.path));
});
next();
}
function endHandler() {
// console.log("Graph:");
// console.log(graph);
callback(graph);
}
walker.on("file", fileHandler);
walker.on("errors", errorsHandler); // plural
walker.on("end", endHandler);
}
module.exports.getModuleDependenciesInProject = getModuleDependenciesInProject;