getAllPackageVersionsForPackageInCache static method

Map<Version, String> getAllPackageVersionsForPackageInCache(
  1. String packageName
)

Implementation

static Map<Version, String> getAllPackageVersionsForPackageInCache(
    String packageName) {
  final cacheDir = pubCacheDir;
  final hostedDir = path.join(cacheDir, 'hosted');

  final versions = <Version, String>{};

  final repositoryDirs =
      Directory(hostedDir).listSync().whereType<Directory>().toList();

  for (final repositoryDir in repositoryDirs) {
    for (final potentialPackageDir
        in repositoryDir.listSync().whereType<Directory>()) {
      final packageDirName = path.basename(potentialPackageDir.path);
      final dirParts = packageDirName.split('-').toList();
      final versionPart = dirParts.last;
      dirParts.removeLast();
      final packageNamePart = dirParts.join('-');

      if (packageNamePart != packageName) {
        continue;
      }

      final version = Version.parse(versionPart);
      versions[version] = potentialPackageDir.path;
    }
  }
  return versions;
}