expandPattern static method

List<String> expandPattern(
  1. String pattern,
  2. String root
)

Directories under root that pattern matches, as canonical paths sorted by path. A pattern without glob characters matches that directory if it exists. Glob matches inside hidden directories, build, and pub_packages are skipped.

Implementation

static List<String> expandPattern(String pattern, String root) {
  final cleaned = pattern.startsWith('./') ? pattern.substring(2) : pattern;
  if (!cleaned.contains(RegExp(r'[*?\[{]'))) {
    final path = p.join(root, cleaned);
    return Directory(path).existsSync() ? [canonicalPath(path)] : const [];
  }

  final matches = <String>[];
  for (final entity in Glob(
    cleaned,
  ).listSync(root: root, followLinks: false)) {
    if (entity is! Directory) {
      continue;
    }
    final relative = p.relative(entity.path, from: root);
    if (p.split(relative).any(_isIgnoredSegment)) {
      continue;
    }
    matches.add(canonicalPath(entity.path));
  }
  return matches..sort();
}