Skip to content

Commit

Permalink
fallback to local binaries for windows system
Browse files Browse the repository at this point in the history
* added optipng-8d and optipng-0.7.1 into vendor/
* necessary change to img task code to work on windows
* to test back on posix

This closes #25
  • Loading branch information
mklabs committed Apr 14, 2012
1 parent f3d4bc2 commit a5566f0
Show file tree
Hide file tree
Showing 13 changed files with 2,071 additions and 45 deletions.
1 change: 0 additions & 1 deletion tasks/h5bp.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ module.exports = function(grunt) {
grunt.registerHelper('min_max_stat', function(min, max) { grunt.registerHelper('min_max_stat', function(min, max) {
min = typeof min === 'string' ? fs.statSync(min) : min; min = typeof min === 'string' ? fs.statSync(min) : min;
max = typeof max === 'string' ? fs.statSync(max) : max; max = typeof max === 'string' ? fs.statSync(max) : max;
console.log(max.size, min.size);
grunt.log.writeln('Uncompressed size: ' + String(max.size).green + ' bytes.'); grunt.log.writeln('Uncompressed size: ' + String(max.size).green + ' bytes.');
grunt.log.writeln('Compressed size: ' + String(min.size).green + ' bytes minified.'); grunt.log.writeln('Compressed size: ' + String(min.size).green + ' bytes minified.');
}); });
Expand Down
83 changes: 39 additions & 44 deletions tasks/img.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var fs = require('fs'),
// } // }
// //


var win32 = process.platform === 'win32';


module.exports = function(grunt) { module.exports = function(grunt) {


Expand Down Expand Up @@ -60,70 +61,54 @@ module.exports = function(grunt) {
opts = opts || {}; opts = opts || {};
cb = cb || function() {}; cb = cb || function() {};


var args = opts.args ? opts.args : []; grunt.helper('which', 'optipng', function(err, cmdpath) {
args = args.concat(files); if(err) return grunt.helper('not installed', 'optipng', cb);
if(!files.length) return cb(); var args = opts.args ? opts.args : [];
grunt.log.writeln('Running optipng... ' + grunt.log.wordlist(files)); args = args.concat(files);
var child = grunt.utils.spawn({ if(!files.length) return cb();
cmd: '/usr/local/bin/optipng', grunt.log.writeln('Running optipng... ' + grunt.log.wordlist(files));
args: args var optipng = grunt.utils.spawn({
}, function() {}); cmd: cmdpath,

args: args
var error = function error(code) { }, function() {});
if(!code) return cb();
grunt.verbose.or.writeln(); optipng.stdout.pipe(process.stdout);
grunt.log.write('Running optipng...').error(); optipng.stderr.pipe(process.stderr);
if (code === 127) { optipng.on('exit', function(code) {
grunt.log.errorlns( if(code) grunt.warn('optipng exited unexpectedly with exit code ' + code + '.', code);
'In order for this task to work properly, optipng must be ' + cb();
'installed and in the system PATH (if you can run "optipng" at' + });
' the command line, this task should work)' });
);
grunt.warn('optipng not found.', code);
} else {
grunt.warn('optipng exited unexpectedly with exit code ' + code + '.', code);
}

return cb(false);
};

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);

child.on('exit', error).on('error', error);

}); });


