-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
80 lines (66 loc) · 1.98 KB
/
cli.js
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
#!/usr/bin/env node
const fs = require('fs');
const execSync = require('child_process').execSync;
try {
const result = execSync(
'git log --name-only --pretty=format: | sort | uniq -c | sort -nr',
).toString();
const commitsPerFile = getCommitsPerFile(result);
const weightedPathes = getPathesWithWeight(commitsPerFile);
writeToFile(weightedPathes);
} catch (e) {
console.error(`Could not execute/find git.`, e);
}
/****/
function writeToFile(pathes) {
fs.writeFileSync('hot-spots.json', JSON.stringify(pathes), err => {
console.error('Could not write file!', err);
});
}
function getPathesWithWeight(files) {
const pathes = {};
files.forEach(file => {
const { commitCount, filePath } = file;
if (filePath) {
const chunkedPath = filePath.split('/');
if (chunkedPath.length > 0) {
const fileName = chunkedPath.pop();
pathes[filePath] = {
id: filePath,
name: fileName,
parent: chunkedPath.length > 0 ? chunkedPath.join('/') : undefined,
commitCount,
};
chunkedPath.forEach((path, i) => {
const id = chunkedPath.slice(0, i).join('/') || path;
if (pathes.hasOwnProperty(id)) {
pathes[id].commitCount += commitCount;
} else {
const parent =
i === 0 ? undefined : chunkedPath.slice(i - 1).join('/');
pathes[id] = {
id,
name: path,
parent,
commitCount,
};
}
});
}
}
});
return sortByCommitCount(pathes);
}
function sortByCommitCount(pathes) {
return Object.values(pathes).sort(
(prevPath, path) => path.commitCount - prevPath.commitCount,
);
}
function getCommitsPerFile(stdout) {
const lines = stdout.split('\n');
return lines.map(line => {
const cleanLine = line.trim();
const [commitCount, path] = cleanLine.split(' ');
return { commitCount: parseInt(commitCount, 10), filePath: path };
});
}