findFlutterProjectRoot static method
Find the root directory of the Flutter project by searching for pubspec.yaml and lib/.
Implementation
static Directory findFlutterProjectRoot() {
Directory dir = Directory.current;
while (true) {
final pubspec = File(path.join(dir.path, 'pubspec.yaml'));
final libDir = Directory(path.join(dir.path, 'lib'));
if (pubspec.existsSync() && libDir.existsSync()) {
return dir;
}
final parent = dir.parent;
if (parent.path == dir.path) {
throw Exception(
'Flutter project root not found. '
'Make sure you run this command inside a Flutter project.',
);
}
dir = parent;
}
}