findProjectRoot method

Future<String?> findProjectRoot(
  1. String startPath
)

Walks up the directory tree from startPath until a project root marker is found. Returns null if the filesystem root is reached.

Implementation

Future<String?> findProjectRoot(String startPath) async {
  var current = Directory(startPath);

  while (true) {
    for (final marker in _rootMarkers) {
      final entity =
          FileSystemEntity.isDirectorySync('${current.path}/$marker')
          ? Directory('${current.path}/$marker')
          : File('${current.path}/$marker');
      if (await entity.exists()) {
        return current.path;
      }
    }

    final parent = current.parent;
    if (parent.path == current.path) return null; // filesystem root
    current = parent;
  }
}