findLibraryFile function

String? findLibraryFile(
  1. String searchDir,
  2. String libraryName
)

Recursively searches a directory for a file containing a specific library directive.

Implementation

String? findLibraryFile(String searchDir, String libraryName) {
  final dir = Directory(searchDir);
  if (!dir.existsSync()) return null;

  final searchPattern = 'library $libraryName;';

  final entries = dir.listSync(recursive: true, followLinks: false);
  for (final entity in entries) {
    if (entity is File && entity.path.endsWith('.dart')) {
      try {
        final content = entity.readAsStringSync();
        if (content.contains(searchPattern)) {
          return entity.path;
        }
      } catch (e) {
        // Ignore files that can't be read.
      }
    }
  }
  return null;
}