copyDirectory method

Future<void> copyDirectory({
  1. required Directory source,
  2. required Directory destination,
  3. bool? isMainDest,
})

Implementation

Future<void> copyDirectory({
  required Directory source,
  required Directory destination,
  bool? isMainDest,
}) 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(platformPathSaperator).last.startsWith('.')) {
        continue;
      }
      if (entity is Directory) {
        String directoryName = entity.path.split(platformPathSaperator).last;
        if (directoryName.containsAvoidableDirectory) continue;
        directoryName = directoryName.toSnakeCase;
        String newPath =
            '${destination.path}$platformPathSaperator$directoryName';
        ColoredLog.yellow(entity.path, name: 'New Directory');

        await copyDirectory(source: entity, destination: Directory(newPath));
      } else if (entity is File) {
        ColoredLog.blue(entity.path, name: 'File');
        String fileName = entity.uri.pathSegments.last;
        if (fileName.containsAvoidableFiles) continue;

        fileName = fileName.replaceAll(
          defaultName.toSnakeCase,
          moduleName.toSnakeCase,
        );

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