Skip to content

Commit a25c271

Browse files
author
Simon Renoult
committed
refactor: use named function for homogeneity
1 parent 3794b65 commit a25c271

File tree

2 files changed

+100
-92
lines changed

2 files changed

+100
-92
lines changed

.idea/workspace.xml

Lines changed: 62 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/count-commits-per-file.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { execSync } from "child_process";
22
import { existsSync, lstatSync } from "fs";
33
import { resolve } from "path";
44

5+
const PER_LINE = "\n";
6+
const COMMITS_PER_FILE = /(?<commitCount>[0-9]+) (?<relativePathToFile>.+)/;
7+
8+
export class CommitCountPerFile {
9+
constructor(
10+
public readonly relativePathToFile: string,
11+
public readonly absolutePathToFile: string,
12+
public readonly commitCount: number
13+
) {}
14+
}
15+
516
export async function countCommitsPerFile(
617
directory,
718
options: { firstParent; since }
@@ -17,33 +28,10 @@ export async function countCommitsPerFile(
1728
.split(PER_LINE)
1829
.map(trim)
1930
.filter(jsOrTsOnly)
20-
.map(line => {
21-
const { groups } = line.match(COMMITS_PER_FILE);
22-
return new CommitCountPerFile(
23-
groups.relativePathToFile,
24-
resolve(directory, groups.relativePathToFile),
25-
parseInt(groups.commitCount, 10)
26-
);
27-
})
31+
.map(toCommitCountPerFile(directory))
2832
.filter(ignoreFilesThatNoLongerExist);
2933
}
3034

31-
const PER_LINE = "\n";
32-
const trim = (s): string => s.trim();
33-
const jsOrTsOnly = (s): string => s.endsWith(".js") || s.endsWith(".ts");
34-
const COMMITS_PER_FILE = /(?<commitCount>[0-9]+) (?<relativePathToFile>.+)/;
35-
const ignoreFilesThatNoLongerExist = (
36-
commitCountPerFile: CommitCountPerFile
37-
): boolean => existsSync(commitCountPerFile.absolutePathToFile);
38-
39-
export class CommitCountPerFile {
40-
constructor(
41-
public readonly relativePathToFile: string,
42-
public readonly absolutePathToFile: string,
43-
public readonly commitCount: number
44-
) {}
45-
}
46-
4735
function assertGitIsInstalled(): void {
4836
try {
4937
execSync("which git");
@@ -70,6 +58,31 @@ function buildCommand(directory, { firstParent, since }): string {
7058
return [
7159
`git -C ${directory} log ${sinceParameter} ${firstParentFlag} --name-only --format=''`,
7260
"sort",
73-
"uniq -c"
61+
"uniq --count"
7462
].join(" | ");
7563
}
64+
65+
function trim(s): string {
66+
return s.trim();
67+
}
68+
69+
function jsOrTsOnly(s): string {
70+
return s.endsWith(".js") || s.endsWith(".ts");
71+
}
72+
73+
function toCommitCountPerFile(directory) {
74+
return (line: string): CommitCountPerFile => {
75+
const { groups } = line.match(COMMITS_PER_FILE);
76+
return new CommitCountPerFile(
77+
groups.relativePathToFile,
78+
resolve(directory, groups.relativePathToFile),
79+
parseInt(groups.commitCount, 10)
80+
);
81+
};
82+
}
83+
84+
function ignoreFilesThatNoLongerExist(
85+
commitCountPerFile: CommitCountPerFile
86+
): boolean {
87+
return existsSync(commitCountPerFile.absolutePathToFile);
88+
}

0 commit comments

Comments
 (0)