const fs = require('fs');
const path = require("path");
function getFiles(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir);
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) {
const name = `${dir}/${file}`
// Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) {
console.log(name);
cleanEmptyFoldersRecursively(name);
// If it is a directory, recursively call the getFiles function with the directory path and the files array
// getFiles(name, files)
} else {
// If it is a file, push the full path to the files array
files.push(name)
}
}
return files
}
function cleanEmptyFoldersRecursively(folder) {
var isDir = fs.statSync(folder).isDirectory();
if (!isDir) {
return;
}
var files = fs.readdirSync(folder);
if (files.length > 0) {
files.forEach(function(file) {
var fullPath = path.join(folder, file);
cleanEmptyFoldersRecursively(fullPath);
});
// re-evaluate files; after deleting subfolder
// we may have parent folder empty now
files = fs.readdirSync(folder);
}
if (files.length == 0) {
console.log("removing: ", folder);
fs.rmdirSync(folder);
return;
}
}
getFiles('files');