-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (37 loc) · 1.14 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
'use strict';
const FS = require('fs');
const directoryTree = require('directory-tree');
const getFolderSize = require('./bin/size');
function dependencyGraph(root) {
root = root || __dirname;
let nodes = {};
let edges = [];
let tree = directoryTree(root, { extensions: /.json$/ }, function(item, PATH) {
if (item.name === 'package.json') {
const packageInfo = JSON.parse(FS.readFileSync(item.path));
const packageDir = PATH.dirname(item.path);
const size = Math.round(getFolderSize(packageDir, /node_modules$/) / 1024);
nodes[packageInfo.name] = size;
for (const dep in packageInfo.dependencies) {
edges.push({
from: packageInfo.name,
to: dep,
});
}
}
});
let nodesArray = [];
for (const node in nodes) {
nodesArray.push({
id: node,
label: node,
value: nodes[node],
title: nodes[node] + ' KB',
});
}
return {
nodes: nodesArray,
edges: edges,
};
}
module.exports = dependencyGraph;