Last active
August 29, 2015 14:10
-
-
Save lnfnunes/c84a67e782520f26c75d to your computer and use it in GitHub Desktop.
Grunt task to find unused images (cleanup image folder)
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
// Find unused images | |
grunt.registerTask('unusedimages', function() { | |
var arrImages = [], | |
arrImageReference = [], | |
destDir = '../htdocs/'; | |
// Populate image list on folder | |
grunt.file.expand({ | |
filter: 'isFile', | |
cwd: destDir //+ 'img/' // Set to search only on images folder for performance improvement | |
}, ['**/*.{png,jpg,jpeg,gif,ico}']).forEach(function(file) { arrImages.push(file); }); | |
// Find images in source (html,js,css) | |
grunt.file.expand({ | |
filter: 'isFile', | |
}, [destDir + '**/*.{js,css,html}']).forEach(function(file) { | |
var buffer = grunt.file.read(file); | |
arrImages.forEach(function(asset) { | |
if(buffer.search(asset) !== -1) { | |
arrImageReference.push(asset); | |
} | |
}); | |
}); | |
// Output unused images | |
var unused = grunt.util._.difference(arrImages, arrImageReference); | |
console.log('Found '+ unused.length +' unused images:'); | |
unused.forEach(function(el) { console.log(el); }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment