tryResolveTracePath method

Future<String?> tryResolveTracePath(
  1. String path
)
inherited

Override to resolve --trace latest or other special trace path values. Return a file system path, or null if not handled.

The default implementation recognizes latest, traces/latest, and traces/latest.log and searches ./traces/ for the newest .log file.

Implementation

Future<String?> tryResolveTracePath(String path) async {
  final trimmed = path.trim();
  if (trimmed.isEmpty) return null;

  final latestAlias =
      trimmed == 'latest' ||
      trimmed == 'traces/latest' ||
      trimmed == 'traces/latest.log';
  if (!latestAlias) {
    if (io.File(trimmed).existsSync()) return trimmed;
    return null;
  }

  final candidates = <io.File>[];
  final dir = io.Directory('traces');
  if (await dir.exists()) {
    await for (final entity in dir.list(followLinks: false)) {
      if (entity is io.File && entity.path.endsWith('.log')) {
        candidates.add(entity);
      }
    }
  }

  if (candidates.isEmpty) return null;
  candidates.sort(
    (a, b) => b.lastModifiedSync().compareTo(a.lastModifiedSync()),
  );
  return candidates.first.path;
}