Skip to content

Commit

Permalink
Ignores: apply before listing files
Browse files Browse the repository at this point in the history
Previously filtered ignores only after files had been listed. That is somewhat inefficient, as it might mean looping through all node_modules before actually filtering out node_modules.

Check ignores as files are listed, so code never enters node_modules & other ignored folders.
  • Loading branch information
stscoundrel committed Jan 29, 2022
1 parent b3a6466 commit f48cece
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
33 changes: 22 additions & 11 deletions src/finder/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
import fs from 'fs';
import { doesNotContainIgnores, getDefaultIgnores } from '../ignores';

const isDirectory = (path: string): boolean => fs.statSync(path).isDirectory();

export const listFiles = (folderPath: string) : string[] => {
export const listFiles = (
folderPath: string,
ignores: string[],
) : string[] => {
const allFiles: string[] = [];
const files = fs.readdirSync(folderPath);

files.forEach((file) => {
const fullPath = `${folderPath}/${file}`;
if (isDirectory(fullPath)) {
allFiles.push(...listFiles(fullPath));
} else {
allFiles.push(fullPath);
}
});
files
.filter((file) => doesNotContainIgnores(file, ignores))
.forEach((file) => {
const fullPath = `${folderPath}/${file}`;
if (isDirectory(fullPath)) {
allFiles.push(...listFiles(fullPath, ignores));
} else {
allFiles.push(fullPath);
}
});

return allFiles;
};

export const listFilesinFolders = (folders: string[]): string[] => {
export const listFilesinFolders = (
folders: string[],
ignores: string[] = getDefaultIgnores(),
): string[] => {
const files: string[] = [];

folders.forEach((folder) => files.push(...listFiles(folder)));
folders
.forEach((folder) => files
.push(...listFiles(folder, ignores)));

return files;
};
Expand Down
13 changes: 13 additions & 0 deletions src/ignores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ const DEFAULT_IGNORES = [

export const getDefaultIgnores = (): string[] => DEFAULT_IGNORES;

export const doesNotContainIgnores = (
path: string,
ignores: string[] = getDefaultIgnores(),
) : boolean => {
let isAllowed = true;
ignores.forEach((ignore) => {
if (path.includes(ignore)) {
isAllowed = false;
}
});
return isAllowed;
};

export default {
getDefaultIgnores,
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getDefaultIgnores } from './ignores';

export const check = (extensions: string[], folders: string[]): string[] => {
const ignores = getDefaultIgnores();
const files = finder.listFilesinFolders(folders);
const files = finder.listFilesinFolders(folders, ignores);

return filterByExtensions(files, extensions, ignores);
};
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/finder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import finder from '../../src/finder';
import { getDefaultIgnores } from '../../src/ignores';

describe('Finder tests', () => {
test('Lists subfolders & files in a folder', () => {
Expand Down Expand Up @@ -36,4 +37,14 @@ describe('Finder tests', () => {

expect(result).toEqual(expected);
});

test('Does not list ignored folders', () => {
// Scanning top level could result in node_modules etc loops.
const results = finder.listFilesinFolders(['.']);

// Assert ignores were not scanned in the first place.
results.forEach((result) => {
getDefaultIgnores().forEach((ignore) => expect(result.includes(ignore)).toBeFalsy());
});
});
});

0 comments on commit f48cece

Please sign in to comment.