From 38007b19ad8812c5bfdb80f775e1da6e35a82c17 Mon Sep 17 00:00:00 2001 From: Andy Dayton Date: Sat, 24 Jan 2015 21:43:26 -0500 Subject: [PATCH 1/2] Try adding grunt async and creating a simple index check --- tasks/wkhtmltopdf.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tasks/wkhtmltopdf.js b/tasks/wkhtmltopdf.js index 6f6dc05..9b37a80 100644 --- a/tasks/wkhtmltopdf.js +++ b/tasks/wkhtmltopdf.js @@ -18,7 +18,9 @@ module.exports = function(grunt) { grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() { - this.files.forEach(function(file) { + var done = this.async(); + + this.files.forEach(function(file, filesIndex) { var pathlib = require('path'); // calculate the destination directory and ensure it exists, since @@ -30,7 +32,7 @@ module.exports = function(grunt) { } grunt.file.mkdir(destPath); - file.src.forEach(function(src) { + file.src.forEach(function(src, srcIndex) { var dest = file.dest; // wkhtmltopdf seems to require that the destination be a file @@ -64,6 +66,10 @@ module.exports = function(grunt) { if (err) { grunt.log('>>>', err); } + // if this is the last src of the last file, we are done. + if((filesIndex+1 >= files.length) && (srcIndex+1 >= src.length)) { + done(); + } } }); }); From 696955b345693879fa0b3baa6c8f144586cbc3d6 Mon Sep 17 00:00:00 2001 From: Andy Dayton Date: Sat, 24 Jan 2015 21:57:51 -0500 Subject: [PATCH 2/2] Added some comments --- tasks/wkhtmltopdf.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tasks/wkhtmltopdf.js b/tasks/wkhtmltopdf.js index 9b37a80..5000ec3 100644 --- a/tasks/wkhtmltopdf.js +++ b/tasks/wkhtmltopdf.js @@ -18,9 +18,13 @@ module.exports = function(grunt) { grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() { - var done = this.async(); + // using grunt async support so we can be sure task doesn't + // end before PDF processing does: http://gruntjs.com/api/inside-tasks + var done = this.async(), + files = this.files; - this.files.forEach(function(file, filesIndex) { + // iterate through each file grouping + files.forEach(function(file, filesIndex) { var pathlib = require('path'); // calculate the destination directory and ensure it exists, since @@ -67,7 +71,7 @@ module.exports = function(grunt) { grunt.log('>>>', err); } // if this is the last src of the last file, we are done. - if((filesIndex+1 >= files.length) && (srcIndex+1 >= src.length)) { + if((filesIndex+1 >= files.length) && (srcIndex+1 >= file.src.length)) { done(); } }