export function findParentWithFile()

in eng/tools/spec-gen-sdk-runner/src/utils.ts [169:194]


export function findParentWithFile(
  startPath: string,
  searchFile: RegExp,
  specRepoFolder: string,
  stopAtFolder?: string,
): string | undefined {
  let currentPath = startPath;

  while (currentPath) {
    try {
      const absolutePath = path.resolve(specRepoFolder, currentPath);
      const files = fs.readdirSync(absolutePath);
      if (files.some((file) => searchFile.test(file))) {
        return currentPath;
      }
    } catch (error) {
      logMessage(`Error reading directory: ${currentPath} with ${error}`, LogLevel.Warn);
      return undefined;
    }
    currentPath = path.dirname(currentPath);
    if (stopAtFolder && currentPath === stopAtFolder) {
      return undefined;
    }
  }
  return undefined;
}