Skip to content

Commit

Permalink
feat: add pruning to go mod projects at depgraph generation
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed May 4, 2023
1 parent 3266baf commit ab49821
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ async function getDependencies(root, targetFile, additionalArgs?: string[]) {
let tempDirObj;
const packageManager = pkgManagerByTarget(targetFile);
if (packageManager === 'gomodules') {
const dependencyGraph = await buildDepGraphFromImportsAndModules(
root,
targetFile,
additionalArgs,
);
return {
dependencyGraph: await buildDepGraphFromImportsAndModules(
root,
targetFile,
additionalArgs,
),
dependencyGraph,
};
}

Expand Down Expand Up @@ -597,10 +598,12 @@ function buildGraph(
currentParent: string,
childrenChain: Map<string, string[]>,
ancestorsChain: Map<string, string[]>,
visited?: Set<string>,
) {
const depPackagesLen = depPackages.length;

for (let i = depPackagesLen - 1; i >= 0; i--) {
visited = visited || new Set<string>();
const packageImport = depPackages[i];
let version = 'unknown';
if (isBuiltinPackage(packageImport)) {
Expand Down Expand Up @@ -636,8 +639,18 @@ function buildGraph(
continue;
}

if (visited.has(packageImport)) {
const prunedId = `${packageImport}:pruned`;
depGraphBuilder.addPkgNode(newNode, prunedId, {
labels: { pruned: 'true' },
});
depGraphBuilder.connectDep(currentParent, prunedId);
continue;
}

depGraphBuilder.addPkgNode(newNode, packageImport);
depGraphBuilder.connectDep(currentParent, packageImport);
visited.add(packageImport);

childrenChain.set(currentParent, [...currentChildren, packageImport]);
ancestorsChain.set(packageImport, [...currentAncestors, currentParent]);
Expand All @@ -651,6 +664,7 @@ function buildGraph(
packageImport,
childrenChain,
ancestorsChain,
visited,
);
}
}
Expand Down

0 comments on commit ab49821

Please sign in to comment.