improve
This commit is contained in:
parent
9c710ba815
commit
9ef4697326
|
@ -250,6 +250,7 @@ export async function moveFile(
|
|||
throw new Error('File does not exist');
|
||||
}
|
||||
if (existsSync(destinationPath)) {
|
||||
// TODO: should we overwrite or ignore as no-op or throw error?
|
||||
throw new Error('Destination file already exists');
|
||||
}
|
||||
// check if destination folder exists
|
||||
|
@ -267,9 +268,16 @@ export async function moveFolder(
|
|||
if (!existsSync(sourcePath)) {
|
||||
throw new Error('Folder does not exist');
|
||||
}
|
||||
|
||||
// check if destination folder exists
|
||||
if (!existsSync(destinationPath)) {
|
||||
await fs.mkdir(destinationPath, { recursive: true });
|
||||
}
|
||||
|
||||
// if source folder has files, first move them to destination folder
|
||||
const files = await fs.readdir(sourcePath);
|
||||
for (const file of files) {
|
||||
try {
|
||||
const sourceFilePath = path.join(sourcePath, file);
|
||||
const destinationFilePath = path.join(destinationPath, file);
|
||||
// check if source file is a folder
|
||||
|
@ -277,13 +285,10 @@ export async function moveFolder(
|
|||
if (isFolder.isDirectory()) {
|
||||
await moveFolder(sourceFilePath, destinationFilePath);
|
||||
} else {
|
||||
try {
|
||||
await moveFile(sourceFilePath, destinationFilePath);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.message === 'Destination file already exists') {
|
||||
await fs.unlink(sourceFilePath);
|
||||
}
|
||||
}
|
||||
logError(e, 'failed to move file');
|
||||
}
|
||||
}
|
||||
// delete source folder
|
||||
|
|
Loading…
Reference in a new issue