copyDirRec function

Stream<FileLog> copyDirRec(
  1. Directory dirIn,
  2. Directory dirOut, {
  3. bool isCopyFile = true,
  4. bool filter(
    1. String
    )?,
})

Recursively copying a file with its contents.

If a file with the same name exists, copying will not be executed.

If isCopyFile is false, so copies only tree directorias without file.

Function filter takes the file path and returned bool type.

Implementation

Stream<FileLog> copyDirRec(
  Directory dirIn,
  Directory dirOut, {
  bool isCopyFile = true,
  bool Function(String)? filter,
}) async* {
  if (!dirOut.existsSync()) {
    dirOut.createSync();
  }

  await for (final entitie in dirIn.list()) {
    if (isCopyFile && entitie.statSync().type == FileSystemEntityType.file) {
      if (!File("${dirOut.path}/${entitie.uri.pathSegments.last}")
          .existsSync()) {
        if (filter != null && !filter(entitie.path)) continue;
        final file = File(entitie.path);
        final fileCopy =
            file.copySync("${dirOut.path}/${entitie.uri.pathSegments.last}");
        yield FileLog(
          file1: file,
          file2: fileCopy,
          action: Action.copy,
        );
      }
    } else if (entitie.statSync().type == FileSystemEntityType.directory) {
      yield* copyDirRec(
        Directory("${dirIn.path}/${basename(entitie.path)}"),
        Directory("${dirOut.path}/${basename(entitie.path)}"),
        isCopyFile: isCopyFile,
        filter: filter,
      );
    }
  }
}