getPubSpecValue static method

String? getPubSpecValue(
  1. String targetDir,
  2. String key
)

Gets the value of key from the pubspec.yaml file in the targetDir.

Implementation

static String? getPubSpecValue(String targetDir, String key) {
  final targetFile = File(path.join(targetDir, 'pubspec.yaml'));
  if (!targetFile.existsSync()) {
    throw FileSystemException('File not found', targetFile.path);
  }

  final yamlPath = Queue<String>.from(key.split('.'));
  final pubspec = loadYaml(targetFile.readAsStringSync());

  var node = pubspec;
  while (yamlPath.isNotEmpty) {
    dynamic part = yamlPath.removeFirst();
    if (RegExps.numberTest.hasMatch(part)) {
      part = int.parse(part);
    }
    try {
      final next = node[part];
      node = next;
    } catch (_) {
      node = null;
      break;
    }
  }

  return node;
}