Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions backend/src/routes/proceed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,31 @@ function removeEmptyDirs(dir: string, stopAt: string): void {
}
}

// Recursively remove ALL empty directories inside root (post-order traversal)
function removeAllEmptyDirs(dir: string): void {
// Recursively remove ALL empty directories inside root (post-order traversal),
// but never remove root itself.
function removeAllEmptyDirs(dir: string, stopAt: string): void {
if (dir === stopAt) {
// Clean up children but never delete the root itself
try {
const entries = fs.readdirSync(dir);
for (const entry of entries) {
const full = path.join(dir, entry);
try {
if (fs.statSync(full).isDirectory()) {
removeAllEmptyDirs(full, stopAt);
}
} catch { /* skip unreadable entries */ }
}
} catch { /* skip if dir is unreadable */ }
return;
}
try {
const entries = fs.readdirSync(dir);
for (const entry of entries) {
const full = path.join(dir, entry);
try {
if (fs.statSync(full).isDirectory()) {
removeAllEmptyDirs(full);
removeAllEmptyDirs(full, stopAt);
}
} catch { /* skip unreadable entries */ }
}
Expand Down Expand Up @@ -87,7 +103,7 @@ router.post('/', (req: Request, res: Response) => {
const processNext = (i: number): void => {
if (i >= rows.length) {
if (deleteEmptyFolders) {
removeAllEmptyDirs(sourceDir);
removeAllEmptyDirs(sourceDir, sourceDir);
}
emit({ type: 'done', ...result });
res.end();
Expand Down