Permalink
Browse files

fallback to local binaries for windows system

* 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 a5566f03b90df3ce7c2c79c12a9abcea6f16c956
View
@@ -107,7 +107,6 @@ module.exports = function(grunt) {
grunt.registerHelper('min_max_stat', function(min, max) {
min = typeof min === 'string' ? fs.statSync(min) : min;
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('Compressed size: ' + String(min.size).green + ' bytes minified.');
});
View
@@ -20,6 +20,7 @@ var fs = require('fs'),
// }
//
+var win32 = process.platform === 'win32';
module.exports = function(grunt) {
@@ -60,70 +61,54 @@ module.exports = function(grunt) {
opts = opts || {};
cb = cb || function() {};
- var args = opts.args ? opts.args : [];
- args = args.concat(files);
- if(!files.length) return cb();
- grunt.log.writeln('Running optipng... ' + grunt.log.wordlist(files));
- var child = grunt.utils.spawn({
- cmd: '/usr/local/bin/optipng',
- args: args
- }, function() {});
-
- var error = function error(code) {
- if(!code) return cb();
- grunt.verbose.or.writeln();
- grunt.log.write('Running optipng...').error();
- if (code === 127) {
- grunt.log.errorlns(
- 'In order for this task to work properly, optipng must be ' +
- '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.helper('which', 'optipng', function(err, cmdpath) {
+ if(err) return grunt.helper('not installed', 'optipng', cb);
+ var args = opts.args ? opts.args : [];
+ args = args.concat(files);
+ if(!files.length) return cb();
+ grunt.log.writeln('Running optipng... ' + grunt.log.wordlist(files));
+ var optipng = grunt.utils.spawn({
+ cmd: cmdpath,
+ args: args
+ }, function() {});
+
+ optipng.stdout.pipe(process.stdout);
+ optipng.stderr.pipe(process.stderr);
+ optipng.on('exit', function(code) {
+ if(code) grunt.warn('optipng exited unexpectedly with exit code ' + code + '.', code);
+ cb();
+ });
+ });
});
grunt.registerHelper('jpegtran', function(files, opts, cb) {
opts = opts || {};
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);
(function run(file) {
if(!file) return cb();
- console.log(opts.args);
- var child = grunt.utils.spawn({
+ grunt.log.subhead('** Processing: ' + file);
+ var jpegtran = grunt.utils.spawn({
cmd: cmdpath,
args: opts.args.concat(file)
}, function() {});
- var jpgtmp = fs.createWriteStream('jpgtmp.jpg');
- child.stdout.pipe(jpgtmp).on('close', function() {
+ jpegtran.stdout.pipe(process.stdout);
+ 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
grunt.helper('min_max_stat', 'jpgtmp.jpg', file);
-
// copy the temporary optimized jpg to original file
fs.createReadStream('jpgtmp.jpg')
.pipe(fs.createWriteStream(file)).on('close', function() {
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()));
});
@@ -137,9 +122,19 @@ module.exports = function(grunt) {
'installed and in the system PATH (if you can run ":cmd" at',
'the command line, this task should work)'
].join(' ').replace(/:cmd/g, cmd));
- grunt.log.writeln('Skiping ' + cmd + ' task');
+ grunt.log.subhead('Skiping ' + cmd + ' task');
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 not shown.
@@ -0,0 +1,2 @@
+[InternetShortcut]
+URL=http://optipng.sourceforge.net/
@@ -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�
Oops, something went wrong.

0 comments on commit a5566f0

Please sign in to comment.