getScripts method

Map<String, String> getScripts([
  1. String? pubspecPath
])

Parse scripts from pubspec.yaml

Implementation

Map<String, String> getScripts([String? pubspecPath]) {
  final File? pubspec = pubspecPath != null ? File(pubspecPath) : findPubspec();
  if (pubspec == null || !pubspec.existsSync()) return <String, String>{};

  try {
    final String content = pubspec.readAsStringSync();
    final YamlMap? yaml = loadYaml(content) as YamlMap?;
    if (yaml == null) return <String, String>{};

    final dynamic scripts = yaml['scripts'];
    if (scripts == null || scripts is! YamlMap) return <String, String>{};

    return Map<String, String>.fromEntries(
      scripts.entries.map((MapEntry<dynamic, dynamic> e) => MapEntry<String, String>(e.key.toString(), e.value.toString())),
    );
  } on Exception catch (e) {
    error('Failed to parse pubspec.yaml: $e');
    return <String, String>{};
  }
}