findConfig method

Future<ConfigMatchDetails?> findConfig()

Search for the configuration file, and return the path and configuration contents.

Returns null if no configuration was found.

Implementation

Future<ConfigMatchDetails?> findConfig() async {
  final parsers = this.parsers.map((s) => s.copyWith(fs: fs)).toList();
  final patterns = filenamePatterns
      .map((p) => RegExp(p.replaceAll('{name}', name)))
      .toList();
  final searchPaths = paths;
  final tried = <String>{};

  for (final pathname in searchPaths) {
    final dirPath = p.isRelative(pathname) ? p.absolute(pathname) : pathname;
    final dir = fs.directory(dirPath);
    final dirExists = await dir.exists();
    if (!dirExists) {
      continue;
    }
    await for (final entity in dir.list()) {
      if (entity is! File || tried.contains(entity.path)) {
        continue;
      }
      final path = entity.path;
      final filename = p.basename(path);
      final isPathInPatterns = patterns.any((regex) =>
          regex.allMatches(path).isNotEmpty ||
          regex.allMatches(filename).isNotEmpty);
      if (!isPathInPatterns) {
        continue;
      }

      for (final parser in parsers) {
        if (!parser.matches(filename) || tried.contains(path)) {
          continue;
        }
        final config = await parser.search(name, path);
        if (config == null) {
          tried.add(path);
          continue;
        }
        return ConfigMatchDetails(path, config);
      }
    }
  }
  return null;
}