TypeScript/scripts/build/getDirSize.js
Wesley Wigham 657d0119cc
Dont use unreliable inodes for checking file identity (#25008)
* Dont use unreliable inode as unique identifier

* Just concat with `\n

* Introduce path-overriding code to allow local executables ot be found
2018-06-15 15:53:03 -07:00

24 lines
548 B
TypeScript

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