grunt-image-info
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "grunt-image-info", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Grunt task to generate CSS, SASS, or JSON files with image dimension info", | ||
@@ -5,0 +5,0 @@ "main": "tasks/grunt-image-info.js", |
@@ -60,5 +60,41 @@ var json2css = require('json2css'), | ||
that = this; | ||
var imageSizeCache = {}; | ||
var newImageSizeCache = {}; | ||
var cacheFile = options.cacheFile; | ||
if (cacheFile) { | ||
try { | ||
imageSizeCache = JSON.parse(fs.readFileSync(cacheFile)); | ||
} catch (e) { | ||
} | ||
} | ||
var gmInstance = options.imageMagick ? gm.subClass({ imageMagick: true }) : gm; | ||
/** | ||
* Retrieves size data from a file from either graphicsmagick or the cache | ||
* @param {String} filename | ||
* @param {Function} callback | ||
*/ | ||
var getSizeData = function (filename, callback) { | ||
var relFilename = path.relative(__dirname, filename); | ||
var cachedMTime = String(imageSizeCache[relFilename] && imageSizeCache[relFilename].mtime); | ||
var newMTime = String(cacheFile ? fs.statSync(filename).mtime : ""); | ||
if (cachedMTime === newMTime) { | ||
newImageSizeCache[relFilename] = imageSizeCache[relFilename]; | ||
callback(null, imageSizeCache[relFilename].data); | ||
return; | ||
} | ||
gmInstance(filename).size(function (err, data) { | ||
if (err) return callback(err); | ||
newImageSizeCache[relFilename] = { | ||
mtime: newMTime, | ||
data: data | ||
}; | ||
callback(err, data); | ||
}); | ||
}; | ||
// Verify all properties are here | ||
@@ -72,3 +108,2 @@ if (this.files.length === 0) { | ||
var processFile = function(file, callback) { | ||
@@ -83,3 +118,3 @@ if (!file.dest || file.src.length === 0) { | ||
// obtain the size of an image | ||
gmInstance(src).size(function(err, size) { | ||
getSizeData(src, function(err, size) { | ||
if (err) { | ||
@@ -107,3 +142,3 @@ callback(err); | ||
}; | ||
// Hitting spawn EMFILE without this. | ||
@@ -152,2 +187,10 @@ var maxGmConcurrency = 10; | ||
if (cacheFile) { | ||
try { | ||
fs.writeFileSync(cacheFile, JSON.stringify(newImageSizeCache)); | ||
} catch (e) { | ||
grunt.log.warn('Unable to write cache file: ' + cacheFile); | ||
} | ||
} | ||
done(true); | ||
@@ -159,2 +202,2 @@ }); | ||
grunt.registerMultiTask('image_info', 'Generate image info', imageInfoTask); | ||
}; | ||
}; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
11076
200
1