feat(fs): Enhanced copy method with optional replaceTargetDir option for directory replacement

This commit is contained in:
Philipp Kunz 2025-01-29 18:23:54 +01:00
parent 8dceea67be
commit e78682d9b4
3 changed files with 13 additions and 7 deletions

View File

@ -1,5 +1,11 @@
# Changelog # 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) ## 2025-01-29 - 11.1.9 - fix(fs)
Fix directory handling in copy and copySync functions Fix directory handling in copy and copySync functions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartfile', 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.' description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.'
} }

View File

@ -74,21 +74,21 @@ export const isFile = (pathArg): boolean => {
/** /**
* copies a file or directory from A to B on the local disk * copies a file or directory from A to B on the local disk
*/ */
export const copy = async (fromArg: string, toArg: string): Promise<void> => { export const copy = async (fromArg: string, toArg: string, optionsArg?: plugins.fsExtra.CopyOptions & { replaceTargetDir?: boolean }): Promise<void> => {
if (isDirectory(fromArg) && isDirectory(toArg)) { if (optionsArg?.replaceTargetDir && isDirectory(fromArg) && isDirectory(toArg)) {
await remove(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 * copies a file or directory SYNCHRONOUSLY from A to B on the local disk
*/ */
export const copySync = (fromArg: string, toArg: string): void => { export const copySync = (fromArg: string, toArg: string, optionsArg?: plugins.fsExtra.CopyOptionsSync & { replaceTargetDir?: boolean }): void => {
if (isDirectory(fromArg) && isDirectory(toArg)) { if (optionsArg?.replaceTargetDir && isDirectory(fromArg) && isDirectory(toArg)) {
removeSync(toArg); removeSync(toArg);
} }
return plugins.fsExtra.copySync(fromArg, toArg, {overwrite: true}); return plugins.fsExtra.copySync(fromArg, toArg, optionsArg as plugins.fsExtra.CopyOptionsSync);
}; };
/** /**