Skip to content

Instantly share code, notes, and snippets.

@lnfnunes
Last active August 29, 2015 14:10
Show Gist options
  • Save lnfnunes/c84a67e782520f26c75d to your computer and use it in GitHub Desktop.
Save lnfnunes/c84a67e782520f26c75d to your computer and use it in GitHub Desktop.
Grunt task to find unused images (cleanup image folder)
// 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