copyPathSync function

void copyPathSync(
  1. String from,
  2. String to
)

Copies all of the files in the from directory to to.

This is similar to cp -R <from> <to>:

  • Symlinks are supported.
  • Existing files are over-written, if any.
  • If to is within from, throws ArgumentError (an infinite operation).
  • If from and to are canonically the same, no operation occurs.

This action is performed synchronously (blocking I/O).

Implementation

void copyPathSync(String from, String to) {
  if (_doNothing(from, to)) {
    return;
  }
  Directory(to).createSync(recursive: true);
  for (final file in Directory(from).listSync(recursive: true)) {
    final copyTo = p.join(to, p.relative(file.path, from: from));
    if (file is Directory) {
      Directory(copyTo).createSync(recursive: true);
    } else if (file is File) {
      File(file.path).copySync(copyTo);
    } else if (file is Link) {
      Link(copyTo).createSync(file.targetSync(), recursive: true);
    }
  }
}