From e78682d9b4be0358ba1e144fe681c5d40c3ba169 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Wed, 29 Jan 2025 18:23:54 +0100 Subject: [PATCH] feat(fs): Enhanced copy method with optional replaceTargetDir option for directory replacement --- changelog.md | 6 ++++++ ts/00_commitinfo_data.ts | 2 +- ts/fs.ts | 12 ++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index 6f03b5d..73be34e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2025-01-29 - 11.2.0 - feat(fs) +Enhanced copy method with optional replaceTargetDir option for directory replacement + +- Added optional 'replaceTargetDir' option to 'copy' and 'copySync' methods in 'fs.ts'. +- The 'replaceTargetDir' option allows replacing the target directory if both source and target are directories. + ## 2025-01-29 - 11.1.9 - fix(fs) Fix directory handling in copy and copySync functions diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 222462a..cd58bdf 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartfile', - version: '11.1.9', + version: '11.2.0', description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.' } diff --git a/ts/fs.ts b/ts/fs.ts index e92c000..5e4b0ae 100644 --- a/ts/fs.ts +++ b/ts/fs.ts @@ -74,21 +74,21 @@ export const isFile = (pathArg): boolean => { /** * copies a file or directory from A to B on the local disk */ -export const copy = async (fromArg: string, toArg: string): Promise => { - if (isDirectory(fromArg) && isDirectory(toArg)) { +export const copy = async (fromArg: string, toArg: string, optionsArg?: plugins.fsExtra.CopyOptions & { replaceTargetDir?: boolean }): Promise => { + if (optionsArg?.replaceTargetDir && isDirectory(fromArg) && isDirectory(toArg)) { await remove(toArg); } - return await plugins.fsExtra.copy(fromArg, toArg, {overwrite: true}); + return await plugins.fsExtra.copy(fromArg, toArg, optionsArg as plugins.fsExtra.CopyOptions); }; /** * copies a file or directory SYNCHRONOUSLY from A to B on the local disk */ -export const copySync = (fromArg: string, toArg: string): void => { - if (isDirectory(fromArg) && isDirectory(toArg)) { +export const copySync = (fromArg: string, toArg: string, optionsArg?: plugins.fsExtra.CopyOptionsSync & { replaceTargetDir?: boolean }): void => { + if (optionsArg?.replaceTargetDir && isDirectory(fromArg) && isDirectory(toArg)) { removeSync(toArg); } - return plugins.fsExtra.copySync(fromArg, toArg, {overwrite: true}); + return plugins.fsExtra.copySync(fromArg, toArg, optionsArg as plugins.fsExtra.CopyOptionsSync); }; /**