2018-06-06 02:30:07 +02:00
|
|
|
// @ts-check
|
|
|
|
const { lstatSync, readdirSync } = require("fs");
|
|
|
|
const { join } = require("path");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the size of a directory recursively.
|
2018-06-16 00:53:03 +02:00
|
|
|
* Symbolic links can cause a loop.
|
2018-06-06 02:30:07 +02:00
|
|
|
* @param {string} root
|
|
|
|
* @returns {number} bytes
|
|
|
|
*/
|
2018-06-16 00:53:03 +02:00
|
|
|
function getDirSize(root) {
|
2018-06-06 02:30:07 +02:00
|
|
|
const stats = lstatSync(root);
|
|
|
|
|
|
|
|
if (!stats.isDirectory()) {
|
|
|
|
return stats.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return readdirSync(root)
|
2018-06-16 00:53:03 +02:00
|
|
|
.map(file => getDirSize(join(root, file)))
|
2018-06-06 02:30:07 +02:00
|
|
|
.reduce((acc, num) => acc + num, 0);
|
|
|
|
}
|
|
|
|
|
2018-06-08 02:52:22 +02:00
|
|
|
module.exports = getDirSize;
|