findFiles method

Future<List<String>> findFiles(
  1. String globPattern, {
  2. String? rootDir,
  3. List<String> excludes = const [],
})

Find files matching globPattern under rootDir (defaults to projectRoot).

Implementation

Future<List<String>> findFiles(
  String globPattern, {
  String? rootDir,
  List<String> excludes = const [],
}) async {
  final root = rootDir ?? projectRoot;
  final dir = Directory(root);
  if (!await dir.exists()) return const [];

  final regex = _globToRegex(globPattern);
  final results = <String>[];

  await for (final entity in dir.list(recursive: true)) {
    if (entity is! File) continue;
    final relative = entity.path.substring(root.length + 1);
    if (!regex.hasMatch(relative)) continue;
    if (excludes.any((ex) => _globToRegex(ex).hasMatch(relative))) continue;
    results.add(entity.path);
  }

  return results;
}