backupFile method

void backupFile(
  1. String filePath
)

Backs up the file at filePath to the backup folder before modification. Does not overwrite the initial backup if it already exists.

Implementation

void backupFile(String filePath) {
  try {
    final absPath = PathUtils.absolute(filePath);
    if (!FileUtils.exists(absPath)) return;

    // Ensure backup directory exists
    FileUtils.createDirectory(backupDirPath);

    final manifest = _loadManifest();
    final relativePath = PathUtils.relative(absPath, from: projectRoot);

    if (manifest.containsKey(relativePath)) {
      // Already backed up, keep original backup to prevent loss
      return;
    }

    final backupFileName = '${relativePath.replaceAll(RegExp(r'[/\\]'), '_')}.bak';
    final backupFilePath = PathUtils.join(backupDirPath, backupFileName);

    FileUtils.copy(absPath, backupFilePath);

    manifest[relativePath] = backupFileName;
    _saveManifest(manifest);
  } catch (e) {
    throw DeepLinkException('Failed to backup file $filePath', e);
  }
}