Skip to content

Commit

Permalink
feat: return all files detected from find()
Browse files Browse the repository at this point in the history
on top of detected files returned the unfiltered list as well
  • Loading branch information
lili2311 committed Aug 27, 2020
1 parent 53a90d4 commit 77d11ba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -3,7 +3,7 @@ config.local.json
/node_modules/
local.log
/patches/
/dist
**/dist
tmp
.DS_Store
/package-lock.json
Expand Down
40 changes: 31 additions & 9 deletions src/lib/find-files.ts
Expand Up @@ -39,6 +39,11 @@ export async function getStats(path: string): Promise<fs.Stats> {
});
}

interface FindFilesRes {
files: string[];
allFilesFound: string[];
}

/**
* Find all files in given search path. Returns paths to files found.
*
Expand All @@ -47,42 +52,55 @@ export async function getStats(path: string): Promise<fs.Stats> {
* @param filter (optional) file names to find. If not provided all files are returned.
* @param levelsDeep (optional) how many levels deep to search, defaults to two, this path and one sub directory.
*/
interface FindFilesRes {
files: string[];
}

export async function find(
path: string,
ignore: string[] = [],
filter: string[] = [],
levelsDeep = 4,
): Promise<FindFilesRes> {
const found: string[] = [];
const foundAll: string[] = [];

// ensure we ignore find against node_modules path.
if (path.endsWith('node_modules')) {
return { files: found };
return { files: found, allFilesFound: foundAll };
}
// ensure node_modules is always ignored
if (!ignore.includes('node_modules')) {
ignore.push('node_modules');
}
try {
if (levelsDeep < 0) {
return { files: found };
return { files: found, allFilesFound: foundAll };
} else {
levelsDeep--;
}
const fileStats = await getStats(path);
if (fileStats.isDirectory()) {
const { files } = await findInDirectory(path, ignore, filter, levelsDeep);
const { files, allFilesFound } = await findInDirectory(
path,
ignore,
filter,
levelsDeep,
);
found.push(...files);
foundAll.push(...allFilesFound);
} else if (fileStats.isFile()) {
const fileFound = findFile(path, filter);
if (fileFound) {
found.push(fileFound);
foundAll.push(fileFound);
}
}
return { files: filterForDefaultManifests(found) };
const filteredOutFiles = foundAll.filter((f) => !found.includes(f));
if (filteredOutFiles.length) {
debug(
`Filtered out ${filteredOutFiles.length}/${
foundAll.length
} files: ${foundAll.join(', ')}`,
);
}
return { files: filterForDefaultManifests(found), allFilesFound: foundAll };
} catch (err) {
throw new Error(`Error finding files in path '${path}'.\n${err.message}`);
}
Expand Down Expand Up @@ -113,7 +131,7 @@ async function findInDirectory(
const resolvedPath = pathLib.resolve(path, file);
if (!fs.existsSync(resolvedPath)) {
debug('File does not seem to exist, skipping: ', file);
return { files: [] };
return { files: [], allFilesFound: [] };
}
return find(resolvedPath, ignore, filter, levelsDeep);
});
Expand All @@ -124,6 +142,10 @@ async function findInDirectory(
[],
found.map((f) => f.files),
),
allFilesFound: Array.prototype.concat.apply(
[],
found.map((f) => f.allFilesFound),
),
};
}

Expand Down
35 changes: 34 additions & 1 deletion test/find-files.test.ts
Expand Up @@ -6,7 +6,7 @@ const testFixture = path.join(__dirname, 'fixtures', 'find-files');

test('find all files in test fixture', async (t) => {
// six levels deep to find all
const { files: result } = await find(testFixture, [], [], 6);
const { files: result, allFilesFound } = await find(testFixture, [], [], 6);
const expected = [
path.join(
testFixture,
Expand All @@ -29,8 +29,41 @@ test('find all files in test fixture', async (t) => {
path.join(testFixture, 'ruby', 'Gemfile.lock'),
path.join(testFixture, 'yarn', 'yarn.lock'),
];
const filteredOut = [
path.join(testFixture, 'golang', 'golang-app', 'Gopkg.toml'),
path.join(testFixture, 'README.md'),
path.join(testFixture, 'yarn', 'package.json'),
path.join(testFixture, 'ruby', 'Gemfile'),
path.join(testFixture, 'gradle-kts', 'subproj', 'build.gradle.kts'),
path.join(testFixture, 'npm-with-lockfile', 'package.json'),
path.join(testFixture, 'gradle', 'subproject', 'build.gradle'),
path.join(testFixture, 'gradle-and-kotlin', 'build.gradle.kts'),
path.join(
testFixture,
'gradle-multiple',
'gradle',
'subproject',
'build.gradle',
),
path.join(
testFixture,
'gradle-multiple',
'gradle-another',
'subproject',
'build.gradle',
),
path.join(testFixture, 'maven', 'test.txt'),
path.join(testFixture, 'mvn', 'test.txt'),
path.join(testFixture, 'npm', 'test.txt'),
path.join(testFixture, 'ruby', 'test.txt'),
];
t.same(result.length, expected.length, 'should be the same length');
t.same(result.sort(), expected.sort(), 'should return all files');
t.same(
allFilesFound.filter((f) => !f.endsWith('broken-symlink')).sort(),
[...filteredOut, ...expected].sort(),
'should return all unfiltered files',
);
});

test('find all files in test fixture ignoring node_modules', async (t) => {
Expand Down

0 comments on commit 77d11ba

Please sign in to comment.