Last active
July 26, 2018 15:03
-
-
Save frankinedinburgh/2e2e137a0ec3d89d70c4ae7cd29ff3d9 to your computer and use it in GitHub Desktop.
get the size of a directory in bytes using node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
place this command in your package.json for convenience
"scripts": {
"foo": "node size.js --dir="./dist"",
}