build: add check for lib size

This commit is contained in:
Steven
2018-06-05 20:30:07 -04:00
parent 3822e3e4ed
commit bceb08b36f
2 changed files with 41 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
// @ts-check
const { lstatSync, readdirSync } = require("fs");
const { join } = require("path");
const { promisify } = require("util");
const execFile = promisify(require("child_process").execFile);
/**
* Find the size of a directory recursively.
* Symbolic links are counted once (same inode).
* @param {string} root
* @param {Set} seen
* @returns {number} bytes
*/
function getDirSize(root, seen = new Set()) {
const stats = lstatSync(root);
if (seen.has(stats.ino)) {
return 0;
}
seen.add(stats.ino);
if (!stats.isDirectory()) {
return stats.size;
}
return readdirSync(root)
.map(file => getDirSize(join(root, file), seen))
.reduce((acc, num) => acc + num, 0);
}
exports.getDirSize = getDirSize;