copyDirectory method

Future<void> copyDirectory(
  1. Directory source,
  2. Directory destination
)

Helper to recursively copy directories while skipping environment files.

Implementation

Future<void> copyDirectory(Directory source, Directory destination) async {
  if (!source.existsSync()) {
    Logger.warning('Source directory does not exist: ${source.path}');
    return;
  }

  try {
    await for (final entity in source.list()) {
      final entityName = p.basename(entity.path);

      // Skip environment files during copy
      if (entity is File && entityName.contains('env')) {
        continue;
      }

      if (entity is Directory) {
        final newDirectory = Directory(p.join(destination.path, entityName));
        await newDirectory.create(recursive: true);
        await copyDirectory(entity, newDirectory);
      } else if (entity is File) {
        if (!entity.existsSync()) {
          Logger.warning(
              'Source file does not exist, skipping: ${entity.path}');
          continue;
        }
        await entity.copy(p.join(destination.path, entityName));
      }
    }
  } catch (e, s) {
    throw BuildException(
      'Failed copying directory from "${source.path}" to "${destination.path}"',
      fix: 'Check file permissions and target path existence.',
      originalStackTrace: s,
    );
  }
}