syncAppStrings function

void syncAppStrings(
  1. String projectPath,
  2. String content
)

Implementation

void syncAppStrings(String projectPath, String content) {
  final stringRegex = RegExp(r"AppStrings\.([a-zA-Z0-9_]+)");
  final matches = stringRegex.allMatches(content);
  if (matches.isEmpty) return;

  final stringsPath =
      p.join(projectPath, 'lib', 'core', 'constants', 'app_strings.dart');
  final stringsFile = File(stringsPath);
  if (!stringsFile.existsSync()) return;

  var stringsContent = stringsFile.readAsStringSync();
  var updated = false;

  for (final match in matches) {
    final key = match.group(1)!;
    if (!stringsContent.contains('static const $key ')) {
      if (!stringsContent.contains('static const $key=')) {
        // Find the last closing brace of the class
        final lastBraceIndex = stringsContent.lastIndexOf('}');
        if (lastBraceIndex != -1) {
          final readable = _camelToSentence(key);
          final newEntry = "  static const $key = \"$readable\";\n";
          stringsContent = stringsContent.substring(0, lastBraceIndex) +
              newEntry +
              stringsContent.substring(lastBraceIndex);
          updated = true;
        }
      }
    }
  }

  if (updated) {
    stringsFile.writeAsStringSync(stringsContent);
    print('✨ AppStrings updated with missing keys.');
  }
}