Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit 15db91c

Browse files
authored
reducing complexity of file ts (#46)
1 parent 377f1df commit 15db91c

3 files changed

Lines changed: 42 additions & 30 deletions

File tree

src/lib/ownership/file.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,39 @@ import { OwnedFile } from './lib/OwnedFile';
33
import { OwnershipEngine } from './lib/OwnershipEngine';
44
import { readDirRecursively } from './lib/readDirRecursively';
55
import { readTrackedGitFiles } from './lib/readTrackedGitFiles';
6+
import { countLinesInFile } from './lib/countLinesInFile';
67

78
export const getFileOwnership = async (options: { codeowners: string, dir: string, onlyGit: boolean, root?: string }): Promise<OwnedFile[]> => {
9+
const filePaths = await getFilePaths(options.dir, options.onlyGit, options.root);
10+
811
const engine = OwnershipEngine.FromCodeownersFile(options.codeowners);
912

13+
const files: OwnedFile[] = [];
14+
15+
for (const filePath of filePaths) {
16+
const owners = engine.calcFileOwnership(filePath);
17+
const lines = await countLinesInFile(filePath);
18+
19+
files.push(new OwnedFile({ path: filePath, owners, lines }));
20+
}
21+
22+
return files;
23+
};
24+
25+
const getFilePaths = async (dir: string, onlyGit: boolean, root?: string) => {
1026
let filePaths;
11-
if (options.onlyGit) {
12-
filePaths = await readTrackedGitFiles(options.dir);
27+
28+
if (onlyGit) {
29+
filePaths = await readTrackedGitFiles(dir);
1330
} else {
14-
filePaths = await readDirRecursively(options.dir, ['.git']);
31+
filePaths = await readDirRecursively(dir, ['.git']);
1532
}
1633

17-
if (options.root) { // We need to re-add the root so that later ops can find the file
18-
filePaths = filePaths.map(filePath => path.join(<string>options.root, filePath));
34+
if (root) { // We need to re-add the root so that later ops can find the file
35+
filePaths = filePaths.map(filePath => path.join(root, filePath));
1936
}
2037

2138
filePaths.sort();
2239

23-
const files: OwnedFile[] = [];
24-
25-
for (const filePath of filePaths) {
26-
files.push(await OwnedFile.FromPath(filePath, engine));
27-
}
28-
29-
return files;
40+
return filePaths;
3041
};

src/lib/ownership/lib/OwnedFile.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { OwnershipEngine } from './OwnershipEngine';
2-
import * as fs from 'fs';
3-
import { log } from '../../logger';
2+
import { countLinesInFile } from './countLinesInFile';
43

54
export class OwnedFile {
65
// tslint:disable-next-line:variable-name
@@ -43,19 +42,3 @@ export class OwnedFile {
4342
});
4443
}
4544
}
46-
47-
const countLinesInFile = async (filePath: string): Promise<number> => {
48-
let i;
49-
let count = 0;
50-
return new Promise((resolve, reject) => {
51-
fs.createReadStream(filePath)
52-
.on('error', (e) => {
53-
log.error(`failed to read lines from file ${filePath}`, e);
54-
reject(e);
55-
})
56-
.on('data', (chunk) => {
57-
for (i = 0; i < chunk.length; ++i) if (chunk[i] === 10) count++;
58-
})
59-
.on('end', () => resolve(count));
60-
});
61-
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as fs from 'fs';
2+
import { log } from '../../logger';
3+
4+
export const countLinesInFile = async (filePath: string): Promise<number> => {
5+
let i;
6+
let count = 0;
7+
return new Promise((resolve, reject) => {
8+
fs.createReadStream(filePath)
9+
.on('error', (e) => {
10+
log.error(`failed to read lines from file ${filePath}`, e);
11+
reject(e);
12+
})
13+
.on('data', (chunk) => {
14+
for (i = 0; i < chunk.length; ++i) if (chunk[i] === 10) count++;
15+
})
16+
.on('end', () => resolve(count));
17+
});
18+
};

0 commit comments

Comments
 (0)