Skip to content

Commit

Permalink
feat: return object with files from find-files
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Aug 26, 2020
1 parent aaf9fbc commit cbdbcd3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
27 changes: 18 additions & 9 deletions src/lib/find-files.ts
Expand Up @@ -47,39 +47,42 @@ 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<string[]> {
): Promise<FindFilesRes> {
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')) {
ignore.push('node_modules');
}
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);
if (fileFound) {
found.push(fileFound);
}
}

return filterForDefaultManifests(found);
return { files: filterForDefaultManifests(found) };
} catch (err) {
throw new Error(`Error finding files in path '${path}'.\n${err.message}`);
}
Expand All @@ -102,20 +105,26 @@ async function findInDirectory(
ignore: string[] = [],
filter: string[] = [],
levelsDeep = 4,
): Promise<string[]> {
): Promise<FindFilesRes> {
const files = await readDirectory(path);
const toFind = files
.filter((file) => !ignore.includes(file))
.map((file) => {
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[] {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plugins/get-deps-from-plugin.ts
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/lib/plugins/get-extra-project-count.ts
Expand Up @@ -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;
Expand Down
27 changes: 16 additions & 11 deletions 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 result = await find(testFixture, [], [], 6);
const { files: result } = await find(testFixture, [], [], 6);
const expected = [
path.join(
testFixture,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'),
Expand All @@ -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'),
Expand Down

0 comments on commit cbdbcd3

Please sign in to comment.