Fixed compatibility with grunt 0.4

This commit is contained in:
Olivier Audard 2012-10-14 02:13:02 +02:00
parent ac15297331
commit f02cffd8a5
3 changed files with 56 additions and 40 deletions

View file

@ -36,6 +36,9 @@
"grunt": "~0.3.9"
},
"keywords": [
"gruntplugin"
"gruntplugin",
"pdf",
"convert",
"html"
]
}

View file

@ -0,0 +1,49 @@
/*
* grunt-wkhtmltopdf
* http://www.dhar.fr/grunt-wkhtmltopdf/
*
* Copyright (c) 2012 Olivier Audard, contributors
* Licensed under the MIT license.
*/
/*globals exports:false*/
exports.init = function(grunt) {
'use strict';
return {
convert: function(options) {
if (!options || !options.args) {
grunt.warn("You need to specify atleast one input file, and exactly one output file");
return null;
}
// TODO: ditch this when grunt v0.4 is released
grunt.util = grunt.util || grunt.utils;
return grunt.util.spawn({
cmd: 'wkhtmltopdf',
args: options.args
}, function(err, result, code) {
grunt.log.writeln('wkhtmltopdf done');
if (!err) { return options.done(null); }
// Something went horribly wrong.
grunt.verbose.or.writeln();
grunt.log.write('Running wkhtmltopdf...').error();
if (code === 127) {
grunt.log.errorlns(
'In order for this task to work properly, wkhtmltopdf must be ' +
'installed and in the system PATH (if you can run "wkhtmltopdf" at' +
' the command line, this task should work). Unfortunately, ' +
'wkhtmltopdf cannot be installed automatically via npm or grunt. '
);
grunt.warn('wkhtmltopdf not found.', options.code);
} else {
result.split('\n').forEach(grunt.log.error, grunt.log);
grunt.warn('wkhtmltopdf exited unexpectedly with exit code ' + code + '.', options.code);
}
options.done(code);
});
}
};
};

View file

@ -5,7 +5,7 @@
* Copyright (c) 2012 Olivier Audard
* Licensed under the MIT license.
*/
/*globals module:false*/
/*globals module:false, require:false*/
module.exports = function(grunt) {
// Please see the grunt documentation for more information regarding task and
@ -14,6 +14,7 @@ module.exports = function(grunt) {
// ==========================================================================
// TASKS
// ==========================================================================
var helper = require('./lib/wkhtmltopdf-lib').init(grunt);
grunt.registerTask('wkhtmltopdf', 'Your task description goes here.', function() {
grunt.config.requires('wkhtmltopdf.src');
@ -35,7 +36,7 @@ module.exports = function(grunt) {
srcpath.replace(/.*\/([^\/]+)\.html/, '$1.pdf');
// Launch PhantomJS.
grunt.helper('wkhtmltopdf', {
helper.convert({
code: 90,
args: [
'--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72)
@ -51,41 +52,4 @@ module.exports = function(grunt) {
});
});
});
// ==========================================================================
// HELPERS
// ==========================================================================
grunt.registerHelper('wkhtmltopdf', function(options) {
if (!options || !options.args) {
grunt.warn("You need to specify atleast one input file, and exactly one output file");
return null;
}
return grunt.utils.spawn({
cmd: 'wkhtmltopdf',
args: options.args
}, function(err, result, code) {
grunt.log.writeln('wkhtmltopdf done');
if (!err) { return options.done(null); }
// Something went horribly wrong.
grunt.verbose.or.writeln();
grunt.log.write('Running wkhtmltopdf...').error();
if (code === 127) {
grunt.log.errorlns(
'In order for this task to work properly, wkhtmltopdf must be ' +
'installed and in the system PATH (if you can run "wkhtmltopdf" at' +
' the command line, this task should work). Unfortunately, ' +
'wkhtmltopdf cannot be installed automatically via npm or grunt. '
);
grunt.warn('wkhtmltopdf not found.', options.code);
} else {
result.split('\n').forEach(grunt.log.error, grunt.log);
grunt.warn('wkhtmltopdf exited unexpectedly with exit code ' + code + '.', options.code);
}
options.done(code);
});
});
};