upload method

Future<void> upload({
  1. String? apiKey,
  2. bool publish = false,
})

Implementation

Future<void> upload({String? apiKey, bool publish = false}) async {
  apiKey ??= config.apiKey;

  if (apiKey == null) {
    error(
      1,
      'Uploading requires api-key to be provided either using pubspec.yaml '
      'or as an argument.',
    );
  }

  final files = await Directory(config.directory!)
      .list(recursive: false, followLinks: false)
      .where((f) => f.statSync().type == FileSystemEntityType.file)
      .map((f) => File(f.path))
      .toList();

  final items = <TranslationEntry>[];

  for (var item in files) {
    final match = matchPattern(path.basename(item.path), config.filename!);
    if (match == null) continue;
    final contents = await item.readAsString();
    final messages = format.parse(contents);

    messages.forEach(
      (key, value) => items.add(TranslationEntry(
        key: key,
        text: value,
        language: match.group(1)!,
      )),
    );
  }

  final result = await uploadTranslations(apiKey!, items);

  for (var item in result.data!['failtures'] ?? []) {
    stderr.writeln(item.toString());
  }

  if (result.status == 200) {
    stdout.writeln('Upload completed!');
    stdout.writeln();
    stdout.writeln('Updated: ${result.data!['numberOfUpdates']}');
    stdout.writeln('Inserted: ${result.data!['numberOfInserts']}');
  } else {
    stdout.writeln('Failed to upload! [${result.msg}]');
  }

  if (publish == true) {
    stdout.writeln();
    this.publish();
  }
}