tryFetchLatestCliVersion static method

Future<PackageVersionData?> tryFetchLatestCliVersion({
  1. String? localStoragePath,
  2. required CommandLogger logger,
})

Implementation

static Future<PackageVersionData?> tryFetchLatestCliVersion({
  String? localStoragePath,
  required final CommandLogger logger,
}) async {
  localStoragePath ??= localStorageDirectory.path;

  void deleteFile(final File file) {
    try {
      file.deleteSync();
    } catch (e) {
      // Ignore since users can't do anything about it.
      logger.debug(
        'Failed to store latest cli version to file: $e',
      );
    }
  }

  try {
    return await LocalStorageManager.tryFetchAndDeserializeJsonFile(
      fileName: ResourceManagerConstants.latestVersionFilePath,
      localStoragePath: localStoragePath,
      fromJson: PackageVersionData.fromJson,
    );
  } on ReadException catch (e) {
    deleteFile(e.file);
  } on DeserializationException catch (e) {
    deleteFile(e.file);
  }

  return null;
}