findServerpodHome function

String findServerpodHome({
  1. Directory? from,
})

Locates the serverpod repository root: walks up from from (defaults to the current directory) until tools/serverpod_cli is found.

For the serverpod repository's own test suites. The suites sit at varying depths (e.g. tests/serverpod_test_server is two levels below the root but the SQLite server is three), so a fixed number of .. segments would point at the wrong directory.

Implementation

String findServerpodHome({Directory? from}) {
  final start = (from ?? Directory.current).absolute;
  for (var dir = start; ; dir = dir.parent) {
    if (Directory(p.join(dir.path, 'tools', 'serverpod_cli')).existsSync()) {
      return p.canonicalize(dir.path);
    }
    if (dir.parent.path == dir.path) {
      throw StateError(
        'Could not locate the serverpod repository root: no '
        'tools/serverpod_cli directory found above ${start.path}.',
      );
    }
  }
}