readPackageName static method

String? readPackageName()

Reads the project name from pubspec.yaml, or null if absent.

Implementation

static String? readPackageName() {
  final pubspec = File('pubspec.yaml');
  if (!pubspec.existsSync()) return null;
  for (final line in pubspec.readAsLinesSync()) {
    final match = RegExp(r'^name:\s*(.+)$').firstMatch(line.trimRight());
    if (match != null) return match.group(1)!.trim();
  }
  return null;
}