diff --git a/src/api/git/getChangedFiles.ts b/src/api/git/getChangedFiles.ts index 11b4385c7..ecaca8182 100644 --- a/src/api/git/getChangedFiles.ts +++ b/src/api/git/getChangedFiles.ts @@ -1,6 +1,7 @@ import fs from 'fs-extra'; import git from 'isomorphic-git'; +import { findRoot } from './findRoot'; import { ABSENT, FILEPATH, @@ -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; + rule?: (params: { file: ChangedFile; gitRoot: string }) => Promise; } interface ChangedFilesParameters { @@ -60,6 +61,11 @@ export const getChangedFiles = async ({ ignore = [], }: ChangedFilesParameters): Promise => { + 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( @@ -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 }), ), ); }; diff --git a/src/cli/lint/autofix.ts b/src/cli/lint/autofix.ts index ddec23078..bf8e41a67 100644 --- a/src/cli/lint/autofix.ts +++ b/src/cli/lint/autofix.ts @@ -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',