prefixedPackagesIn function

List<Pubspec> prefixedPackagesIn(
  1. String workspaceRoot,
  2. String prefix, {
  3. int maxDepth = 3,
})

Every package whose directory is named prefix* under workspaceRoot, searched to maxDepth so a moved repository is found without walking the whole workspace.

Implementation

List<Pubspec> prefixedPackagesIn(
  String workspaceRoot,
  String prefix, {
  int maxDepth = 3,
}) {
  final found = <Pubspec>[];
  void walk(Directory dir, int depth) {
    if (depth > maxDepth) return;
    List<FileSystemEntity> children;
    try {
      children = dir.listSync();
    } on FileSystemException {
      return;
    }
    for (final entity in children.whereType<Directory>()) {
      final name = p.basename(entity.path);
      if (name.startsWith('.') || name == 'node_modules') continue;
      if (name.startsWith(prefix)) {
        final pubspec = Pubspec.read(entity.path);
        if (pubspec != null) {
          found.add(pubspec);
          continue;
        }
      }
      walk(entity, depth + 1);
    }
  }

  walk(Directory(workspaceRoot), 0);
  found.sort((a, b) => a.name.compareTo(b.name));
  return found;
}