findPubspecYaml function

File? findPubspecYaml(
  1. Directory currentDir
)

Searches up the directory tree until it finds the pubspec.yaml file. Returns null if pubspec.yaml is not found.

Implementation

File? findPubspecYaml(Directory currentDir) {
  if (!currentDir.isAbsolute) {
    currentDir = Directory(normalize(currentDir.absolute.path));
  }
  final rootDir = split(currentDir.path).first;
  while (currentDir.path != rootDir) {
    final currentDirItems =
        currentDir.listSync(recursive: false, followLinks: false);
    final pubspecYaml = currentDirItems
        .whereType<File>()
        .where((file) => basename(file.path).toLowerCase() == 'pubspec.yaml');
    if (pubspecYaml.length == 1) {
      return pubspecYaml.first;
    } else {
      currentDir = currentDir.parent;
    }
  }
  return null;
}