copyFile function

CopyResult copyFile(
  1. String sourcePath,
  2. String destinationPath,
  3. bool overwrite, [
  4. String transformContent(
    1. String
    )?,
])

Implementation

CopyResult copyFile(
  String sourcePath,
  String destinationPath,
  bool overwrite, [
  String Function(String)? transformContent,
]) {
  if (!File(sourcePath).existsSync()) {
    return CopyResult(
      success: false,
      message: 'Source file not found: $sourcePath',
    );
  }

  if (File(destinationPath).existsSync() && !overwrite) {
    return CopyResult(
      success: false,
      message: 'File already exists: $destinationPath. Skipping...',
      skipped: true,
    );
  }

  try {
    final destDir = path.dirname(destinationPath);
    ensureDirectoryExists(destDir);

    var content = File(sourcePath).readAsStringSync();

    if (transformContent != null) {
      content = transformContent(content);
    }

    final destFile = File(destinationPath);
    destFile.writeAsStringSync(content);

    return CopyResult(
      success: true,
      message: '✓ Created $destinationPath',
    );
  } catch (e) {
    return CopyResult(
      success: false,
      message: 'Failed to copy file: $e',
    );
  }
}