grunt.registerHelper('jpegtran', function(files, opts, cb) { grunt.registerHelper('jpegtran', function(files, opts, cb) {
opts = opts || {}; opts = opts || {};
cb = cb || function() {}; cb = cb || function() {};
opts.args = opts.args ? opts.args : ['-copy', 'none', '-optimize']; opts.args = opts.args ? opts.args : ['-copy', 'none', '-optimize', '-outfile', 'jpgtmp.jpg'];


which('jpegtran', function(err, cmdpath) { grunt.helper('which', 'jpegtran', function(err, cmdpath) {
if(err) return grunt.helper('not installed', 'jpegtran', cb); if(err) return grunt.helper('not installed', 'jpegtran', cb);
(function run(file) { (function run(file) {
if(!file) return cb(); if(!file) return cb();
console.log(opts.args); grunt.log.subhead('** Processing: ' + file);
var child = grunt.utils.spawn({ var jpegtran = grunt.utils.spawn({
cmd: cmdpath, cmd: cmdpath,
args: opts.args.concat(file) args: opts.args.concat(file)
}, function() {}); }, function() {});


var jpgtmp = fs.createWriteStream('jpgtmp.jpg'); jpegtran.stdout.pipe(process.stdout);
child.stdout.pipe(jpgtmp).on('close', function() { jpegtran.stderr.pipe(process.stderr);

jpegtran.on('exit', function(code) {
if(code) return grunt.warn('jpgtran exited unexpectedly with exit code ' + code + '.', code);
// output some size info about the file // output some size info about the file
grunt.helper('min_max_stat', 'jpgtmp.jpg', file); grunt.helper('min_max_stat', 'jpgtmp.jpg', file);

// copy the temporary optimized jpg to original file // copy the temporary optimized jpg to original file
fs.createReadStream('jpgtmp.jpg') fs.createReadStream('jpgtmp.jpg')
.pipe(fs.createWriteStream(file)).on('close', function() { .pipe(fs.createWriteStream(file)).on('close', function() {
run(files.shift()); run(files.shift());
}); });
}); });
child.stderr.pipe(process.stderr);
child.on('exit', function(code) {
if(code) grunt.warn('jpg exited unexpectedly with exit code ' + code + '.', code);
});
}(files.shift())); }(files.shift()));
}); });


Expand All @@ -137,9 +122,19 @@ module.exports = function(grunt) {
'installed and in the system PATH (if you can run ":cmd" at', 'installed and in the system PATH (if you can run ":cmd" at',
'the command line, this task should work)' 'the command line, this task should work)'
].join(' ').replace(/:cmd/g, cmd)); ].join(' ').replace(/:cmd/g, cmd));
grunt.log.writeln('Skiping ' + cmd + ' task'); grunt.log.subhead('Skiping ' + cmd + ' task');
if(cb) cb(); if(cb) cb();
}); });


// **which** helper, wrapper to isaacs/which package plus some fallback logic
// specifically for the win32 binaries in vendor/ (optipng.exe, jpegtran.exe)
grunt.registerHelper('which', function(cmd, cb) {
if(!win32 || !/optipng|jpegtran/.test(cmd)) return which(cmd, cb);

var cmdpath = cmd === 'optipng' ? '../vendor/optipng-0.7.1-win32/optipng.exe' :
'../vendor/jpegtran-8d/jpegtran.exe';

cb(null, path.join(__dirname, cmdpath));
});
}; };


Binary file added vendor/jpegtran-8d/jpegtran.exe
Binary file not shown.
2 changes: 2 additions & 0 deletions vendor/optipng-0.7.1-win32/OptiPNG.url
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
[InternetShortcut]
URL=http://optipng.sourceforge.net/
25 changes: 25 additions & 0 deletions vendor/optipng-0.7.1-win32/doc/authors.txt
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
# OptiPNG version 0.7.1
# Copyright (C) 2001-2012 Cosmin Truta and the Contributing Authors.
# See the accompanying LICENSE file for details.
#
# A Contributing Author is a person or company who contributed code that
# is now part of OptiPNG.
#
# For the purpose of copyright and licensing, this is the official list
# of Contributing Authors, in alphabetic order.

Adam Ciarcinski
Brian McQuade
Elias Pipping
Fabien Barbier
Maciej Pilichowski
Matthew Fearnley
Nelson A. de Oliveira
Niels de Koning
Petr Gajdos
Piotr Bandurski
Ramona C. Truta
Sebastian Pipping
Stefan Br�ns
Till Maas
Ville Skytt�
Loading

0 comments on commit a5566f0

Please sign in to comment.