findFilesImporting method

Future<List<String>> findFilesImporting(
  1. String projectPath,
  2. String packageName
)

Fast import scan: returns files under projectPath that contain an import of packageName.

This reads raw file text and matches import directives via regex. No AST parsing is performed, so it is much faster than searchApiUsages.

Implementation

Future<List<String>> findFilesImporting(
  String projectPath,
  String packageName,
) async {
  final allFiles = await findDartFiles(projectPath);
  final importPattern = RegExp(
    r'''import\s+['"]package:''' +
        RegExp.escape(packageName) +
        r'''[/'"]''',
  );

  final matching = <String>[];
  for (final filePath in allFiles) {
    try {
      final content = await File(filePath).readAsString();
      if (importPattern.hasMatch(content)) {
        matching.add(filePath);
      }
    } catch (e) {
      Logger.debug('Could not read $filePath: $e');
    }
  }

  Logger.debug(
    'Found ${matching.length} files importing package:$packageName',
  );
  return matching;
}