copyDirectoryToSync static method

void copyDirectoryToSync(
  1. String path,
  2. String pathTarget, {
  3. String? rootPath,
  4. bool showLog = true,
})

目录拷贝

path 源文件路径 pathTarget 目标文件路径(需要包括文件名称) showLog 是否展示消息日志

Implementation

static void copyDirectoryToSync(
  String path,
  String pathTarget, {
  String? rootPath,
  bool showLog = true,
}) {
  if (!existsSync(path) || !isDirectorySync(path)) {
    return;
  }
  if (showLog) {
    final samePath = TextUtil.getSameTextInTwoStr(path, pathTarget);
    Logger.info(
      'copy dir',
      '${path.replaceFirst(samePath, '')} -> ${pathTarget.replaceFirst(samePath, '')}',
    );
  }
  Future<void> runCopy(
    String rPath,
    String rPathTarget, {
    bool inShowLog = true,
  }) async {
    final diff = PathUtil.relative(rPath, from: path);
    final mergedPathTarget = PathUtil.join(pathTarget, diff);
    final fileList = Directory(rPath).listSync();
    for (final f in fileList) {
      if (isDirectorySync(f.path)) {
        runCopy(
          f.path,
          pathTarget,
          inShowLog: false,
        );
      } else if (isFileSync(f.path)) {
        copyFileToSync(
          f.path,
          mergedPathTarget,
          targetIsDirectory: true,
          showLog: false,
        );
      }
    }
  }

  runCopy(path, pathTarget);
}