scan function

Future<void> scan()

Implementation

Future<void> scan() async {
  final db = await loadDB();
  final paths = await getDefaultScanPaths();

  print('🔍 Scanning for Flutter projects...'.blue);

  final loadingChars = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
  var i = 0;

  Timer? spinner = Timer.periodic(Duration(milliseconds: 100), (_) {
    stdout.write('\r${loadingChars[i++ % loadingChars.length]} Scanning...');
  });

  for (var basePath in paths) {
    final dir = Directory(basePath);
    if (!dir.existsSync()) continue;

    List<FileSystemEntity> list;
    try {
      list = await dir.list(recursive: true, followLinks: false).toList();
    } catch (_) {
      continue;
    }

    await Future.wait(
      list.whereType<File>().map((file) async {
        if (!file.path.endsWith('pubspec.yaml')) return;

        final projectPath = file.parent.path;
        final projectName = projectPath.split(Platform.pathSeparator).last;
        final existing =
            db[projectName] is Map
                ? db[projectName] as Map<String, dynamic>
                : null;

        if (existing != null &&
            existing['last_scanned'] is String &&
            existing['last_scanned'].toString().isNotEmpty &&
            DateTime.tryParse(existing['last_scanned']) != null &&
            DateTime.now()
                    .difference(DateTime.parse(existing['last_scanned']))
                    .inHours <
                24) {
          return;
        }

        if (await isFlutterProject(file)) {
          db[projectName] = {
            'path': projectPath,
            'last_scanned': DateTime.now().toIso8601String(),
            'last_opened': existing?['last_opened'] ?? '',
          };
          stdout.write('\r');
          print('🟢 Found: $projectName → $projectPath'.green);
        }
      }),
    );
  }

  spinner.cancel();
  stdout.write('\r');
  print('✅ Scanning completed.'.blue);
  await saveDB(db);
}