setPubspecKey function

void setPubspecKey(
  1. String key,
  2. String value, {
  3. FileSystem? fs,
  4. Directory? workingDir,
})

Implementation

void setPubspecKey(String key, String value,
    {FileSystem? fs, Directory? workingDir}) {
  final _fs = fs ?? const LocalFileSystem();
  final wd = getCurrentDir(workingDir, fs: _fs).path;
  final pubspecFile = _fs.file(path.join(wd, 'pubspec.yaml'));
  final lines = pubspecFile.readAsStringSync().split('\n');
  final index = lines.indexWhere((x) => x.startsWith('$key:'));
  lines[index] = '$key: $value';
  logger.debug('Setting $key: $value from ${pubspecFile.path}');
  pubspecFile.writeAsStringSync(lines.join('\n'));
}