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

Commit c926197

Browse files
authored
feat: audit --only-git tracked files (#37)
* feat: target node 8 (remove await boilerplate) Maybe this makes it start faster? Maybe it's my imagination? * chore: ignore junit.xml (generated by jest) * feat: audit --only-git tracked files This stops the auditing report from showing random files people have lying around in their working directory, if they happen to not care about those.
1 parent accca12 commit c926197

8 files changed

Lines changed: 44 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ fabric.properties
7070

7171
# Android studio 3.1+ serialized cache file
7272
.idea/caches/build_file_checksums.ser
73+
/junit.xml

src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ commander.command('audit')
1717
.option('-c, --codeowners <filePath>', 'path to codeowners file (default: "<dir>/.github/CODEOWNERS")')
1818
.option('-o, --output <outputFormat>', `how to output format eg: ${Object.values(OUTPUT_FORMAT).join(', ')}`, OUTPUT_FORMAT.SIMPLE)
1919
.option('-u, --unloved', 'write unowned files only', false)
20+
.option('-g, --only-git', 'consider only files tracked by git', false)
2021
.option('-s, --stats', 'write output stats', false)
2122
.option('-r, --root <rootPath>', 'the root path to filter files by', '')
2223
.action(async (options) => {

src/commands/audit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface AuditOptions {
77
dir: string;
88
unloved: boolean;
99
output: OUTPUT_FORMAT;
10+
onlyGit: boolean;
1011
stats: boolean;
1112
root: string;
1213
}

src/lib/ownership/file.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ import { OwnedFile } from './lib/OwnedFile';
22
import { OwnershipEngine } from './lib/OwnershipEngine';
33
import { readDirRecursively } from './lib/readDirRecursively';
44
import * as path from 'path';
5+
import { gitLsFiles } from './lib/gitLsFiles';
56

6-
export const getFileOwnership = async (options: { codeowners: string, dir: string, root?: string }): Promise<OwnedFile[]> => {
7+
export const getFileOwnership = async (options: { codeowners: string, dir: string, onlyGit: boolean, root?: string }): Promise<OwnedFile[]> => {
78
const engine = OwnershipEngine.FromCodeownersFile(options.codeowners);
89

9-
let filePaths = await readDirRecursively(options.dir, ['.git']);
10+
let filePaths;
11+
if (options.onlyGit) {
12+
filePaths = await gitLsFiles(options.dir);
13+
} else {
14+
filePaths = await readDirRecursively(options.dir, ['.git']);
15+
}
1016

1117
if (options.root) { // We need to re-add the root so that later ops can find the file
1218
filePaths = filePaths.map(filePath => path.join(<string>options.root, filePath));

src/lib/ownership/lib/execFile.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { execFile as realExecFile } from 'child_process';
2+
import { promisify } from 'util';
3+
4+
export const execFile = promisify(realExecFile);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { execFile } from './execFile';
2+
import { mocked } from 'ts-jest';
3+
import { gitLsFiles } from './gitLsFiles';
4+
jest.mock('./execFile');
5+
6+
describe('git ls-files', () => {
7+
it('splits the input', async () => {
8+
mocked(execFile).mockResolvedValue({ stdout: 'foo\nbar\n', stderr: '' });
9+
10+
const result = await gitLsFiles('some/dir');
11+
12+
expect(result).toStrictEqual(['foo', 'bar']);
13+
expect(execFile).toHaveBeenCalledWith(
14+
expect.anything(),
15+
expect.anything(),
16+
expect.objectContaining({
17+
cwd: 'some/dir',
18+
}),
19+
);
20+
});
21+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { execFile } from './execFile';
2+
3+
export const gitLsFiles = async (dir: string): Promise<string[]> => {
4+
const { stdout } = await execFile('git', ['ls-files'], { cwd: dir });
5+
return stdout.split('\n').filter(x => !!x);
6+
};

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"compilerOptions": {
33
/* Basic Options */
44
// "incremental": true, /* Enable incremental compilation */
5-
"target": "es6",
5+
"target": "es2017",
66
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
77
"module": "commonjs",
88
/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
9-
// "lib": [], /* Specify library files to be included in the compilation. */
9+
"lib": ["es2017"], /* Specify library files to be included in the compilation. */
1010
// "allowJs": true, /* Allow javascript files to be compiled. */
1111
// "checkJs": true, /* Report errors in .js files. */
1212
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */

0 commit comments

Comments
 (0)