Skip to content

Commit

Permalink
pass gitroot
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy committed Oct 27, 2023
1 parent e479587 commit 63f37ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/api/git/getChangedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra';
import git from 'isomorphic-git';

import { findRoot } from './findRoot';
import {
ABSENT,
FILEPATH,
Expand All @@ -23,7 +24,7 @@ export interface IgnoredFile extends ChangedFile {
* @param file - The file to validate.
* @returns boolean - Whether the file should be ignored.
*/
rule?: (dir: string, file: ChangedFile) => Promise<boolean>;
rule?: (params: { file: ChangedFile; gitRoot: string }) => Promise<boolean>;
}

interface ChangedFilesParameters {
Expand Down Expand Up @@ -60,6 +61,11 @@ export const getChangedFiles = async ({

ignore = [],
}: ChangedFilesParameters): Promise<ChangedFile[]> => {
const gitRoot = await findRoot({ dir });

if (!gitRoot) {
throw new Error(`Could not find Git root from directory: ${dir}`);
}
const allFiles = await git.statusMatrix({ fs, dir });
return allFiles
.filter(
Expand All @@ -75,7 +81,7 @@ export const getChangedFiles = async ({
(i) =>
i.path === changedFile.path &&
i.state === changedFile.state &&
i.rule?.(dir, changedFile),
i.rule?.({ gitRoot, file: changedFile }),
),
);
};
6 changes: 1 addition & 5 deletions src/cli/lint/autofix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export const AUTOFIX_IGNORE_FILES: Git.IgnoredFile[] = [
// further changes as the CI environment may have appended an npm token.
path: '.npmrc',
state: 'modified',
rule: async (dir, file) => {
const gitRoot = await Git.findRoot({ dir });
if (!gitRoot) {
throw new Error(`Could not find Git root from directory: ${dir}`);
}
rule: async ({ file, gitRoot }) => {
const content = await fs.promises.readFile(
path.join(gitRoot, file.path),
'utf8',
Expand Down

0 comments on commit 63f37ac

Please sign in to comment.