From 77d11ba3f86c28938cbab9dacd067d03021fef1b Mon Sep 17 00:00:00 2001 From: ghe Date: Thu, 27 Aug 2020 16:55:00 +0100 Subject: [PATCH] feat: return all files detected from find() on top of detected files returned the unfiltered list as well --- .gitignore | 2 +- src/lib/find-files.ts | 40 +++++++++++++++++++++++++++++++--------- test/find-files.test.ts | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 7a6bb64a769..1f7c828e001 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ config.local.json /node_modules/ local.log /patches/ -/dist +**/dist tmp .DS_Store /package-lock.json diff --git a/src/lib/find-files.ts b/src/lib/find-files.ts index 4d31147ccf7..bc9708141dc 100644 --- a/src/lib/find-files.ts +++ b/src/lib/find-files.ts @@ -39,6 +39,11 @@ export async function getStats(path: string): Promise { }); } +interface FindFilesRes { + files: string[]; + allFilesFound: string[]; +} + /** * Find all files in given search path. Returns paths to files found. * @@ -47,10 +52,6 @@ export async function getStats(path: string): Promise { * @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[] = [], @@ -58,9 +59,11 @@ export async function find( levelsDeep = 4, ): Promise { 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')) { @@ -68,21 +71,36 @@ export async function find( } 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}`); } @@ -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); }); @@ -124,6 +142,10 @@ async function findInDirectory( [], found.map((f) => f.files), ), + allFilesFound: Array.prototype.concat.apply( + [], + found.map((f) => f.allFilesFound), + ), }; } diff --git a/test/find-files.test.ts b/test/find-files.test.ts index 026739acd92..d37558f26e9 100644 --- a/test/find-files.test.ts +++ b/test/find-files.test.ts @@ -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, @@ -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) => {