Skip to content

Commit f27b771

Browse files
committed
Enhance package-dependencies tool with library count tracking
- Added a new interface for tracking specific library counts in package-dependencies.ts. - Implemented logic to count occurrences of 'struts', 'commons', 'log4j', and 'cryptix' in target classes. - Updated Markdown output to include a section for specific library counts, detailing the number of dependencies for each library. - Logged library counts to the console for better visibility during execution. - Updated .gitignore to include package-dependencies.md.
1 parent 1d49d1f commit f27b771

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
samples/
2+
package-dependencies.md
23

34
# Dependency directories
45
node_modules/

package-dependencies.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,26 @@ interface PackageInfo {
2424
isExternal: boolean;
2525
}
2626

27+
// Interface for tracking specific library counts
28+
interface LibraryCounts {
29+
struts: number;
30+
commons: number;
31+
log4j: number;
32+
cryptix: number;
33+
}
34+
2735
class PackageDependencyExtractor {
2836
private packageMap: Map<string, PackageInfo> = new Map();
2937
private dependencyMap: Map<string, Set<string>> = new Map();
3038
private artifactMap: Map<string, Set<string>> = new Map();
3139
private basePackageDependencyMap: Map<string, Set<string>> = new Map();
40+
// Add property to track library counts
41+
private libraryCounts: LibraryCounts = {
42+
struts: 0,
43+
commons: 0,
44+
log4j: 0,
45+
cryptix: 0
46+
};
3247

3348
async parseJsonlFile(filePath: string): Promise<void> {
3449
const fileStream = fs.createReadStream(filePath);
@@ -83,6 +98,26 @@ class PackageDependencyExtractor {
8398
}
8499
this.artifactMap.get(record.artifactId)!.add(sourcePackage);
85100
this.artifactMap.get(record.artifactId)!.add(targetPackage);
101+
102+
// Count specific libraries in targetClass
103+
this.countSpecificLibraries(targetClass);
104+
}
105+
106+
// Method to count instances of specific libraries
107+
private countSpecificLibraries(targetClass: string): void {
108+
// Check for each specific library in the targetClass
109+
if (targetClass.toLowerCase().includes('struts')) {
110+
this.libraryCounts.struts++;
111+
}
112+
if (targetClass.toLowerCase().includes('commons')) {
113+
this.libraryCounts.commons++;
114+
}
115+
if (targetClass.toLowerCase().includes('log4j')) {
116+
this.libraryCounts.log4j++;
117+
}
118+
if (targetClass.toLowerCase().includes('cryptix')) {
119+
this.libraryCounts.cryptix++;
120+
}
86121
}
87122

88123
private getPackageName(className: string): string {
@@ -186,6 +221,16 @@ class PackageDependencyExtractor {
186221
let markdownContent = '# Project Package Dependencies\n\n';
187222
markdownContent += 'This document lists all base packages that the project depends on.\n\n';
188223

224+
// Add section for specific library counts
225+
markdownContent += '## Specific Library Counts\n\n';
226+
markdownContent += 'These counts represent the number of dependencies where the `targetClass` field in the JSONL data contains each specific library name. This helps quantify how many times your application code depends on classes from these libraries, which is useful for identifying vulnerability exposure.\n\n';
227+
markdownContent += '| Library | Count |\n';
228+
markdownContent += '|---------|-------|\n';
229+
markdownContent += `| Struts | ${this.libraryCounts.struts} |\n`;
230+
markdownContent += `| Commons | ${this.libraryCounts.commons} |\n`;
231+
markdownContent += `| Log4j | ${this.libraryCounts.log4j} |\n`;
232+
markdownContent += `| Cryptix | ${this.libraryCounts.cryptix} |\n\n`;
233+
189234
// List all base packages
190235
markdownContent += '## Base Packages\n\n';
191236

@@ -278,6 +323,18 @@ class PackageDependencyExtractor {
278323

279324
fs.writeFileSync(outputFile, markdownContent);
280325
console.log(`Markdown output written to ${outputFile}`);
326+
327+
// Log the library counts to console
328+
console.log('\nSpecific Library Counts:');
329+
console.log(`- Struts: ${this.libraryCounts.struts}`);
330+
console.log(`- Commons: ${this.libraryCounts.commons}`);
331+
console.log(`- Log4j: ${this.libraryCounts.log4j}`);
332+
console.log(`- Cryptix: ${this.libraryCounts.cryptix}`);
333+
}
334+
335+
// Getter for library counts (useful for testing)
336+
getLibraryCounts(): LibraryCounts {
337+
return this.libraryCounts;
281338
}
282339
}
283340

0 commit comments

Comments
 (0)