scan method
Scans projectRoot for direct and transitive dependencies.
Throws StateError if pubspec.yaml is missing, or if pubspec.lock is
missing (telling the user to run dart pub get first).
Implementation
List<ScannedDependency> scan(String projectRoot) {
final root = Directory(projectRoot);
final pubspecFile = File(
'${root.path}${Platform.pathSeparator}pubspec.yaml',
);
final lockFile = File('${root.path}${Platform.pathSeparator}pubspec.lock');
if (!pubspecFile.existsSync()) {
throw StateError(
'No pubspec.yaml found at ${pubspecFile.path}. '
'Pass the root of a Dart/Flutter project.',
);
}
if (!lockFile.existsSync()) {
throw StateError(
'No pubspec.lock found at ${lockFile.path}. '
'Run `dart pub get` (or `flutter pub get`) in the project first.',
);
}
final directNames = _parseDirectDependencies(
pubspecFile.readAsStringSync(),
);
final resolved = _parseLockFile(lockFile.readAsStringSync());
final results = <ScannedDependency>[];
for (final entry in resolved.entries) {
results.add(
ScannedDependency(
packageName: entry.key,
resolvedVersion: entry.value,
isDirect: directNames.contains(entry.key),
),
);
}
results.sort((a, b) => a.packageName.compareTo(b.packageName));
return results;
}