-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
96 lines (87 loc) · 2.85 KB
/
Copy pathutils.js
File metadata and controls
96 lines (87 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const simpleGit = require('simple-git');
const fsp = require('fs/promises');
const path = require('path');
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const cleanupRepo = async (repoPath) => {
await sleep(5000);
try {
await fsp.rm(repoPath, { recursive: true, force: true });
console.log(`Successfully removed the repo directory at ${repoPath}`);
} catch (error) {
console.error(`Error cleaning up repo directory: ${error}`);
}
};
const getFilesRecursively = async (directoryPath) => {
let filePaths = [];
const files = await fsp.readdir(directoryPath);
for (const file of files) {
const filePath = path.join(directoryPath, file);
if (filePath.includes('.git') || file.startsWith('.')) {
continue;
}
const stat = await fsp.stat(filePath);
if (stat.isDirectory()) {
const subDirFiles = await getFilesRecursively(filePath);
filePaths = filePaths.concat(subDirFiles);
} else {
filePaths.push(filePath);
}
}
return filePaths;
};
const analyzeRepository = async (repoPath) => {
const services = {};
const fileOwners = {};
const filePaths = await getFilesRecursively(repoPath);
for (const filePath of filePaths) {
const stat = await fsp.stat(filePath);
if (stat.isFile()) {
if (filePath.endsWith('.js')) {
services[filePath] = 'Detected service from JS file';
}
const owner = await getFileOwner(filePath, repoPath);
const devs = await getFileContributors(filePath, repoPath);
fileOwners[filePath] = { owner, devs };
}
}
return {
services,
fileOwners,
};
};
const getFileOwner = async (filePath, repoPath) => {
try {
const git = simpleGit(repoPath);
const blameInfo = await git.raw(['blame', '-e', filePath]);
const lines = blameInfo.trim().split('\n');
const lastLine = lines[lines.length - 1];
const ownerMatch = lastLine.match(/<(.+?)>/);
if (ownerMatch && ownerMatch[1]) {
return ownerMatch[1];
}
return 'Unknown Owner';
} catch (error) {
console.error(`Error getting file owner for ${filePath}:`, error);
return 'Unknown Owner';
}
}
const getFileContributors = async (filePath, repoPath) => {
const git = simpleGit(repoPath);
const contributors = {};
const logs = await git.log([filePath]);
logs.all.forEach((log) => {
const author = log.author_name;
contributors[author] = (contributors[author] || 0) + 1;
});
return Object.entries(contributors).map(([name, score]) => ({
name,
score,
}));
}
module.exports = {
sleep,
cleanupRepo,
getFilesRecursively,
analyzeRepository,
getFileOwner
};