Skip to content

Commit

Permalink
fixing async again
Browse files Browse the repository at this point in the history
  • Loading branch information
jney committed Sep 23, 2012
1 parent 487a478 commit 20a815e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Options can be whatever `htmlcompressor accepts`
[htmlcompressor]: http://code.google.com/p/htmlcompressor/

## Release History
* 0.1.4 Async fixing
* 0.1.0 Dynamic input (`src` parameter can be a function) and output (`processName`) files
* 0.0.1 First Release

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "grunt-htmlcompressor",
"description": "compress html with grunt and htmlcompressor",
"version": "0.1.3",
"version": "0.1.4",
"homepage": "https://github.com/jney/grunt-htmlcompressor",
"author": {
"name": "Jean-Sébastien Ney",
"url": "http://github.com/jney/htmlcompressor"
"url": "http://github.com/jney/"
},
"repository": {
"type": "git",
Expand Down
50 changes: 27 additions & 23 deletions tasks/grunt-htmlcompressor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,43 @@ module.exports = function(grunt) {

delete options.processName;

async.forEachSeries(files, function(file) {
async.forEach(files, function(file, next) {
var src = _.isFunction(file.src) ? file.src() : file.src;
var srcFiles = grunt.file.expandFiles(src);

srcFiles.forEach(function(srcFile) {
grunt.helper('htmlcompressor', srcFile, options, function(html) {
var dest = _.isFunction(processName) ?
processName(srcFile, html) : file.dest;
grunt.file.write(dest, html);
grunt.log.writeln('File "' + dest + '" created.');
}, done);
});
async.forEach(srcFiles, function(srcFile, nextF) {

grunt.helper('htmlcompressor', srcFile, options, function(err, html) {
if (err) {
nextF(err);
} else {
var dest = _.isFunction(processName) ?
processName(srcFile, html) : file.dest;
grunt.file.write(dest, html);
grunt.log.writeln('File "' + dest + '" created.');
nextF();
}
});
}, next);
}, done);

});

grunt.registerHelper('htmlcompressor', function(inputFile, opts, callback, done) {
done = done || noop;
grunt.registerHelper('htmlcompressor', function(srcFile, options, callback) {
var jar = __dirname + '/../ext/htmlcompressor-1.5.3.jar';
var args = _.flatten(['-jar', jar, _.map(opts, toParameter), inputFile]);
var args = _.flatten(['-jar', jar, _.map(options, toParameter), srcFile]);

grunt.util.spawn({
cmd: 'java',
args: args
}, function(err, output, code) {
if (err) {
grunt.log.error(err);
grunt.fail.warn('htmlcompressor failed to compress html.');
done(false);
} else {
callback(output.stdout);
}
});
if (err) {
grunt.log.error(err);
grunt.fail.warn('htmlcompressor failed to compress html.');
callback(err);
} else {
callback(null, output.stdout);
}
});
});

// Convert a pair of key/value to an array
Expand All @@ -76,8 +80,8 @@ module.exports = function(grunt) {
// // => ['--preserve-comments']
function toParameter(val, key) {
var str = '--' + key.replace(/([A-Z])/g, function(a) {
return '-' + a.toLowerCase();
});
return '-' + a.toLowerCase();
});

return (val === true) ? [str] : [str, val];
}
Expand Down
6 changes: 4 additions & 2 deletions test/grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function(grunt) {
grunt.initConfig({
pkg: {
name: 'grunt-htmlcompressor',
version: '0.1.3'
version: '0.1.4'
},

files: {
Expand All @@ -23,7 +23,9 @@ module.exports = function(grunt) {

htmlcompressor: {
compress: {
src: 'fixtures/html/*.html',
src: [
'fixtures/html/*.html'
],
options: {
type: 'html',
processName: function (path) {
Expand Down

0 comments on commit 20a815e

Please sign in to comment.