Skip to content

Instantly share code, notes, and snippets.

@ded
Created March 5, 2011 10:16
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ded/856277 to your computer and use it in GitHub Desktop.
Save ded/856277 to your computer and use it in GitHub Desktop.
a simple node script to determine the size of any directory on a file system
/**
* usage
* DIR='public/js' node directory-size.js
* => "size of public/js is: 12,432
*/
var fs = require('fs'),
_ = require('./underscore'); // requires underscore for _.flatten()
function format(n) {
return n.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
}
var DIR = process.env.DIR || './';
function getFiles(dir) {
dir = dir.replace(/\/$/, '');
return _.flatten(fs.readdirSync(dir).map(function(file) {
var fileOrDir = fs.statSync([dir, file].join('/'));
if (fileOrDir.isFile()) {
return (dir + '/' + file).replace(/^\.\/\/?/, '');
} else if (fileOrDir.isDirectory()) {
return getFiles([dir, file].join('/'));
}
}));
}
var allFiles = getFiles(DIR).map(function(file) {
return fs.readFileSync(file);
}).join('\n');
console.log('size of ' + DIR + ' is: ' + format(allFiles.length));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment