Skip to content

Commit

Permalink
feat: notify about very large trees
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Mar 17, 2023
1 parent 84c90c5 commit d9e5f8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/charts/tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const D3Node = require('d3-node');
const logger = require('../cli/logger');
const addTooltips = require('./tooltip');
const {
getModuleName,
Expand Down Expand Up @@ -30,6 +31,22 @@ const buildTree = (
const {d3} = d3n;
const root = d3.hierarchy(processGraph(data, {maxDepth, includeDev}));

const descendants = root.descendants();

if (descendants.length > 100000) {
logger.log(
`\n\n⚠️ Your dependency tree has a very large number of nodes (${descendants.length}).`,
);
logger.log('Generating the tree chart might take a lot of time & memory.');
logger.log('If the process crashes you have several options:');
logger.log(
'- Allocate more memory to the node process by exporting `NODE_OPTIONS="--max-old-space-size=16384"`',
);
logger.log(
'- Reduce the depth of the tree represented by passing the `--max-depth` option to Sandworm',
);
}

// Construct an ordinal color scale
const color = d3.scaleOrdinal(
new Set([getModuleName(root), (root.children || []).map((d) => getModuleName(d))]),
Expand Down Expand Up @@ -117,7 +134,7 @@ const buildTree = (
.append('g')
.attr('id', 'label-group')
.selectAll('g')
.data(root.descendants())
.data(descendants)
.join('g')
.attr('style', 'cursor: pointer')
.attr('transform', (d) => `translate(${d.y},${d.x})`);
Expand Down
5 changes: 4 additions & 1 deletion src/charts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ const processGraph = (node, options = {}, depth = 0, history = []) => {
.map((n) => processGraph(n, options, depth + 1, [...history, node]));

return postprocess({
...node,
name: node.name,
version: node.version,
license: node.license,
size: node.size,
children: dependencies,
});
};
Expand Down

0 comments on commit d9e5f8c

Please sign in to comment.