From cbdbcd361eb490901d87570df29f332ed6221012 Mon Sep 17 00:00:00 2001 From: ghe Date: Mon, 17 Aug 2020 14:40:46 +0100 Subject: [PATCH] feat: return object with files from find-files --- src/lib/find-files.ts | 27 ++++++++++++++-------- src/lib/plugins/get-deps-from-plugin.ts | 2 +- src/lib/plugins/get-extra-project-count.ts | 6 ++++- test/find-files.test.ts | 27 +++++++++++++--------- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/lib/find-files.ts b/src/lib/find-files.ts index c89eb3b64a7..4d31147ccf7 100644 --- a/src/lib/find-files.ts +++ b/src/lib/find-files.ts @@ -47,16 +47,20 @@ 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[] = [], filter: string[] = [], levelsDeep = 4, -): Promise { +): Promise { const found: string[] = []; // ensure we ignore find against node_modules path. if (path.endsWith('node_modules')) { - return found; + return { files: found }; } // ensure node_modules is always ignored if (!ignore.includes('node_modules')) { @@ -64,13 +68,13 @@ export async function find( } try { if (levelsDeep < 0) { - return found; + return { files: found }; } else { levelsDeep--; } const fileStats = await getStats(path); if (fileStats.isDirectory()) { - const files = await findInDirectory(path, ignore, filter, levelsDeep); + const { files } = await findInDirectory(path, ignore, filter, levelsDeep); found.push(...files); } else if (fileStats.isFile()) { const fileFound = findFile(path, filter); @@ -78,8 +82,7 @@ export async function find( found.push(fileFound); } } - - return filterForDefaultManifests(found); + return { files: filterForDefaultManifests(found) }; } catch (err) { throw new Error(`Error finding files in path '${path}'.\n${err.message}`); } @@ -102,7 +105,7 @@ async function findInDirectory( ignore: string[] = [], filter: string[] = [], levelsDeep = 4, -): Promise { +): Promise { const files = await readDirectory(path); const toFind = files .filter((file) => !ignore.includes(file)) @@ -110,12 +113,18 @@ async function findInDirectory( const resolvedPath = pathLib.resolve(path, file); if (!fs.existsSync(resolvedPath)) { debug('File does not seem to exist, skipping: ', file); - return []; + return { files: [] }; } return find(resolvedPath, ignore, filter, levelsDeep); }); + const found = await Promise.all(toFind); - return Array.prototype.concat.apply([], found); + return { + files: Array.prototype.concat.apply( + [], + found.map((f) => f.files), + ), + }; } function filterForDefaultManifests(files: string[]): string[] { diff --git a/src/lib/plugins/get-deps-from-plugin.ts b/src/lib/plugins/get-deps-from-plugin.ts index 4fd4117e65f..57240107f6e 100644 --- a/src/lib/plugins/get-deps-from-plugin.ts +++ b/src/lib/plugins/get-deps-from-plugin.ts @@ -42,7 +42,7 @@ export async function getDepsFromPlugin( const scanType = options.yarnWorkspaces ? 'yarnWorkspaces' : 'allProjects'; const levelsDeep = options.detectionDepth; const ignore = options.exclude ? options.exclude.split(',') : []; - const targetFiles = await find( + const { files: targetFiles } = await find( root, ignore, multiProjectProcessors[scanType].files, diff --git a/src/lib/plugins/get-extra-project-count.ts b/src/lib/plugins/get-extra-project-count.ts index 8c40a71d7d0..8d31f99a6c7 100644 --- a/src/lib/plugins/get-extra-project-count.ts +++ b/src/lib/plugins/get-extra-project-count.ts @@ -19,7 +19,11 @@ export async function getExtraProjectCount( return inspectResult.plugin.meta.allSubProjectNames.length; } try { - const extraTargetFiles = await find(root, [], AUTO_DETECTABLE_FILES); + const { files: extraTargetFiles } = await find( + root, + [], + AUTO_DETECTABLE_FILES, + ); const foundProjectsCount = extraTargetFiles.length > 1 ? extraTargetFiles.length - 1 : undefined; return foundProjectsCount; diff --git a/test/find-files.test.ts b/test/find-files.test.ts index aa34873061c..026739acd92 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 result = await find(testFixture, [], [], 6); + const { files: result } = await find(testFixture, [], [], 6); const expected = [ path.join( testFixture, @@ -35,7 +35,7 @@ test('find all files in test fixture', async (t) => { test('find all files in test fixture ignoring node_modules', async (t) => { // six levels deep to ensure node_modules is tested - const result = await find(testFixture, ['node_modules'], [], 6); + const { files: result } = await find(testFixture, ['node_modules'], [], 6); const expected = [ path.join( testFixture, @@ -64,14 +64,19 @@ test('find all files in test fixture ignoring node_modules', async (t) => { test('find package.json file in test fixture ignoring node_modules', async (t) => { // six levels deep to ensure node_modules is tested const nodeModulesPath = path.join(testFixture, 'node_modules'); - const result = await find(nodeModulesPath, [], ['package.json'], 6); + const { files: result } = await find( + nodeModulesPath, + [], + ['package.json'], + 6, + ); const expected = []; t.same(result.sort(), expected.sort(), 'should return expected file'); }); test('find package.json file in test fixture (by default ignoring node_modules)', async (t) => { // six levels deep to ensure node_modules is tested - const result = await find(testFixture, [], ['package.json'], 6); + const { files: result } = await find(testFixture, [], ['package.json'], 6); const expected = [ path.join(testFixture, 'npm', 'package.json'), path.join(testFixture, 'npm-with-lockfile', 'package.json'), @@ -83,7 +88,7 @@ test('find package.json file in test fixture (by default ignoring node_modules)' test('find package-lock.json file in test fixture (ignore package.json in the same folder)', async (t) => { const npmLockfilePath = path.join(testFixture, 'npm-with-lockfile'); - const result = await find( + const { files: result } = await find( npmLockfilePath, [], ['package.json', 'package-lock.json'], @@ -96,7 +101,7 @@ test('find package-lock.json file in test fixture (ignore package.json in the sa test('find build.gradle file in test fixture (ignore build.gradle in the same folder)', async (t) => { const buildGradle = path.join(testFixture, 'gradle-and-kotlin'); - const result = await find( + const { files: result } = await find( buildGradle, [], ['build.gradle.kts', 'build.gradle'], @@ -109,7 +114,7 @@ test('find build.gradle file in test fixture (ignore build.gradle in the same fo test('find Gemfile.lock file in test fixture (ignore Gemfile in the same folder)', async (t) => { const npmLockfilePath = path.join(testFixture, 'ruby'); - const result = await find( + const { files: result } = await find( npmLockfilePath, [], ['Gemfile', 'Gemfile.lock'], @@ -122,7 +127,7 @@ test('find Gemfile.lock file in test fixture (ignore Gemfile in the same folder) test('find yarn.lock file in test fixture (ignore package.json in the same folder)', async (t) => { const yarnLockfilePath = path.join(testFixture, 'yarn'); - const result = await find( + const { files: result } = await find( yarnLockfilePath, [], ['package.json', 'yarn.lock'], @@ -134,7 +139,7 @@ test('find yarn.lock file in test fixture (ignore package.json in the same folde test('find package.json file in test fixture (by default ignoring node_modules)', async (t) => { // four levels deep to ensure node_modules is tested - const result = await find(testFixture, [], ['package.json'], 4); + const { files: result } = await find(testFixture, [], ['package.json'], 4); const expected = [ path.join(testFixture, 'npm', 'package.json'), path.join(testFixture, 'npm-with-lockfile', 'package.json'), @@ -144,13 +149,13 @@ test('find package.json file in test fixture (by default ignoring node_modules)' }); test('find Gemfile file in test fixture', async (t) => { - const result = await find(testFixture, [], ['Gemfile']); + const { files: result } = await find(testFixture, [], ['Gemfile']); const expected = [path.join(testFixture, 'ruby', 'Gemfile')]; t.same(result.sort(), expected.sort(), 'should return expected file'); }); test('find pom.xml files in test fixture', async (t) => { - const result = await find(testFixture, [], ['pom.xml']); + const { files: result } = await find(testFixture, [], ['pom.xml']); const expected = [ path.join(testFixture, 'maven', 'pom.xml'), path.join(testFixture, 'mvn', 'pom.xml'),