createBackup method

Future<String?> createBackup()

Implementation

Future<String?> createBackup() async {
  final timestamp = DateTime.now().toIso8601String().replaceAll(':', '-').split('.').first;
  final currentBackupPath = p.join(Constants.backupDir, timestamp);

  final filesToBackup = [
    Constants.androidBuildGradlePath,
    Constants.androidGoogleServicesPath,
    Constants.iosGoogleServicesPath,
  ];

  bool backupCreated = false;

  try {
    for (final filePath in filesToBackup) {
      final file = File(filePath);
      if (file.existsSync()) {
        final destination = File(p.join(currentBackupPath, filePath));
        destination.parent.createSync(recursive: true);
        file.copySync(destination.path);
        logger.v('Backed up $filePath to ${destination.path}');
        backupCreated = true;
      }
    }

    if (backupCreated) {
      logger.success('Backup created at: $currentBackupPath');
      return timestamp;
    }
    return null;
  } catch (e) {
    throw BackupException('Failed to create backup: $e');
  }
}