discover static method
Finds a project's bundles from from.
With searchAncestors false, the default, from is the project root,
as a host knows the workspace or worktree it opens, and discovery reads
nothing above it, not even to find a Git boundary: the bundles are the
applies_to paths of its own wayfinder.json, or, without one, its
knowledge/ directory.
With searchAncestors true, as the wayfinder command searches from
its working directory, discovery finds the nearest wayfinder.json at
or above from and its applies_to paths. Without one, the nearest
knowledge/ directory below the Git repository boundary is the
project's one bundle.
bundleName keeps only the bundle whose folder has that name. A
from that does not exist or cannot be read is bundles-missing.
Implementation
static Future<WayfinderProject> discover(
Directory from, {
String? bundleName,
bool searchAncestors = false,
}) async {
if (bundleName != null && bundleName.trim().isEmpty) {
throw const WayfinderException(
'bundle-name-invalid',
'--bundle must be a nonblank bundle name.',
);
}
final Directory start;
try {
start = Directory(await from.resolveSymbolicLinks());
} on FileSystemException catch (error) {
throw WayfinderException(
'bundles-missing',
'No project bundles found in ${from.path}: it does not exist or '
'cannot be read (${error.osError?.message ?? error.message}).',
);
}
final (directory, config, paths) = searchAncestors
? await _nearest(start)
: await _atRoot(start);
paths.sort();
final selected = paths
.where((path) => bundleName == null || p.basename(path) == bundleName)
.toList();
if (selected.isEmpty) {
throw WayfinderException(
'bundle-unknown',
'Unknown bundle "$bundleName". Available: '
'${paths.map(p.basename).join(', ')}.',
);
}
if (bundleName != null && selected.length > 1) {
throw WayfinderException(
'bundle-ambiguous',
'Bundle name "$bundleName" is ambiguous: ${selected.join(', ')}. '
'Pass an explicit bundle path.',
);
}
final roots = <String>{};
final bundles = <ProjectBundle>[];
for (final path in selected) {
final bundle = Directory(p.join(directory.path, path));
if (!await bundle.exists()) {
throw WayfinderException(
'bundle-missing',
'Configured bundle does not exist: $path.',
);
}
final root = await bundle.resolveSymbolicLinks();
if (!p.isWithin(directory.path, root)) {
throw WayfinderException(
'bundle-escapes',
'Configured bundle leaves the project: $path.',
);
}
if (roots.add(root)) {
bundles.add(ProjectBundle(p.basename(path), path, root));
}
}
return WayfinderProject._(
directory.path,
config,
List.unmodifiable(bundles),
);
}