findRootDirectory function

String findRootDirectory(
  1. String currentPath
)

Finds the root directory of the current Dart project.

This function traverses up the directory tree from the current path until it finds a directory containing a pubspec.yaml file, which indicates the root of a Dart project.

This is useful for locating project-specific directories like dump folders or configuration files relative to the project root.

Parameters:

  • currentPath: The starting path to search from

Returns:

  • The absolute path to the project root directory

Example:

String root = findRootDirectory('/path/to/project/lib/utils');
// Returns: /path/to/project

Implementation

String findRootDirectory(String currentPath) {
  /// Traverse up the directory tree until pubspec.yaml is found
  while (!File(path.join(currentPath, 'pubspec.yaml')).existsSync()) {
    currentPath = path.dirname(currentPath);
  }
  return currentPath;
}