Skip to content
Permalink
Browse files

feat: return object with files from find-files

  • Loading branch information
lili2311 committed Aug 26, 2020
1 parent aaf9fbc commit cbdbcd361eb490901d87570df29f332ed6221012
@@ -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}`);
}
@@ -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[] {
@@ -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,
@@ -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;
@@ -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'),

0 comments on commit cbdbcd3

Please sign in to comment.
You can’t perform that action at this time.