Skip to content

Commit 03dc1b0

Browse files
author
AvrAlexandra
committed
refactoring
1 parent 823ba1a commit 03dc1b0

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

src/commands/history/history-git-commit.ts

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function processCommitForPlugins(
3131
const changes = await getChangedFiles(commit, folder);
3232
await ensureDirectoryExists(depinderTempFolder);
3333

34-
const tempFilePathsMap = new Map<string, string[]>(); // Store temp files per plugin
34+
const tempFilePathsMap = new Map<string, string[]>();
3535

3636
for (const plugin of selectedPlugins) {
3737
let filteredFiles = changes
@@ -43,38 +43,14 @@ export async function processCommitForPlugins(
4343

4444
try {
4545
if (plugin.name === 'npm') {
46-
if (filteredFiles.includes('package.json') || filteredFiles.includes('package-lock.json')) {
47-
if (!filteredFiles.includes('package.json')) {
48-
filteredFiles.push('package.json');
49-
}
50-
if (!filteredFiles.includes('package-lock.json')) {
51-
filteredFiles.push('package-lock.json');
52-
}
53-
}
54-
55-
for (const file of filteredFiles) {
56-
const tempFilePath = path.join(depinderTempFolder, `${commit.oid}-${path.basename(file)}`);
57-
try {
58-
const fileContent = await git.readBlob({
59-
fs,
60-
dir: folder,
61-
oid: commit.oid,
62-
filepath: file,
63-
});
64-
await fs.writeFile(tempFilePath, fileContent.blob);
65-
tempFilePaths.push(tempFilePath);
66-
} catch (error) {
67-
console.error(`Failed to read file ${file} at commit ${commit.oid}:`, error);
68-
}
69-
}
46+
tempFilePaths = await processNpmPlugin(commit, folder, filteredFiles);
7047
} else if (plugin.name === 'java') {
7148
tempFilePaths = await processJavaPlugin(commit, folder, filteredFiles);
7249
if (!tempFilePaths || tempFilePaths.length === 0) {
7350
throw new Error('No temp files returned from processJavaPlugin.');
7451
}
7552
}
7653

77-
// Store temp file paths for later extraction
7854
tempFilePathsMap.set(plugin.name, tempFilePaths);
7955
} catch (error) {
8056
console.error(`Error processing plugin ${plugin.name} for commit ${commit.oid}:`, error);
@@ -83,7 +59,6 @@ export async function processCommitForPlugins(
8359
}
8460
}
8561

86-
// Now, extract projects for all plugins after handling temp files
8762
for (const [pluginName, tempFilePaths] of tempFilePathsMap.entries()) {
8863
let projects: DepinderProject[] | string = [];
8964

@@ -102,11 +77,44 @@ export async function processCommitForPlugins(
10277
}
10378
commitProjectsMap.get(pluginName)!.push({ commit, projects });
10479

105-
// Cleanup temp files after extraction
10680
await cleanupTempFiles(tempFilePaths);
10781
}
10882
}
10983

84+
async function processNpmPlugin(commit: any, folder: string, filteredFiles: string[]): Promise<string[]> {
85+
const tempFilePaths: string[] = [];
86+
87+
const requiredFiles = ['package.json', 'package-lock.json'];
88+
const hasAnyRequiredFile = requiredFiles.some(file => filteredFiles.includes(file));
89+
90+
if (hasAnyRequiredFile) {
91+
for (const file of requiredFiles) {
92+
if (!filteredFiles.includes(file)) {
93+
filteredFiles.push(file);
94+
}
95+
}
96+
}
97+
98+
for (const file of filteredFiles) {
99+
const tempFilePath = path.join(depinderTempFolder, `${commit.oid}-${path.basename(file)}`);
100+
try {
101+
const { blob } = await git.readBlob({
102+
fs,
103+
dir: folder,
104+
oid: commit.oid,
105+
filepath: file,
106+
});
107+
108+
await fs.writeFile(tempFilePath, blob);
109+
tempFilePaths.push(tempFilePath);
110+
} catch (error) {
111+
console.error(`Failed to process ${file} at commit ${commit.oid}:`, error);
112+
}
113+
}
114+
115+
return tempFilePaths;
116+
}
117+
110118
// Function to get changed files between commits
111119
async function getChangedFiles(commit: any, folder: string): Promise<string[]> {
112120
const changedFiles: string[] = [];

0 commit comments

Comments
 (0)