copyDirectory method

void copyDirectory(
  1. String sourceDir,
  2. String destinationDir
)

Recursively copies a directory's contents.

Implementation

void copyDirectory(String sourceDir, String destinationDir) {
  try {
    final source = Directory(sourceDir);
    if (!source.existsSync()) {
      throw CliException('Source directory does not exist: $sourceDir');
    }

    final destination = Directory(destinationDir);
    if (!destination.existsSync()) {
      destination.createSync(recursive: true);
    }

    source.listSync(recursive: true).forEach((entity) {
      final relativePath = p.relative(entity.path, from: sourceDir);
      final newPath = p.join(destinationDir, relativePath);

      if (entity is Directory) {
        Directory(newPath).createSync(recursive: true);
      } else if (entity is File) {
        entity.copySync(newPath);
      }
    });
  } catch (e) {
    throw CliException('Failed to copy directory $sourceDir to $destinationDir', e);
  }
}