1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-25 01:40:10 +00:00

Use async spawn() by default.

This commit is contained in:
hacksalot 2016-01-12 12:32:32 -05:00
parent 32769a2b0b
commit 07b23109f9
2 changed files with 50 additions and 33 deletions

View File

@ -14,6 +14,7 @@ Definition of the HtmlPdfCLIGenerator class.
, FS = require('fs-extra') , FS = require('fs-extra')
, HTML = require( 'html' ) , HTML = require( 'html' )
, PATH = require('path') , PATH = require('path')
, SPAWN = require('../utils/safe-spawn')
, SLASH = require('slash'); , SLASH = require('slash');
@ -77,24 +78,8 @@ Definition of the HtmlPdfCLIGenerator class.
// Save the markup to a temporary file // Save the markup to a temporary file
var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html'); var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
FS.writeFileSync( tempFile, markup, 'utf8' ); 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,28 +96,15 @@ Definition of the HtmlPdfCLIGenerator class.
// Save the markup to a temporary file // Save the markup to a temporary file
var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html'); var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
FS.writeFileSync( tempFile, markup, 'utf8' ); FS.writeFileSync( tempFile, markup, 'utf8' );
var scriptPath = SLASH( PATH.relative( process.cwd(), var scriptPath = SLASH( PATH.relative( process.cwd(),
PATH.resolve( __dirname, '../utils/rasterize.js' ) ) ); PATH.resolve( __dirname, '../utils/rasterize.js' ) ) );
var sourcePath = SLASH( PATH.relative( process.cwd(), tempFile) ); var sourcePath = SLASH( PATH.relative( process.cwd(), tempFile) );
var destPath = SLASH( PATH.relative( process.cwd(), fOut) ); 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);
}
}; };

45
src/utils/safe-spawn.js Normal file
View File

@ -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 ) {
}
};
}());