Skip to content

Instantly share code, notes, and snippets.

@frankinedinburgh
Last active July 26, 2018 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankinedinburgh/2e2e137a0ec3d89d70c4ae7cd29ff3d9 to your computer and use it in GitHub Desktop.
Save frankinedinburgh/2e2e137a0ec3d89d70c4ae7cd29ff3d9 to your computer and use it in GitHub Desktop.
get the size of a directory in bytes using node
// EXPORT THE SIZE OF A DIRECTORY USING NODE
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const argv = yargs.argv;
const dist = argv.dir;
// du -ach ./dist // unix command
const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat({
name: file,
path: path.join(dir, file),
size: fs.statSync(path.join(dir, file)).size
});
});
return filelist;
};
let build = walkSync(dist);
build = build.map(value => value.size);
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const message = `Total size => ${build.reduce(reducer)} bytes`;
module.exports = message;
@frankinedinburgh
Copy link
Author

frankinedinburgh commented Jul 26, 2018

place this command in your package.json for convenience

"scripts": {
"foo": "node size.js --dir="./dist"",
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment