saveGlobalConfig method

void saveGlobalConfig(
  1. GlobalConfig updater(
    1. GlobalConfig current
    )
)

Save the global config with an updater function.

Implementation

void saveGlobalConfig(GlobalConfig Function(GlobalConfig current) updater) {
  final current = getGlobalConfig();
  final updated = updater(current);
  if (identical(updated, current)) return;

  // Auth-loss guard.
  if (_wouldLoseAuthState(current, updated)) return;

  try {
    final dir = Directory(p.dirname(configFilePath));
    if (!dir.existsSync()) dir.createSync(recursive: true);

    // Remove project history to avoid config bloat.
    final toWrite = updated.toJson();
    _removeProjectHistory(toWrite);

    File(
      configFilePath,
    ).writeAsStringSync(const JsonEncoder.withIndent('  ').convert(toWrite));
    _cache = updated;
    _cacheMtime = DateTime.now();
    _writeCount++;
  } catch (e) {
    // Log but don't throw.
  }
}