removeFromArbSpecificKeys static method

Future<void> removeFromArbSpecificKeys({
  1. required String arbDirectory,
  2. required Map<String, int> analyzedArb,
  3. bool verbose = false,
})

Implementation

static Future<void> removeFromArbSpecificKeys({
  required String arbDirectory,
  required Map<String, int> analyzedArb,
  bool verbose = false,
}) async {
  final Directory directory =
      Directory(join(Directory.current.path, arbDirectory));

  if (!await directory.exists()) {
    throw Exception("Directory does not exist: ${directory.path}");
  }

  final keysToRemove = analyzedArb.entries
      .where((element) => element.value == 0)
      .map((e) => e.key);

  print(keysToRemove);
  var encoder = JsonEncoder.withIndent("     ");

  await for (var entity in directory.list(recursive: false)) {
    if (entity is File && entity.path.endsWith(FileConstants.arbFormat)) {
      final arb = jsonDecode(File(entity.path).readAsStringSync())
          as Map<String, dynamic>;

      for (var key in keysToRemove) {
        arb.remove(key);
      }

      if (verbose) {
        print(grey('\tUpdating: ${entity.path}'));
      }
      File(entity.path).writeAsStringSync(encoder.convert(arb));
    }
  }
}