run method

Future<int> run()

Runs the clean command.

Implementation

Future<int> run() async {
  if (!apply) {
    printError(
        'Error: --apply flag is required to perform deletions. This is a safety measure.');
    printError('Run with --apply to actually delete files.');
    return 1;
  }

  if (priorityRoots.isEmpty && !includeDefaults) {
    printError('Error: No scan roots specified. Use --root or --include-defaults');
    return 1;
  }

  printVerbose('Scanning for Flutter projects and caches...');

  try {
    // First, scan to find what will be deleted
    final scanResult = await CacheScanner.scan(
      priorityRoots: priorityRoots,
      includeDefaults: includeDefaults,
      includeOptional: includeOptional,
      includeGlobal: includeGlobal,
      maxDepth: maxDepth,
    );

    if (scanResult.totalSize == 0) {
      printMessage('No cache files found to clean.');
      return 0;
    }

    // Show summary
    _printSummary(scanResult);

    // Confirm deletion
    if (!yes) {
      stdout.write('Do you want to proceed with deletion? (yes/no): ');
      final response = stdin.readLineSync()?.toLowerCase().trim();
      if (response != 'yes' && response != 'y') {
        printMessage('Cancelled.');
        return 0;
      }
    }

    // Perform cleaning
    printMessage('');
    printMessage('Cleaning caches...');
    printVerbose('Move to trash: $moveToTrash');

    final cleaner = CacheCleaner(moveToTrash: moveToTrash);
    final cleanResult = await _performCleaning(scanResult, cleaner);

    // Print results
    if (jsonOutput) {
      _printJsonResult(cleanResult);
    } else {
      _printHumanReadableResult(cleanResult);
    }

    return cleanResult.failedPaths.isEmpty ? 0 : 1;
  } catch (e) {
    printError('Error during cleaning: $e');
    return 1;
  }
}