1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-05-03 12:57:08 +01:00

Improve proc spawn behavior.

Interim until async / promises support is in.
This commit is contained in:
hacksalot
2016-02-01 09:25:22 -05:00
parent 36d641801b
commit 212b01092c
8 changed files with 75 additions and 95 deletions

View File

@ -6,20 +6,20 @@ Definition of the HtmlPdfCLIGenerator class.
*/
(function() {
var FS, HTML, HtmlPdfCLIGenerator, PATH, SLASH, SPAWN, TemplateGenerator, engines;
var FS, HMSTATUS, HtmlPdfCLIGenerator, PATH, SLASH, TemplateGenerator, _, engines;
TemplateGenerator = require('./template-generator');
FS = require('fs-extra');
HTML = require('html');
PATH = require('path');
SPAWN = require('../utils/safe-spawn');
SLASH = require('slash');
_ = require('underscore');
HMSTATUS = require('../core/status-codes');
/**
An HTML-driven PDF resume generator for HackMyResume. Talks to Phantom,
@ -34,30 +34,20 @@ Definition of the HtmlPdfCLIGenerator class.
/** Generate the binary PDF. */
onBeforeSave: function(info) {
var ex, safe_eng;
try {
safe_eng = info.opts.pdf || 'wkhtmltopdf';
if (safe_eng !== 'none') {
engines[safe_eng].call(this, info.mk, info.outputFile);
return null;
}
} catch (_error) {
ex = _error;
if (ex.inner && ex.inner.code === 'ENOENT') {
throw {
fluenterror: this.codes.notOnPath,
inner: ex.inner,
engine: ex.cmd,
stack: ex.inner && ex.inner.stack
};
} else {
throw {
fluenterror: this.codes.pdfGeneration,
inner: ex,
stack: ex.stack
};
}
var safe_eng;
safe_eng = info.opts.pdf || 'wkhtmltopdf';
if (safe_eng === 'phantom') {
safe_eng = 'phantomjs';
}
if (_.has(engines, safe_eng)) {
this.SPAWN = require('../utils/safe-spawn');
this.errHandler = info.opts.errHandler;
engines[safe_eng].call(this, info.mk, info.outputFile, this.onError);
return null;
}
},
onError: function(ex, param) {
param.errHandler.err(HMSTATUS.pdfGeneration, ex);
}
});
@ -70,11 +60,11 @@ Definition of the HtmlPdfCLIGenerator class.
TODO: If HTML generation has run, reuse that output
TODO: Local web server to ease wkhtmltopdf rendering
*/
wkhtmltopdf: function(markup, fOut) {
var info, tempFile;
wkhtmltopdf: function(markup, fOut, on_error) {
var tempFile;
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
FS.writeFileSync(tempFile, markup, 'utf8');
return info = SPAWN('wkhtmltopdf', [tempFile, fOut]);
return this.SPAWN('wkhtmltopdf', [tempFile, fOut], false, on_error, this);
},
/**
@ -84,14 +74,14 @@ Definition of the HtmlPdfCLIGenerator class.
TODO: If HTML generation has run, reuse that output
TODO: Local web server to ease Phantom rendering
*/
phantom: function(markup, fOut) {
var destPath, info, scriptPath, sourcePath, tempFile;
phantomjs: function(markup, fOut, on_error) {
var destPath, scriptPath, sourcePath, tempFile;
tempFile = fOut.replace(/\.pdf$/i, '.pdf.html');
FS.writeFileSync(tempFile, markup, 'utf8');
scriptPath = SLASH(PATH.relative(process.cwd(), PATH.resolve(__dirname, '../utils/rasterize.js')));
sourcePath = SLASH(PATH.relative(process.cwd(), tempFile));
destPath = SLASH(PATH.relative(process.cwd(), fOut));
return info = SPAWN('phantomjs', [scriptPath, sourcePath, destPath]);
return this.SPAWN('phantomjs', [scriptPath, sourcePath, destPath], false, on_error, this);
}
};