@@ -2,6 +2,17 @@ import { execSync } from "child_process";
22import { existsSync , lstatSync } from "fs" ;
33import { 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+
516export 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-
4735function 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