Skip to content

Commit

Permalink
feat: prune graph on test if asked
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 authored and dkontorovskyy committed Aug 5, 2019
1 parent 351dc7f commit f0e51f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ export async function monitorGraph(
});
}

function countPathsToGraphRoot(graph: depGraphLib.DepGraph): number {
export function countPathsToGraphRoot(graph: depGraphLib.DepGraph): number {
return graph
.getPkgs()
.map((pkg) => graph.countPathsToRoot(pkg))
.reduce((acc, pathsToRoot) => acc + pathsToRoot, 0);
}

async function pruneGraph(
export async function pruneGraph(
depGraph: depGraphLib.DepGraph,
packageManager: SupportedPackageManagers,
): Promise<depGraphLib.DepGraph> {
Expand Down
17 changes: 15 additions & 2 deletions src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../errors';
import { maybePrintDeps } from '../print-deps';
import { SupportedPackageManagers } from '../package-managers';
import { countPathsToGraphRoot, pruneGraph } from '../monitor';

// tslint:disable-next-line:no-var-requires
const debug = require('debug')('snyk');
Expand Down Expand Up @@ -72,7 +73,6 @@ async function runTest(packageManager: SupportedPackageManagers,
if (payload.body && payload.body.docker && payload.body.docker.dockerfilePackages) {
dockerfilePackages = payload.body.docker.dockerfilePackages;
}
await spinner.clear<void>(spinnerLbl)();
await spinner(spinnerLbl);
analytics.add('depGraph', !!depGraph);
analytics.add('isDocker', !!(payload.body && payload.body.docker));
Expand Down Expand Up @@ -333,9 +333,22 @@ async function assembleLocalPayloads(root, options: Options & TestOptions): Prom
// Graphs are more compact and robust representations.
// Legacy parts of the code are still using trees, but will eventually be fully migrated.
debug('converting dep-tree to dep-graph', {name: pkg.name, targetFile: depRoot.targetFile || options.file});
const depGraph = await depGraphLib.legacy.depTreeToGraph(
let depGraph = await depGraphLib.legacy.depTreeToGraph(
pkg, options.packageManager);

debug('done converting dep-tree to dep-graph', {uniquePkgsCount: depGraph.getPkgs().length});
if (options['prune-repeated-subdependencies']) {
debug('Trying to prune the graph');
const prePruneDepCount = countPathsToGraphRoot(depGraph);
debug('pre prunedPathsCount: ' + prePruneDepCount);

depGraph = await pruneGraph(depGraph, options.packageManager);

analytics.add('prePrunedPathsCount', prePruneDepCount);
const postPruneDepCount = countPathsToGraphRoot(depGraph);
debug('post prunedPathsCount: ' + prePruneDepCount);
analytics.add('postPrunedPathsCount', postPruneDepCount);
}
body.depGraph = depGraph;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function isMultiResult(pet: SingleDepRootResult | MultiDepRootsResult): p
export interface TestOptions {
traverseNodeModules: boolean;
interactive: boolean;
'prune-repeated-subdependencies'?: boolean;
}
export interface ProtectOptions {
loose: boolean;
Expand Down

0 comments on commit f0e51f2

Please sign in to comment.