discover static method

Workspace? discover(
  1. String start
)

Finds the workspace that contains start.

The current package is the nearest directory at or above start with a pubspec.yaml. The workspace root is the nearest directory at or above that package whose pubspec.yaml workspace list, or whose dpk.yaml workspace globs, include the package. A package that no workspace includes is its own root.

Returns null when no directory at or above start has a pubspec.yaml.

Implementation

static Workspace? discover(String start) {
  final startPath = canonicalPath(start);
  final currentPath = _nearestPackageDirectory(startPath);
  if (currentPath == null) {
    return null;
  }

  for (final candidate in _selfAndAncestors(currentPath)) {
    final members = workspaceMemberPaths(candidate);
    if (members == null) {
      continue;
    }
    if (candidate != currentPath && !members.contains(currentPath)) {
      continue;
    }

    final root = WorkspacePackage.read(candidate, root: candidate)!;
    final packages = [
      for (final member in members)
        ?WorkspacePackage.read(member, root: candidate),
    ];
    final current = currentPath == candidate
        ? root
        : packages.firstWhere((package) => package.path == currentPath);
    return Workspace(root: root, packages: packages, current: current);
  }

  final package = WorkspacePackage.read(currentPath, root: currentPath);
  if (package == null) {
    return null;
  }
  return Workspace(root: package, packages: const [], current: package);
}