electrode-electrify-react-component
Version:
electrode-electrify-react-component
90 lines (73 loc) • 2.08 kB
Flow
/*eslint-disable no-magic-numbers*/
module.exports = function jsonTree(modules) {
let maxDepth = 1;
let rootSize = 0;
const root = {
children: [],
name: "root"
};
const getChild = function (arr, name) {
for (let i = 0; i < arr.length; i++) {
if (arr[i].name === name) {
return arr[i];
}
}
};
const getFile = function (module, fileName, parentTree) {
const charIndex = fileName.indexOf("/");
if (charIndex !== -1) {
let folder = fileName.slice(0, charIndex);
if (folder === "~") {
folder = "node_modules";
}
let childFolder = getChild(parentTree.children, folder);
if (!childFolder) {
childFolder = {
name: folder,
children: []
};
parentTree.children.push(childFolder);
}
getFile(module, fileName.slice(charIndex + 1), childFolder);
} else {
module.name = fileName;
parentTree.children.push(module);
}
};
const dirsizes = function (child) {
return child.size = "size" in child //eslint-disable-line no-return-assign
? child.size
: child.children.reduce((size, child) => { //eslint-disable-line no-shadow
return size + ("size" in child ? child.size : dirsizes(child));
}, 0);
};
modules.forEach(
function addToTree(module) { //eslint-disable-line prefer-arrow-callback
let size;
if (module.source) {
size = module.source.length;
} else {
size = module.size;
}
rootSize += size;
const mod = {
id: module.id,
fullName: module.name,
size,
reasons: module.reasons
};
const depth = mod.fullName.split("/").length - 1;
if (depth > maxDepth) {
maxDepth = depth;
}
let fileName = mod.fullName;
const beginning = mod.fullName.slice(0, 2);
if (beginning === "./") {
fileName = fileName.slice(2);
}
getFile(mod, fileName, root);
});
root.maxDepth = maxDepth;
root.size = rootSize;
return root;
};