getDynamicProjectRoot method

String getDynamicProjectRoot()

Implementation

String getDynamicProjectRoot() {
  Directory? candidate;

  // First candidate: directory of the running script.
  Directory scriptDir = Directory.fromUri(Platform.script).absolute;
  if (File(p.join(scriptDir.path, 'pubspec.yaml')).existsSync()) {
    candidate = scriptDir;
  }

  // Second candidate: Directory.current.
  Directory currentDir = Directory.current;
  if (candidate == null && File(p.join(currentDir.path, 'pubspec.yaml')).existsSync()) {
    candidate = currentDir;
  }
  // If neither candidate contains a pubspec.yaml, fall back with a warning.
  if (candidate == null) {
    candidate = currentDir;
  }

  if (candidate.path.trim() == '/' || candidate.path.trim().isEmpty) {
  }

  return candidate.path;
}