Skip to content

Commit

Permalink
Modified utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MertJSX committed Sep 3, 2024
1 parent 8988697 commit 1682dc3
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
const fs = require("fs");
const path = require("path");
const { promisify } = require('util')
const yauzl = require('yauzl');
const { mkdirp } = require('mkdirp');
const { DirItem } = require("./dir_item");
const fastFolderSize = require('fast-folder-size')


const getAllFiles = async function (dirPath, arrayOfFiles) {
let files = await fs.promises.readdir(dirPath)

arrayOfFiles = arrayOfFiles || []

files.forEach(async function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = await getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, file))
}
})

return arrayOfFiles
}

const convertBytes = function (bytes) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"]

Expand Down Expand Up @@ -195,15 +209,31 @@ const getDirItems = async function (dirPath, mode, config) {
};

const getTotalSize = async function (directoryPath, stringOutput = true) {
const fastFolderSizeAsync = promisify(fastFolderSize)
const bytes = await fastFolderSizeAsync(directoryPath)
const getSize = async (filePath) => {
const stats = await fs.promises.stat(filePath);

if (stringOutput) {
return convertBytes(bytes);
} else {
return bytes;
if (stats.isDirectory()) {
const files = await fs.promises.readdir(filePath);
const sizes = await Promise.all(files.map(file => getSize(path.join(filePath, file))));
return sizes.reduce((acc, size) => acc + size, 0);
} else {
return stats.size;
}
};

try {
const totalSize = await getSize(directoryPath);

if (stringOutput) {
return convertBytes(totalSize);
} else {
return totalSize;
}
} catch (err) {
console.error('Error calculating size:', err);
throw err;
}
}
};

const outputFolderSize = async (config) => {
try {
Expand Down

0 comments on commit 1682dc3

Please sign in to comment.