selectSpecificFiles method

String selectSpecificFiles()

Selects specific files from a directory.

This method allows the user to select specific files based on certain criteria.

Returns a String representing the selected files.

Implementation

String selectSpecificFiles() {
  final currentDir = Directory.current;

  final filesByPath = <String, List<String>>{};
  currentDir
      .listSync(recursive: true)
      .where((entity) => entity is File && entity.path.endsWith('.dart'))
      .forEach((file) {
    final dirPath = Directory(file.parent.path).path;
    if (!filesByPath.containsKey(dirPath)) {
      filesByPath[dirPath] = [];
    }
    filesByPath[dirPath]?.add(file.path);
  });

  if (filesByPath.isEmpty) {
    exit(1);
  }

  // Display grouped files by path
  final groupedEntries = <String>[];
  filesByPath.forEach((dir, files) {
    groupedEntries.add('📁 $dir');
    groupedEntries.addAll(files.map((file) => '  - $file').toList());
  });

  final selectedIndex = Select(
    prompt: 'Choose a Dart file:',
    options: groupedEntries,
  ).interact();

  final selectedPath = groupedEntries[selectedIndex];
  if (selectedPath.startsWith('  - ')) {
    return selectedPath.substring(4); // Return file path
  } else {
    exit(1);
  }
}