Created
December 18, 2013 03:38
-
-
Save ericf/8016919 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
var fs = require('fs'), | |
path = require('path'), | |
Promise = require('es6-promise').Promise, | |
zlib = require('zlib'); | |
module.exports = filesizes; | |
// ----------------------------------------------------------------------------- | |
function filesizes(dirPath, callback) { | |
getModules(dirPath).then(function (modules) { | |
return Promise.all(modules.map(function (module) { | |
var filePath = path.join(dirPath, module + '-min.css'); | |
return readModule(filePath).then(getSize); | |
})).then(function (sizes) { | |
return sizes.reduce(function (map, size, i) { | |
map[modules[i]] = size; | |
return map; | |
}, {}); | |
}); | |
}).then(function (moduleSizes) { | |
callback(null, moduleSizes); | |
}, function (err) { | |
callback(err); | |
}); | |
} | |
function getModules(dirPath) { | |
return new Promise(function (resolve, reject) { | |
fs.readdir(dirPath, function (err, files) { | |
if (err) { return reject(err); } | |
resolve(files.filter(function (file) { | |
return (/\-min\.css$/).test(file); | |
}).map(function (file) { | |
return file.replace(/\-min\.css$/, ''); | |
})); | |
}); | |
}); | |
} | |
function readModule(filePath) { | |
return new Promise(function (resolve, reject) { | |
fs.readFile(filePath, function (err, file) { | |
if (err) { return reject(err); } | |
resolve(file); | |
}); | |
}); | |
} | |
function getSize(file) { | |
return new Promise(function (resolve, reject) { | |
zlib.gzip(file, function (err, compressed) { | |
if (err) { return reject(err); } | |
resolve(compressed.length); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment