copyDirectory method

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

Implementation

Future<void> copyDirectory(Directory source, Directory destination) async {
  try {
    if (!await destination.exists()) {
      await destination.create(recursive: true);
      print('Directory created: ${destination.path}');
    } else {
      print('Directory already exists.');
    }

    await for (FileSystemEntity entity in source.list(recursive: false)) {
      if (entity.path.split('/').last.startsWith('.')) continue;
      if (entity is Directory) {
        String directoryName = entity.path.split('/').last;
        String newPath = '${destination.path}/$directoryName';
        ColoredLog.yellow(entity.path, name: 'New Directory');

        await copyDirectory(entity, Directory(newPath));
      } else if (entity is File) {
        ColoredLog.blue(entity.path, name: 'File');
        String fileName = entity.uri.pathSegments.last;
        fileName = fileName.replaceCaseWith(
          defaultName.toSnakeCase,
          moduleName.toSnakeCase,
        );

        File newFile = File('${destination.path}/$fileName');
        await individualFileOperations(file: entity, newPath: newFile.path);
      }
    }
  } catch (e) {
    ColoredLog.red(e, name: 'Copy Directory Failed');
  }
}