validateDeletion static method

String? validateDeletion(
  1. CacheTarget target,
  2. String projectRoot
)

Validates that a path is safe to delete. Returns null if safe, or an error message if not.

Implementation

static String? validateDeletion(CacheTarget target, String projectRoot) {
  // Resolve absolute paths
  final targetPath = PathUtils.resolveAbsolute(target.path);
  final projectRootPath = PathUtils.resolveAbsolute(projectRoot);

  if (targetPath == null) {
    return 'Target path does not exist or cannot be resolved: ${target.path}';
  }

  if (projectRootPath == null) {
    return 'Project root does not exist or cannot be resolved: $projectRoot';
  }

  // Check if target type is in allowlist
  if (!safeTargetTypes.contains(target.type)) {
    return 'Target type "${target.type}" is not in the safe allowlist';
  }

  // For global targets, verify they match expected global cache locations
  if (target.isGlobal) {
    if (!_isValidGlobalCachePath(targetPath, target.type)) {
      return 'Global cache path does not match expected location for type "${target.type}"';
    }
  } else {
    // For project targets, verify they are within the project root
    if (!PathUtils.isWithin(targetPath, projectRootPath)) {
      return 'Target path is not within project root: $targetPath';
    }
  }

  // Verify the path actually exists
  if (!PathUtils.isDirectory(targetPath) && !PathUtils.isFile(targetPath)) {
    return 'Target path does not exist: $targetPath';
  }

  return null; // Safe to delete
}