Skip to content

Commit

Permalink
fix: represent non-prod dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Dec 2, 2022
1 parent e082516 commit 5e04c15
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 250 deletions.
105 changes: 0 additions & 105 deletions .sinkchart/sinkchart@1.1.0-tree.svg

This file was deleted.

105 changes: 0 additions & 105 deletions .sinkchart/sinkchart@1.1.0-treemap.svg

This file was deleted.

105 changes: 105 additions & 0 deletions .sinkchart/sinkchart@1.2.0-tree.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions .sinkchart/sinkchart@1.2.0-treemap.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -43,7 +43,7 @@ Options:
--help Show help [boolean]
-o, --output The name of the output directory, relative to the
application path [string] [default: ".sinkchart"]
-d, --dev Include dev dependencies [boolean] [default: false]
-d, --include-dev Include dev dependencies [boolean] [default: false]
-v, --show-versions Show package versions [boolean] [default: false]
-t, --type Visualization type[string] [choices: "tree", "treemap"]
-p, --path The application path [string] [default: current dir]
Expand Down
23 changes: 9 additions & 14 deletions src/charts/tree.js
@@ -1,6 +1,12 @@
const D3Node = require('d3-node');
const addTooltips = require('./tooltip');
const {getModuleName, addSandwormLogo, getAncestors, addVulnerabilities} = require('./utils');
const {
getModuleName,
addSandwormLogo,
getAncestors,
addVulnerabilities,
processGraph,
} = require('./utils');

const d3n = new D3Node();
const {d3} = d3n;
Expand All @@ -17,21 +23,10 @@ const buildTree = (
width = 1000,
vulnerabilities = {},
maxDepth = Infinity,
includeDev = false,
} = {},
) => {
const processData = (node, depth) => {
const dependencies =
depth >= maxDepth
? []
: Object.values(node.dependencies || {}).map((n) => processData(n, depth + 1));

return {
...node,
children: dependencies,
};
};

const root = d3.hierarchy(processData(data, 0));
const root = d3.hierarchy(processGraph(data, {maxDepth, includeDev}));

// Construct an ordinal color scale
const color = d3.scaleOrdinal(
Expand Down
42 changes: 21 additions & 21 deletions src/charts/treemap.js
Expand Up @@ -8,31 +8,12 @@ const {
humanFileSize,
addSandwormLogo,
addVulnerabilities,
processGraph,
} = require('./utils');

const d3n = new D3Node();
const {d3} = d3n;

const processData = (node) => {
const dependencies = Object.values(node.dependencies || {}).map(processData);

return {
...node,
size: dependencies.length > 0 ? 0 : node.size,
children:
dependencies.length > 0
? [
...dependencies,
{
...node,
dependencies: undefined,
children: [],
},
]
: [],
};
};

// Modified from the original source below
// Copyright 2021 Observable, Inc.
// Released under the ISC license.
Expand All @@ -44,6 +25,7 @@ function buildTreemap(
width = 1000, // outer width, in pixels
vulnerabilities = {},
maxDepth = Infinity,
includeDev = false,
} = {},
) {
const moduleCallCounts = [];
Expand All @@ -57,7 +39,25 @@ function buildTreemap(
return currentCallCount;
};

const root = d3.hierarchy(processData(data));
const root = d3.hierarchy(
processGraph(data, {
maxDepth,
includeDev,
postprocess: (node) => {
if (node.children.length > 0) {
// eslint-disable-next-line no-param-reassign
node.size = 0;
node.children.push({
...node,
dependencies: undefined,
children: [],
});
}

return node;
},
}),
);

const color = d3.scaleSequential([0, root.height], d3.interpolateBrBG);
const nodeColor = (d) => {
Expand Down
25 changes: 25 additions & 0 deletions src/charts/utils.js
Expand Up @@ -83,6 +83,29 @@ const addVulnerabilities = (node, vulnerabilities) =>
.attr('url', (d) => d.url)
.attr('via', (d) => (typeof d === 'string' ? d : undefined));

const aggregateDependencies = (node, includeDev = false) => [
...Object.values(node.dependencies || {}),
...Object.values(node.optionalDependencies || {}),
...Object.values(node.peerDependencies || {}),
...Object.values(node.bundledDependencies || {}),
...(includeDev ? Object.values(node.devDependencies || {}) : []),
];

const processGraph = (node, options = {}, depth = 0, history = []) => {
const {maxDepth = Infinity, includeDev = false, postprocess = (n) => n} = options;
const dependencies =
depth >= maxDepth
? []
: aggregateDependencies(node, includeDev)
.filter((n) => !history.includes(n))
.map((n) => processGraph(n, options, depth + 1, [...history, node]));

return postprocess({
...node,
children: dependencies,
});
};

module.exports = {
getModuleName,
getUid,
Expand All @@ -91,4 +114,6 @@ module.exports = {
groupByDepth,
addSandwormLogo,
addVulnerabilities,
aggregateDependencies,
processGraph,
};
2 changes: 1 addition & 1 deletion src/cli.js
Expand Up @@ -69,7 +69,7 @@ require('yargs')
type: 'string',
})
.option('d', {
alias: 'dev',
alias: 'include-dev',
demandOption: false,
default: false,
describe: 'Include dev dependencies',
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -18,7 +18,6 @@ const getChartsSVG = async ({
onProgress({type: 'start', stage: 'sizes'});
packageTree = await getPackageSizes({
appPath,
includeDev,
onProgress: ({name, version}) =>
onProgress({type: 'update', stage: 'sizes', message: `${name}@${version}`}),
});
Expand All @@ -40,6 +39,7 @@ const getChartsSVG = async ({
width,
maxDepth,
vulnerabilities,
includeDev,
onProgress: (message) => onProgress({type: 'update', stage: 'chart', message}),
};

Expand Down
4 changes: 2 additions & 2 deletions src/package.js
Expand Up @@ -28,8 +28,8 @@ const getFolderSize = (folderPath) =>
}
});

const getPackageSizes = async ({appPath, includeDev = false, onProgress = () => {}}) => {
const packageTree = (await graph(appPath, includeDev)).root;
const getPackageSizes = async ({appPath, onProgress = () => {}}) => {
const packageTree = (await graph(appPath)).root;
const decorateWithSize = async (modules = {}, path = []) =>
Object.values(modules).reduce(async (acc, module) => {
await acc;
Expand Down

0 comments on commit 5e04c15

Please sign in to comment.