Skip to content

Commit

Permalink
feat: output all dependency data as csv
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Jan 24, 2023
1 parent 3a08587 commit eb92647
Show file tree
Hide file tree
Showing 12 changed files with 807 additions and 541 deletions.
479 changes: 479 additions & 0 deletions .sinkchart/sinkchart@1.10.0-dependencies.csv

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions .sinkchart/sinkchart@1.10.0-tree.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 133 additions & 0 deletions .sinkchart/sinkchart@1.10.0-treemap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 0 additions & 133 deletions .sinkchart/sinkchart@1.3.0-tree.svg

This file was deleted.

133 changes: 0 additions & 133 deletions .sinkchart/sinkchart@1.3.0-treemap.svg

This file was deleted.

133 changes: 0 additions & 133 deletions .sinkchart/sinkchart@1.4.0-tree.svg

This file was deleted.

133 changes: 0 additions & 133 deletions .sinkchart/sinkchart@1.4.0-treemap.svg

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"d3-node": "3.0.0",
"ora": "6.1.2",
"sandworm-utils": "1.11.1",
"sandworm-utils": "1.11.2",
"semver": "^7.3.8",
"yargs": "17.6.0"
},
Expand Down
36 changes: 36 additions & 0 deletions src/charts/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function jsonToCsv(items) {
const headerNames = Object.keys(items[0]);
const rowItems = items.map((row) =>
headerNames
.map((fieldName) => JSON.stringify(row[fieldName], (_, value) => value ?? '-'))
.join(','),
);
return [headerNames.join(','), ...rowItems].join('\r\n');
}

module.exports = (dependencies) => {
const processedDependencies = (dependencies || []).map(
({name, version, flags, parents, size, license}) => ({
name,
version,
size,
license,
isProd: !!flags.prod,
isDev: !!flags.dev,
isOptional: !!flags.optional,
isPeer: !!flags.peer,
isBundled: !!flags.bundled,
parents: Object.values(parents || {})
.reduce(
(agg, deps) =>
agg.concat(
Object.values(deps).map(({name: dname, version: dversion}) => `${dname}@${dversion}`),
),
[],
)
.join(','),
}),
);

return jsonToCsv(processedDependencies);
};
8 changes: 7 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const getStartMessage = (stage) => {
return 'Drawing tree chart...';
case 'treemap':
return 'Drawing treemap chart...';
case 'csv':
return 'Building CSV...';
default:
return '';
}
Expand All @@ -31,6 +33,8 @@ const getEndMessage = (stage) => {
return 'Tree chart done';
case 'treemap':
return 'Treemap chart done';
case 'csv':
return 'CSV done';
default:
return '';
}
Expand Down Expand Up @@ -107,7 +111,7 @@ require('yargs')
logger.log('\x1b[36m%s\x1b[0m', `Sinkchart 🧭`);
const {default: ora} = await import('ora');

const {svgs, name, version} = await getReport({
const {svgs, csv, name, version} = await getReport({
types: argv.t ? [argv.t] : undefined,
appPath: argv.p,
includeDev: argv.d,
Expand All @@ -124,6 +128,8 @@ require('yargs')
await fs.mkdir(path.dirname(outputPath), {recursive: true});
await fs.writeFile(outputPath, svgs[chartType]);
}, Promise.resolve());
const csvOutputPath = path.join(argv.p, argv.o, `${name}@${version}-dependencies.csv`);
await fs.writeFile(csvOutputPath, csv);
currentSpinner.stopAndPersist({symbol: '✨', text: 'Done'});
},
)
Expand Down
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {getDependencyGraph, addDependencyGraphData} = require('sandworm-utils');
const {getDependencyVulnerabilities} = require('./vulnerabilities/dependencies');
const {buildTree, buildTreemap} = require('./charts');
const {getReports} = require('./vulnerabilities/utils');
const csv = require('./charts/csv');

const getReport = async ({
types = ['tree', 'treemap'],
Expand All @@ -16,9 +17,8 @@ const getReport = async ({
}) => {
const errors = [];
onProgress({type: 'start', stage: 'graph'});
const packageGraph = (
dependencyGraph || (await getDependencyGraph(appPath, {loadDataFromDisk: true}))
).root;
const dGraph = dependencyGraph || (await getDependencyGraph(appPath, {loadDataFromDisk: true}));
const packageGraph = dGraph.root;
onProgress({type: 'end', stage: 'graph'});

onProgress({type: 'start', stage: 'vulnerabilities'});
Expand Down Expand Up @@ -71,10 +71,21 @@ const getReport = async ({
return current;
}, Promise.resolve({}));

onProgress({type: 'start', stage: 'csv'});
let csvData;
try {
csvData = csv(dGraph.all);
} catch (error) {
errors.push(error);
}
onProgress({type: 'end', stage: 'csv'});

return {
dependencyGraph: dGraph,
dependencyVulnerabilities: dependencyVulnerabilities.filter(({findings: {affects}}) => affects.length),
rootVulnerabilities,
svgs,
csv: csvData,
name: packageGraph.name,
version: packageGraph.version,
errors,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2780,10 +2780,10 @@ safe-regex-test@^1.0.0:
resolved "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

sandworm-utils@1.11.1:
version "1.11.1"
resolved "https://registry.yarnpkg.com/sandworm-utils/-/sandworm-utils-1.11.1.tgz#29f8e83da28f2b12dbb39e8f6b202b9bfde3ae18"
integrity sha512-hoMj5+rrm24nwzRdNDVpQxu1H4YBJul6O2CR9VMdsNVR2zxdTVeDnXTLmyHcb+2VHvf/A4nRm8WXNwZ7nhvSmA==
sandworm-utils@1.11.2:
version "1.11.2"
resolved "https://registry.yarnpkg.com/sandworm-utils/-/sandworm-utils-1.11.2.tgz#c5cff976bb72f6dc17327ec51af3979f046e2a26"
integrity sha512-p1axwl4MX6BBPCwCJxP6Fn8qh18wSoMSPZRmCrgtcrPPUbXpNnXlqRSncjU8dcRo+7iKzqsdXEnQZVa1fabq1Q==
dependencies:
"@pnpm/lockfile-file" "6.0.2"
"@pnpm/logger" "5.0.0"
Expand Down

0 comments on commit eb92647

Please sign in to comment.