Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Use pretty-bytes instead of rolling our own function. Closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Jan 20, 2015
1 parent 4704783 commit f201914
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"dependencies": {
"glob": "^4.3.2",
"lodash": "^2.4.1"
"lodash": "^2.4.1",
"pretty-bytes": "^1.0.2"
},
"repository": "jeffposnick/sw-precache",
"bugs": "https://github.com/jeffposnick/sw-precache/issues",
Expand Down
21 changes: 5 additions & 16 deletions sw-precache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var crypto = require('crypto');
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var prettyBytes = require('pretty-bytes');
var util = require('util');
var _ = require('lodash');

Expand Down Expand Up @@ -40,18 +41,6 @@ function getHash(data) {
return md5.digest('hex');
}

function formatBytesAsString(bytes) {
if (bytes < 1024) {
return bytes + ' B';
}

if (bytes < (1024 * 1024)) {
return Math.round(bytes / 1024) + ' KB';
}

return Math.round(bytes / (1024 * 1024)) + ' MB';
}

module.exports = function(params, callback) {
_.defaults(params, {
dynamicUrlToDependencies: {},
Expand Down Expand Up @@ -81,12 +70,12 @@ module.exports = function(params, callback) {
relativeUrlToHash[relativeUrl] = fileAndSizeAndHash.hash;

params.logger(util.format("Caching static resource '%s' (%s)", fileAndSizeAndHash.file,
formatBytesAsString(fileAndSizeAndHash.size)));
prettyBytes(fileAndSizeAndHash.size)));
cumulativeSize += fileAndSizeAndHash.size;
} else {
params.logger(util.format("Skipping static resource '%s' (%s) - max size is %s",
fileAndSizeAndHash.file, formatBytesAsString(fileAndSizeAndHash.size),
formatBytesAsString(params.maximumFileSizeToCacheInBytes)));
fileAndSizeAndHash.file, prettyBytes(fileAndSizeAndHash.size),
prettyBytes(params.maximumFileSizeToCacheInBytes)));
}
});
});
Expand Down Expand Up @@ -122,7 +111,7 @@ module.exports = function(params, callback) {
});

params.logger(util.format('Total precache size is about %s for %d resources.',
formatBytesAsString(cumulativeSize), relativeUrls.length));
prettyBytes(cumulativeSize), relativeUrls.length));

fs.readFile(params.templateFilePath, 'utf8', function(error, data) {
if (error) {
Expand Down

0 comments on commit f201914

Please sign in to comment.