deleteVersion method

Future<void> deleteVersion(
  1. String version,
  2. String path
)

Implementation

Future<void> deleteVersion(String version, String path) async {
  final normPath = _normalizePath(path);
  final draft = _versionDir(version);
  final file = _artifactFile(draft, normPath);
  if (await file.exists()) await file.delete();

  final key = _shardKey(normPath);
  final routeShard = File(
    '${_routeShardsDir(draft).path}${Platform.pathSeparator}$key.json',
  );
  if (await routeShard.exists()) {
    try {
      final decoded = jsonDecode(await routeShard.readAsString()) as Map;
      final map = Map<String, Object?>.from(decoded);
      if (map.remove(normPath) != null) {
        if (map.isEmpty) {
          await routeShard.delete();
        } else {
          await _writeJsonFile(routeShard, map);
        }
      }
    } catch (_) {}
  }

  final depShard = File(
    '${_depShardsDir(draft).path}${Platform.pathSeparator}$key.json',
  );
  if (await depShard.exists()) {
    try {
      final decoded = jsonDecode(await depShard.readAsString()) as Map;
      final map = Map<String, Object?>.from(decoded);
      if (map.remove(normPath) != null) {
        if (map.isEmpty) {
          await depShard.delete();
        } else {
          await _writeJsonFile(depShard, map);
        }
      }
    } catch (_) {}
  }

  final entries = await _loadRouteArtifacts(draft);
  if (entries.remove(normPath) != null) {
    await _saveRouteArtifacts(draft, entries);
  }
}