Created
March 5, 2011 10:16
-
-
Save ded/856277 to your computer and use it in GitHub Desktop.
Revisions
-
ded created this gist
Mar 5, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ /** * 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));