Skip to content

Commit

Permalink
feat: [OSM-1040] switched to config object as find files function param
Browse files Browse the repository at this point in the history
  • Loading branch information
gemaxim committed Apr 29, 2024
1 parent 7dc15c9 commit ab7ad73
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 126 deletions.
85 changes: 51 additions & 34 deletions src/lib/find-files.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as fs from 'fs';
import * as pathLib from 'path';

const sortBy = require('lodash.sortby');
const groupBy = require('lodash.groupby');
import * as sortBy from 'lodash.sortby';
import * as groupBy from 'lodash.groupby';
import * as assign from 'lodash.assign';
import { detectPackageManagerFromFile } from './detect';
import * as debugModule from 'debug';
import {
Expand Down Expand Up @@ -53,6 +54,30 @@ interface FindFilesRes {

const ignoreFolders = ['node_modules', '.build'];

interface FindFilesConfig {
path: string;
ignore?: string[];
filter?: string[];
levelsDeep?: number;
featureFlags?: Set<string>;
}

type DefaultFindConfig = {
path: string;
ignore: string[];
filter: string[];
levelsDeep: number;
featureFlags: Set<string>;
};

const defaultFindConfig: DefaultFindConfig = {
path: '',
ignore: [],
filter: [],
levelsDeep: 4,
featureFlags: new Set<string>(),
};

/**
* Find all files in given search path. Returns paths to files found.
*
Expand All @@ -61,47 +86,36 @@ const ignoreFolders = ['node_modules', '.build'];
* @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.
*/
export async function find(
path: string,
ignore: string[] = [],
filter: string[] = [],
featureFlags: Set<string> = new Set<string>(),
levelsDeep = 4,
): Promise<FindFilesRes> {
export async function find(findConfig: FindFilesConfig): Promise<FindFilesRes> {
const config: DefaultFindConfig = assign({}, defaultFindConfig, findConfig);
const found: string[] = [];
const foundAll: string[] = [];

// ensure we ignore find against node_modules path and .build folder for swift.
if (path.endsWith('node_modules') || path.endsWith('/.build')) {
if (config.path.endsWith('node_modules') || config.path.endsWith('/.build')) {
return { files: found, allFilesFound: foundAll };
}

// ensure dependencies folders is always ignored
for (const folder of ignoreFolders) {
if (!ignore.includes(folder)) {
ignore.push(folder);
if (!config.ignore.includes(folder)) {
config.ignore.push(folder);
}
}

try {
if (levelsDeep < 0) {
if (config.levelsDeep < 0) {
return { files: found, allFilesFound: foundAll };
} else {
levelsDeep--;
config.levelsDeep--;
}
const fileStats = await getStats(path);
const fileStats = await getStats(config.path);
if (fileStats.isDirectory()) {
const { files, allFilesFound } = await findInDirectory(
path,
ignore,
filter,
featureFlags,
levelsDeep,
);
const { files, allFilesFound } = await findInDirectory(config);
found.push(...files);
foundAll.push(...allFilesFound);
} else if (fileStats.isFile()) {
const fileFound = findFile(path, filter);
const fileFound = findFile(config.path, config.filter);
if (fileFound) {
found.push(fileFound);
foundAll.push(fileFound);
Expand All @@ -116,11 +130,13 @@ export async function find(
);
}
return {
files: filterForDefaultManifests(found, featureFlags),
files: filterForDefaultManifests(found, config.featureFlags),
allFilesFound: foundAll,
};
} catch (err) {
throw new Error(`Error finding files in path '${path}'.\n${err.message}`);
throw new Error(
`Error finding files in path '${config.path}'.\n${err.message}`,
);
}
}

Expand All @@ -137,22 +153,23 @@ function findFile(path: string, filter: string[] = []): string | null {
}

async function findInDirectory(
path: string,
ignore: string[] = [],
filter: string[] = [],
featureFlags: Set<string> = new Set<string>(),
levelsDeep = 4,
findConfig: FindFilesConfig,
): Promise<FindFilesRes> {
const files = await readDirectory(path);
const config: DefaultFindConfig = assign({}, defaultFindConfig, findConfig);
const files = await readDirectory(config.path);
const toFind = files
.filter((file) => !ignore.includes(file))
.filter((file) => !config.ignore.includes(file))
.map((file) => {
const resolvedPath = pathLib.resolve(path, file);
const resolvedPath = pathLib.resolve(config.path, file);
if (!fs.existsSync(resolvedPath)) {
debug('File does not seem to exist, skipping: ', file);
return { files: [], allFilesFound: [] };
}
return find(resolvedPath, ignore, filter, featureFlags, levelsDeep);
const findconfig = {
...config,
path: resolvedPath,
};
return find(findconfig);
});

const found = await Promise.all(toFind);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/plugins/get-deps-from-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export async function getDepsFromPlugin(
const levelsDeep = options.detectionDepth;
const ignore = options.exclude ? options.exclude.split(',') : [];

const { files: targetFiles, allFilesFound } = await find(
root,
const { files: targetFiles, allFilesFound } = await find({
path: root,
ignore,
multiProjectProcessors[scanType].files,
filter: multiProjectProcessors[scanType].files,
featureFlags,
levelsDeep,
);
});
debug(
`auto detect manifest files, found ${targetFiles.length}`,
targetFiles,
Expand Down
10 changes: 5 additions & 5 deletions src/lib/plugins/get-extra-project-count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export async function getExtraProjectCount(
return inspectResult.plugin.meta.allSubProjectNames.length;
}
try {
const { files: extraTargetFiles } = await find(
root,
[],
AUTO_DETECTABLE_FILES,
);
const { files: extraTargetFiles } = await find({
path: root,
ignore: [],
filter: AUTO_DETECTABLE_FILES,
});
const foundProjectsCount =
extraTargetFiles.length > 1 ? extraTargetFiles.length - 1 : undefined;
return foundProjectsCount;
Expand Down
3 changes: 1 addition & 2 deletions src/lib/plugins/get-single-plugin-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export async function getSinglePluginResult(
options: Options & (TestOptions | MonitorOptions),
targetFile?: string,
): Promise<pluginApi.InspectResult> {
let plugin: types.Plugin;
plugin = plugins.loadPlugin(options.packageManager);
const plugin: types.Plugin = plugins.loadPlugin(options.packageManager);
const moduleInfo = ModuleInfo(plugin, options.policy);
const inspectRes: pluginApi.InspectResult = await moduleInfo.inspect(
root,
Expand Down

0 comments on commit ab7ad73

Please sign in to comment.