Created
August 6, 2012 12:04
-
-
Save sapegin/3273957 to your computer and use it in GitHub Desktop.
imgo task for Grunt
This file contains hidden or 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
| /** | |
| * imgo task for Grunt | |
| * | |
| * @author Artem Sapegin (http://sapegin.me) | |
| */ | |
| /*jshint node:true */ | |
| module.exports = function(grunt) { | |
| 'use strict'; | |
| var async = grunt.utils.async; | |
| grunt.registerMultiTask('imgo', 'Optimize images using imgo', function(target) { | |
| this.requiresConfig('imgo'); | |
| // Build an array of files/tasks objects | |
| var imgo = grunt.config('imgo'), | |
| targets = target ? [target] : Object.keys(imgo).filter(function(key) { | |
| return typeof imgo[key] !== 'string' && !Array.isArray(imgo[key]); | |
| }); | |
| targets = targets.map(function(target) { | |
| // Fail if any required config properties have been omitted | |
| target = ['imgo', target]; | |
| this.requiresConfig(target.concat('files')); | |
| return grunt.config(target); | |
| }, this); | |
| // Allow "basic" non-target format | |
| if (typeof imgo.files === 'string' || Array.isArray(imgo.files)) { | |
| targets.push({files: imgo.files, options: imgo.options}); | |
| } | |
| // Work | |
| var done = this.async(); | |
| async.forEachSeries(targets, function(target, nextTarget) { | |
| var files = grunt.file.expandFiles(target.files); | |
| async.forEach(files, function(file, nextFile) { | |
| var args = [file]; | |
| if (target.options) { | |
| args.unshift(target.options); | |
| } | |
| grunt.helper('imgo', { | |
| args: args, | |
| done: function(data) { | |
| if (!data.err && data.compressed) { | |
| grunt.log.writeln("File '" + file + "': saved " + data.saved + " bytes " + | |
| "(" + Math.round(data.saved/data.before*100) + "%)"); | |
| } | |
| nextFile(); | |
| } | |
| }); | |
| }, function() { | |
| nextTarget(); | |
| }); | |
| }, function() { | |
| done(); | |
| }); | |
| }); | |
| grunt.registerHelper('imgo', function(options) { | |
| return grunt.utils.spawn({ | |
| cmd: 'imgo', | |
| args: options.args | |
| }, function(err, result, code) { | |
| if (!err) { | |
| var fields = result.split('\n')[0].split(' '), | |
| before = parseInt(fields[1], 10), | |
| after = parseInt(fields[3], 10), | |
| saved = before - after; | |
| if (!isNaN(saved) && saved > 0) { | |
| return options.done({ | |
| compressed: true, | |
| before: before, | |
| after: before, | |
| saved: saved | |
| }) | |
| } | |
| else { | |
| return options.done({ compressed: false }); | |
| } | |
| }; | |
| // Something went horribly wrong | |
| grunt.verbose.or.writeln(); | |
| grunt.log.write('Running imgo...').error(); | |
| if (code === 127) { // @todo ??? | |
| grunt.log.errorlns( | |
| 'Please install imgo: https://github.com/imgo/imgo' | |
| ); | |
| grunt.warn('imgo not found', options.code); | |
| } | |
| else { | |
| result.split('\n').forEach(grunt.log.error, grunt.log); | |
| grunt.warn('imgo exited unexpectedly with exit code ' + code + '.', options.code); | |
| } | |
| options.done({ err: code }); | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment