Skip to content

Commit

Permalink
feat: add config to disable size scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Jan 11, 2023
1 parent 73bc2c1 commit 88dda72
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
19 changes: 10 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {graph} = require('sandworm-utils');
const {getVulnerabilities} = require('./vulnerabilities');
const {buildTree, buildTreemap} = require('./charts');
const {getPackageSizes} = require('./package');
const {decorateWithSize} = require('./package');

const getChartsSVG = async ({
types = ['tree', 'treemap'],
Expand All @@ -12,19 +12,20 @@ const getChartsSVG = async ({
maxDepth = 7,
showLicenseInfo = true,
onProgress = () => {},
packageSizes = null,
}) => {
let packageTree;
const packageTree = (await graph(appPath)).root;

if (types.includes('treemap')) {
onProgress({type: 'start', stage: 'sizes'});
packageTree = await getPackageSizes({
appPath,
onProgress: ({name, version}) =>
onProgress({type: 'update', stage: 'sizes', message: `${name}@${version}`}),
});
if (!packageSizes) {
await decorateWithSize({
modules: packageTree.dependencies,
onProgress,
appPath
});
}
onProgress({type: 'end', stage: 'sizes'});
} else {
packageTree = (await graph(appPath)).root;
}

onProgress({type: 'start', stage: 'vulnerabilities'});
Expand Down
49 changes: 21 additions & 28 deletions src/package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const {exec} = require('child_process');
const {join} = require('path');
const {graph} = require('sandworm-utils');

const logger = console;
const packageSizeCache = {};
Expand Down Expand Up @@ -28,34 +27,28 @@ const getFolderSize = (folderPath) =>
}
});

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;
onProgress(module);
const pathToPackage = join(appPath, 'node_modules', module.name);
let size = 0;
try {
size = await getFolderSize(pathToPackage);
} catch (error) {
logger.error(`\nCould not get size for directory: ${pathToPackage}`);
}
const ancestors = module.ancestors || [];
const currentPath = [...path, `${module.name}@${module.version}`];
ancestors.push(currentPath);
Object.assign(module, {
size,
ancestors,
});

return decorateWithSize(module.dependencies, currentPath);
}, Promise.resolve());
await decorateWithSize(packageTree.dependencies, []);
const decorateWithSize = async ({modules = {}, path = [], onProgress = () => {}, appPath}) =>
Object.values(modules).reduce(async (acc, module) => {
await acc;
onProgress(module);
const pathToPackage = join(appPath, 'node_modules', module.name);
let size = 0;
try {
size = await getFolderSize(pathToPackage);
} catch (error) {
logger.error(`\nCould not get size for directory: ${pathToPackage}`);
}
const ancestors = module.ancestors || [];
const currentPath = [...path, `${module.name}@${module.version}`];
ancestors.push(currentPath);
Object.assign(module, {
size,
ancestors,
});

return packageTree;
};
return decorateWithSize({modules: module.dependencies, path: currentPath, onProgress, appPath});
}, Promise.resolve());

module.exports = {
getPackageSizes,
decorateWithSize,
};

0 comments on commit 88dda72

Please sign in to comment.