From 07b23109f9a6617d6700538df1b05b57a942f3ab Mon Sep 17 00:00:00 2001 From: hacksalot Date: Tue, 12 Jan 2016 12:32:32 -0500 Subject: [PATCH] Use async spawn() by default. --- src/generators/html-pdf-cli-generator.js | 38 +++----------------- src/utils/safe-spawn.js | 45 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 src/utils/safe-spawn.js diff --git a/src/generators/html-pdf-cli-generator.js b/src/generators/html-pdf-cli-generator.js index 8a80e3f..35018af 100644 --- a/src/generators/html-pdf-cli-generator.js +++ b/src/generators/html-pdf-cli-generator.js @@ -14,6 +14,7 @@ Definition of the HtmlPdfCLIGenerator class. , FS = require('fs-extra') , HTML = require( 'html' ) , PATH = require('path') + , SPAWN = require('../utils/safe-spawn') , SLASH = require('slash'); @@ -77,24 +78,8 @@ Definition of the HtmlPdfCLIGenerator class. // Save the markup to a temporary file var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html'); FS.writeFileSync( tempFile, markup, 'utf8' ); + var info = SPAWN( 'wkhtmltopdf', [ tempFile, fOut ] ); - var spawn = require('child_process').spawnSync; - var info = spawn('wkhtmltopdf', [ - tempFile, fOut - ]); - if( info.error ) { - throw { - cmd: 'wkhtmltopdf', - inner: info.error - }; - } - - // child.stdout.on('data', function(chunk) { - // // output will be here in chunks - // }); - - // or if you want to send output elsewhere - //child.stdout.pipe(dest); }, @@ -111,29 +96,16 @@ Definition of the HtmlPdfCLIGenerator class. // Save the markup to a temporary file var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html'); FS.writeFileSync( tempFile, markup, 'utf8' ); - var scriptPath = SLASH( PATH.relative( process.cwd(), PATH.resolve( __dirname, '../utils/rasterize.js' ) ) ); var sourcePath = SLASH( PATH.relative( process.cwd(), tempFile) ); var destPath = SLASH( PATH.relative( process.cwd(), fOut) ); + var info = SPAWN('phantomjs', [ scriptPath, sourcePath, destPath ]); - var spawn = require('child_process').spawnSync; - var info = spawn('phantomjs', [ scriptPath, sourcePath, destPath ]); - if( info.error ) { - throw { - cmd: 'phantomjs', - inner: info.error - }; - } - - // child.stdout.on('data', function(chunk) { - // // output will be here in chunks - // }); - // - // // or if you want to send output elsewhere - // child.stdout.pipe(dest); } + + }; diff --git a/src/utils/safe-spawn.js b/src/utils/safe-spawn.js new file mode 100644 index 0000000..3cf25dc --- /dev/null +++ b/src/utils/safe-spawn.js @@ -0,0 +1,45 @@ +/** +Safe spawn utility for HackMyResume / FluentCV. +@module safe-spawn.js +@license MIT. See LICENSE.md for details. +*/ + + + +(function() { + + + + module.exports = function( cmd, args, isSync ) { + + try { + + var spawn = require('child_process')[ isSync? 'spawnSync' : 'spawn']; + var info = spawn( cmd, args ); + + if( !isSync ) { + info.on('error', function(err) { + throw { + cmd: 'wkhtmltopdf', + inner: err + }; + }); + } + else { + if( info.error ) { + throw { + cmd: 'wkhtmltopdf', + inner: info.error + }; + } + } + + } + catch( ex ) { + + } + }; + + + +}());