copyFileToSync static method

void copyFileToSync(
  1. String path,
  2. String pathTarget, {
  3. bool showLog = true,
  4. bool targetIsDirectory = false,
})

文件拷贝

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

Implementation

static void copyFileToSync(
  String path,
  String pathTarget, {
  bool showLog = true,
  bool targetIsDirectory = false,
}) {
  if (!existsSync(path) || !isFileSync(path)) {
    return;
  }
  if (showLog) {
    final samePath = TextUtil.getSameTextInTwoStr(path, pathTarget);
    Logger.info(
      'copy',
      '${path.replaceFirst(samePath, '')} -> ${pathTarget.replaceFirst(samePath, '')}',
    );
  }
  // 增加处理 - 判断是否需要创建目录
  String mergedPathTarget = pathTarget;
  if (targetIsDirectory) {
    if (!existsSync(pathTarget)) {
      mkdirSync(pathTarget, showLog: showLog);
    }
    final fileName = path.substring(path.lastIndexOf(PathUtil.separator) + 1);
    mergedPathTarget = PathUtil.join(pathTarget, fileName);
  } else {
    final targetDir = PathUtil.dirname(pathTarget);
    if (!existsSync(targetDir)) {
      mkdirSync(targetDir, showLog: showLog);
    }
  }

  File(path).copySync(mergedPathTarget);
}