mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2025-05-10 07:47:07 +01:00
Gather.
This commit is contained in:
@ -18,89 +18,90 @@ Error-handling routines for HackMyResume.
|
||||
, WRAP = require('word-wrap')
|
||||
, chalk = require('chalk')
|
||||
, SyntaxErrorEx = require('../utils/syntax-error-ex');
|
||||
require('string.prototype.startswith');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
An amorphous blob of error handling code for HackMyResume.
|
||||
Error handler for HackMyResume. All errors are handled here.
|
||||
@class ErrorHandler
|
||||
*/
|
||||
var ErrorHandler = module.exports = {
|
||||
|
||||
init: function( debug ) {
|
||||
this.debug = debug;
|
||||
return this;
|
||||
},
|
||||
|
||||
err: function( ex, shouldExit ) {
|
||||
|
||||
var msg = '', exitCode, log = console.log, showStack = ex.showStack;
|
||||
if( ex.fluenterror ) {
|
||||
|
||||
// If the exception has been handled elsewhere and shouldExit is true,
|
||||
// let's get out of here, otherwise silently return.
|
||||
if( ex.handled ) {
|
||||
if( shouldExit )
|
||||
process.exit( exitCode );
|
||||
return;
|
||||
}
|
||||
var objError = assembleError( ex );
|
||||
o( this[ 'format' + (objError.warning ? 'Warning' : 'Error')]( objError.msg ) );
|
||||
|
||||
// Get an error message -- either a HackMyResume error message or the
|
||||
// exception's associated error message
|
||||
if( ex.fluenterror ){
|
||||
var errInfo = get_error_msg( ex );
|
||||
msg = errInfo.msg;
|
||||
exitCode = ex.fluenterror;
|
||||
showStack = errInfo.showStack;
|
||||
if( objError.showStack )
|
||||
o( chalk.red( ex.stack || ex.inner.stack ) );
|
||||
|
||||
if( objError.quit ) {
|
||||
this.debug && o(
|
||||
chalk.cyan('Exiting with error code ' + ex.fluenterror.toString()));
|
||||
process.exit( ex.fluenterror );
|
||||
}
|
||||
}
|
||||
else {
|
||||
msg = ex.toString();
|
||||
exitCode = -1;
|
||||
// Deal with pesky 'Error:' prefix.
|
||||
var idx = msg.indexOf('Error: ');
|
||||
msg = idx === -1 ? msg : msg.substring( idx + 7 );
|
||||
o( ex );
|
||||
var stackTrace = ex.stack || (ex.inner && ex.inner.stack)
|
||||
if( stackTrace && this.debug )
|
||||
o( ex.stack || ex.inner.stack );
|
||||
|
||||
// if( this.debug )
|
||||
// o( ex.stack || ex.inner.stack );
|
||||
}
|
||||
|
||||
// Log non-HackMyResume-handled errors in red with ERROR prefix. Log HMR
|
||||
// errors as-is.
|
||||
ex.fluenterror ?
|
||||
log( msg.toString() ) :
|
||||
log( chalk.red.bold('ERROR: ' + msg.toString()) );
|
||||
},
|
||||
|
||||
// Selectively show the stack trace
|
||||
if( (ex.stack || (ex.inner && ex.inner.stack)) &&
|
||||
((showStack && ex.code !== 'ENOENT' ) || (this.debug) ))
|
||||
log( chalk.red( ex.stack || ex.inner.stack ) );
|
||||
formatError: function( msg ) {
|
||||
return chalk.red.bold(
|
||||
msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg );
|
||||
},
|
||||
|
||||
// Let the error code be the process's return code.
|
||||
( shouldExit || ex.shouldExit ) && process.exit( exitCode );
|
||||
formatWarning: function( brief, msg ) {
|
||||
return chalk.yellow(brief) + chalk.yellow(msg || '');
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
var o = function() {
|
||||
console.log.apply( console.log, arguments );
|
||||
};
|
||||
|
||||
function get_error_msg( ex ) {
|
||||
function assembleError( ex ) {
|
||||
|
||||
var msg = '', withStack = false, isError = false, quit = true, warn = true;
|
||||
|
||||
var msg = '', withStack = false, isError = false;
|
||||
switch( ex.fluenterror ) {
|
||||
|
||||
case HACKMYSTATUS.themeNotFound:
|
||||
msg = formatWarning(
|
||||
msg =
|
||||
chalk.bold("Couldn't find the '" + ex.data + "' theme."),
|
||||
" Please specify the name of a preinstalled FRESH theme " +
|
||||
"or the path to a locally installed FRESH or JSON Resume theme.");
|
||||
"or the path to a locally installed FRESH or JSON Resume theme.";
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.copyCSS:
|
||||
msg = formatWarning("Couldn't copy CSS file to destination folder.");
|
||||
msg = "Couldn't copy CSS file to destination folder.";
|
||||
quit = false;
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.resumeNotFound:
|
||||
msg = formatWarning('Please ' + chalk.bold('feed me a resume') +
|
||||
' in FRESH or JSON Resume format.');
|
||||
msg = 'Please ' + chalk.bold('feed me a resume') +
|
||||
' in FRESH or JSON Resume format.';
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.missingCommand:
|
||||
msg = formatWarning("Please " +chalk.bold("give me a command") + " (");
|
||||
msg = "Please " +chalk.bold("give me a command") + " (";
|
||||
|
||||
msg += Object.keys( FCMD.verbs ).map( function(v, idx, ar) {
|
||||
return (idx === ar.length - 1 ? chalk.yellow('or ') : '') +
|
||||
@ -112,79 +113,81 @@ Error-handling routines for HackMyResume.
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.invalidCommand:
|
||||
msg = formatWarning('Invalid command: "'+chalk.bold(ex.attempted)+'"');
|
||||
msg = 'Invalid command: "'+chalk.bold(ex.attempted)+'"';
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.resumeNotFoundAlt:
|
||||
msg = formatWarning('Please ' + chalk.bold('feed me a resume') +
|
||||
' in either FRESH or JSON Resume format.');
|
||||
msg = 'Please ' + chalk.bold('feed me a resume') +
|
||||
' in either FRESH or JSON Resume format.';
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.inputOutputParity:
|
||||
msg = formatWarning('Please ' +
|
||||
msg = 'Please ' +
|
||||
chalk.bold('specify an output file name') +
|
||||
' for every input file you wish to convert.');
|
||||
' for every input file you wish to convert.';
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.createNameMissing:
|
||||
msg = formatWarning('Please ' +
|
||||
chalk.bold('specify the filename of the resume') + ' to create.');
|
||||
msg = 'Please ' +
|
||||
chalk.bold('specify the filename of the resume') + ' to create.';
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.pdfGeneration:
|
||||
msg = formatError(chalk.bold('ERROR: PDF generation failed. ') +
|
||||
'Make sure wkhtmltopdf is installed and accessible from your path.');
|
||||
msg = chalk.bold('PDF generation failed. ') +
|
||||
'Make sure wkhtmltopdf is installed and accessible from your path.';
|
||||
if( ex.inner ) msg += chalk.red('\n' + ex.inner);
|
||||
withStack = true;
|
||||
withStack = true; quit = false; warn = false;
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.invalid:
|
||||
msg = formatError('Validation failed and the --assert option was ' +
|
||||
'specified.');
|
||||
msg = 'Validation failed and the --assert option was ' +
|
||||
'specified.';
|
||||
warn = false;
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.invalidFormat:
|
||||
ex.data.forEach(function(d){ msg +=
|
||||
formatWarning('The ' + chalk.bold(ex.theme.name.toUpperCase()) +
|
||||
'The ' + chalk.bold(ex.theme.name.toUpperCase()) +
|
||||
" theme doesn't support the " + chalk.bold(d.format.toUpperCase()) +
|
||||
" format.\n");
|
||||
" format.\n";
|
||||
});
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.notOnPath:
|
||||
msg = formatError( ex.engine + " wasn't found on your system path or" +
|
||||
" is inaccessible. PDF not generated." );
|
||||
msg = ex.engine + " wasn't found on your system path or" +
|
||||
" is inaccessible. PDF not generated.";
|
||||
quit = false;
|
||||
warn = false;
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.readError:
|
||||
msg = formatError( ex.inner.toString() );
|
||||
msg = ex.inner.toString();
|
||||
warn = false;
|
||||
break;
|
||||
|
||||
case HACKMYSTATUS.parseError:
|
||||
if( SyntaxErrorEx.is( ex.inner )) {
|
||||
var se = new SyntaxErrorEx( ex, ex.raw );
|
||||
msg = formatError( 'Invalid or corrupt JSON on line ' + se.line +
|
||||
' column ' + se.col );
|
||||
msg = 'Invalid or corrupt JSON on line ' + se.line +
|
||||
' column ' + se.col + '.';
|
||||
}
|
||||
else {
|
||||
msg = formatError( ex.inner.toString() );
|
||||
}
|
||||
warn = false;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
msg: msg,
|
||||
withStack: withStack
|
||||
warning: warn, // True if this is a warning, false if error
|
||||
msg: msg, // The error message to display
|
||||
withStack: withStack, // Whether to include the stack
|
||||
quit: quit
|
||||
};
|
||||
}
|
||||
|
||||
function formatError( msg ) {
|
||||
return chalk.red.bold( 'ERROR: ' + msg );
|
||||
}
|
||||
|
||||
function formatWarning( brief, msg ) {
|
||||
return chalk.yellow(brief) + chalk.yellow(msg || '');
|
||||
}
|
||||
|
||||
|
||||
}());
|
||||
|
@ -19,8 +19,6 @@ Definition of the `main` function.
|
||||
, HACKMYSTATUS = require('../core/status-codes')
|
||||
, HME = require('../core/event-codes')
|
||||
, safeLoadJSON = require('../utils/safe-json-loader')
|
||||
, _opts = { }
|
||||
, title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***')
|
||||
, StringUtils = require('../utils/string.js')
|
||||
, _ = require('underscore')
|
||||
, OUTPUT = require('./out')
|
||||
@ -28,6 +26,10 @@ Definition of the `main` function.
|
||||
, PAD = require('string-padding')
|
||||
, Command = require('commander').Command;
|
||||
|
||||
var _opts = { };
|
||||
var _title = chalk.white.bold('\n*** HackMyResume v' +PKG.version+ ' ***');
|
||||
var _out = new OUTPUT( _opts );
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -120,9 +122,19 @@ Definition of the `main` function.
|
||||
*/
|
||||
function initialize( ar ) {
|
||||
|
||||
logMsg( title );
|
||||
logMsg( _title );
|
||||
|
||||
var o = initOptions( ar );
|
||||
if( o.debug ) {
|
||||
_out.log(chalk.cyan('The -d or --debug switch was specified. DEBUG mode engaged.'));
|
||||
_out.log('');
|
||||
_out.log(chalk.cyan(PAD(' Platform:',20, null, PAD.RIGHT)) + chalk.cyan.bold( process.platform ));
|
||||
_out.log(chalk.cyan(PAD(' Node.js:',20, null, PAD.RIGHT)) + chalk.cyan.bold( process.version ));
|
||||
_out.log(chalk.cyan(PAD(' HackMyResume:',20, null, PAD.RIGHT)) + chalk.cyan.bold('v' + PKG.version ));
|
||||
_out.log(chalk.cyan(PAD(' FRESCA:',20, null, PAD.RIGHT)) + chalk.cyan.bold( PKG.dependencies.fresca ));
|
||||
_out.log(chalk.cyan(PAD(' fresh-themes:',20, null, PAD.RIGHT)) + chalk.cyan.bold( PKG.dependencies['fresh-themes'] ));
|
||||
_out.log('');
|
||||
}
|
||||
|
||||
// Handle invalid verbs here (a bit easier here than in commander.js)...
|
||||
if( o.verb && !HMR.verbs[ o.verb ] && !HMR.alias[ o.verb ] ) {
|
||||
@ -150,6 +162,10 @@ Definition of the `main` function.
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Init options prior to setting up command infrastructure.
|
||||
*/
|
||||
function initOptions( ar ) {
|
||||
var oVerb, verb = '', args = ar.slice(), cleanArgs = args.slice(2), oJSON;
|
||||
if( cleanArgs.length ) {
|
||||
@ -179,7 +195,13 @@ Definition of the `main` function.
|
||||
}
|
||||
}
|
||||
|
||||
// Grab the --debug flag
|
||||
var isDebug = _.some( args, function(v) {
|
||||
return v === '-d' || v === '--debug';
|
||||
});
|
||||
|
||||
return {
|
||||
debug: isDebug,
|
||||
orgVerb: oVerb,
|
||||
verb: verb,
|
||||
json: oJSON,
|
||||
@ -194,10 +216,11 @@ Definition of the `main` function.
|
||||
function execute( src, dst, opts, log ) {
|
||||
|
||||
loadOptions.call( this, opts, this.parent.jsonArgs );
|
||||
require( './error-handler' ).init( _opts.debug );
|
||||
var out = new OUTPUT( _opts );
|
||||
var hand = require( './error-handler' ).init( _opts.debug );
|
||||
var v = new HMR.verbs[ this.name() ]();
|
||||
v.on( 'hmr:status', function() { out.do.apply( out, arguments ); });
|
||||
_out.init( _opts );
|
||||
v.on( 'hmr:status', function() { _out.do.apply( _out, arguments ); });
|
||||
v.on( 'hmr:error', function() { hand.err.apply( hand, arguments ); });
|
||||
v.invoke.call( v, src, dst, _opts, log );
|
||||
|
||||
}
|
||||
@ -228,14 +251,15 @@ Definition of the `main` function.
|
||||
o.debug = this.parent.debug;
|
||||
|
||||
if( o.debug ) {
|
||||
logMsg(chalk.cyan('Merged options: '));
|
||||
logMsg(chalk.cyan('OPTIONS:') + '\n');
|
||||
_.each(o, function(val, key) {
|
||||
logMsg(chalk.cyan('%s: %s'), PAD(key,10), val);
|
||||
logMsg(chalk.cyan(' %s') + chalk.cyan.bold(' %s'), PAD(key,17,null,PAD.RIGHT), val);
|
||||
});
|
||||
logMsg('');
|
||||
}
|
||||
|
||||
// Cache
|
||||
_opts = o;
|
||||
EXTEND(true, _opts, o);
|
||||
}
|
||||
|
||||
|
||||
|
@ -14,50 +14,71 @@ Output routines for HackMyResume.
|
||||
, HME = require('../core/event-codes')
|
||||
, _ = require('underscore')
|
||||
, Class = require('../utils/class.js')
|
||||
, M2C = require('../utils/md2chalk.js')
|
||||
, PATH = require('path')
|
||||
, LO = require('lodash')
|
||||
, FS = require('fs')
|
||||
, EXTEND = require('../utils/extend')
|
||||
, HANDLEBARS = require('handlebars')
|
||||
, pad = require('string-padding');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
A stateful output handler. All HMR console output handled here.
|
||||
A stateful output module. All HMR console output handled here.
|
||||
*/
|
||||
var OutputHandler = module.exports = Class.extend({
|
||||
|
||||
|
||||
|
||||
init: function( opts ) {
|
||||
this.opts = opts;
|
||||
this.opts = EXTEND( true, this.opts || { }, opts );
|
||||
},
|
||||
|
||||
|
||||
|
||||
log: function( msg ) {
|
||||
msg = msg || '';
|
||||
this.opts.silent || console.log( msg );
|
||||
this.opts.silent || console.log.apply( console.log, arguments );
|
||||
},
|
||||
|
||||
|
||||
|
||||
do: function( evt ) {
|
||||
|
||||
var that = this;
|
||||
function L() {
|
||||
that.log.apply( that, arguments );
|
||||
}
|
||||
|
||||
switch( evt.sub ) {
|
||||
|
||||
case HME.error:
|
||||
//L('ERROR occured');
|
||||
break;
|
||||
|
||||
case HME.beforeCreate:
|
||||
this.log( chalk.green('Creating new ') +
|
||||
chalk.green.bold(evt.fmt) +
|
||||
chalk.green(' resume: ') + chalk.green.bold(evt.file));
|
||||
L(
|
||||
M2C('Creating new **%s** resume: **%s**', 'green'),
|
||||
evt.fmt, evt.file
|
||||
);
|
||||
break;
|
||||
|
||||
case HME.beforeRead:
|
||||
this.log( chalk.cyan('Reading resume: ' + chalk.bold( evt.file )));
|
||||
L(
|
||||
M2C('Reading resume: **%s**', 'cyan'), evt.file
|
||||
);
|
||||
break;
|
||||
|
||||
case HME.beforeTheme:
|
||||
this.opts.debug && L(
|
||||
M2C('Verifying theme: **%s**', 'cyan'), evt.theme.toUpperCase()
|
||||
);
|
||||
break;
|
||||
|
||||
case HME.afterTheme:
|
||||
this.theme = evt.theme;
|
||||
this.opts.debug && L( M2C('Verifying outputs: ???', 'cyan') );
|
||||
break;
|
||||
|
||||
case HME.beforeMerge:
|
||||
@ -66,15 +87,14 @@ Output routines for HackMyResume.
|
||||
msg += ((idx === 0) ? chalk.cyan('Merging ') :
|
||||
chalk.cyan(' onto ')) + chalk.cyan.bold(a.i().file);
|
||||
});
|
||||
this.log( msg );
|
||||
L( msg );
|
||||
break;
|
||||
|
||||
case HME.afterMerge:
|
||||
var numFormats = Object.keys(this.theme.formats).length;
|
||||
this.log( chalk.yellow('Applying ') +
|
||||
chalk.yellow.bold( this.theme.name.toUpperCase() ) +
|
||||
chalk.yellow(' theme (' + numFormats + ' format' +
|
||||
( evt.numFormats === 1 ? ')' : 's)') ));
|
||||
var numFormats = Object.keys( this.theme.formats ).length;
|
||||
L( M2C('Applying **%s** theme (%s format%s)', 'yellow'),
|
||||
this.theme.name.toUpperCase(),
|
||||
numFormats, ( numFormats === 1 ? '' : 's') );
|
||||
break;
|
||||
|
||||
case HME.end:
|
||||
@ -83,23 +103,21 @@ Output routines for HackMyResume.
|
||||
if( this.opts.tips && (this.theme.message || this.theme.render) ) {
|
||||
var WRAP = require('word-wrap');
|
||||
if( this.theme.message ) {
|
||||
this.log( WRAP( chalk.gray('The ' + themeName + ' theme says: "') +
|
||||
L( WRAP( chalk.gray('The ' + themeName + ' theme says: "') +
|
||||
chalk.white(this.theme.message) + chalk.gray('"'),
|
||||
{ width: this.opts.wrap, indent: '' } ));
|
||||
}
|
||||
else if ( this.theme.render ) {
|
||||
this.log( WRAP( chalk.gray('The ' + themeName +
|
||||
' theme says: "') + chalk.white('For best results view JSON ' +
|
||||
'Resume themes over a local or remote HTTP connection. For ' +
|
||||
'example:'), { width: this.opts.wrap, indent: '' }
|
||||
));
|
||||
this.log( '');
|
||||
this.log(
|
||||
' npm install http-server -g\r' +
|
||||
L( M2C( 'The **' + themeName + '** theme says:', 'cyan'));
|
||||
L( WRAP( '"For best results view JSON Resume themes over a ' +
|
||||
'local or remote HTTP connection. For example:',
|
||||
{ width: this.opts.wrap, indent: '' })
|
||||
);
|
||||
L('');
|
||||
L(' npm install http-server -g\r' +
|
||||
' http-server <resume-folder>' );
|
||||
this.log('');
|
||||
this.log(chalk.white('For more information, see the README."'),
|
||||
{ width: this.opts.wrap, indent: '' } );
|
||||
L('');
|
||||
L(chalk.white('For more information, see the README."'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -113,7 +131,7 @@ Output routines for HackMyResume.
|
||||
suffix = chalk.green(' (with ' + this.opts.pdf + ')');
|
||||
}
|
||||
else {
|
||||
this.log( chalk.gray('Skipping ') +
|
||||
L( chalk.gray('Skipping ') +
|
||||
chalk.white.bold( pad(evt.fmt.toUpperCase(),4,null,pad.RIGHT)) +
|
||||
chalk.gray(' resume') + suffix + chalk.green(': ') +
|
||||
chalk.white( evt.file ));
|
||||
@ -122,7 +140,7 @@ Output routines for HackMyResume.
|
||||
}
|
||||
}
|
||||
|
||||
this.log( chalk.green('Generating ') +
|
||||
L( chalk.green('Generating ') +
|
||||
chalk.green.bold(
|
||||
pad(evt.fmt.toUpperCase(),4,null,pad.RIGHT)) +
|
||||
chalk.green(' resume') + suffix + chalk.green(': ') +
|
||||
@ -130,7 +148,7 @@ Output routines for HackMyResume.
|
||||
break;
|
||||
|
||||
case HME.beforeAnalyze:
|
||||
this.log(chalk.cyan('Analyzing ') + chalk.cyan.bold(evt.fmt) +
|
||||
L(chalk.cyan('Analyzing ') + chalk.cyan.bold(evt.fmt) +
|
||||
chalk.cyan(' resume: ') + chalk.cyan.bold(evt.file));
|
||||
break;
|
||||
|
||||
@ -148,20 +166,20 @@ Output routines for HackMyResume.
|
||||
|
||||
case HME.beforeConvert:
|
||||
// TODO: Core should not log
|
||||
this.log( chalk.green('Converting ') + chalk.green.bold(evt.srcFile) +
|
||||
L( chalk.green('Converting ') + chalk.green.bold(evt.srcFile) +
|
||||
chalk.green(' (' + evt.srcFmt + ') to ') + chalk.green.bold(evt.dstFile) +
|
||||
chalk.green(' (' + evt.dstFmt + ').'));
|
||||
break;
|
||||
|
||||
case HME.afterValidate:
|
||||
var style = evt.isValid ? 'green' : 'yellow';
|
||||
this.log( chalk.white('Validating ') + chalk.white.bold(evt.file) + chalk.white(' against ') +
|
||||
L( chalk.white('Validating ') + chalk.white.bold(evt.file) + chalk.white(' against ') +
|
||||
chalk.white.bold( evt.fmt ).toUpperCase() +
|
||||
chalk.white(' schema: ') + chalk[style].bold(evt.isValid ? 'VALID!' : 'INVALID'));
|
||||
|
||||
if( evt.errors ) {
|
||||
_.each(evt.errors, function(err,idx) {
|
||||
this.log( chalk.yellow.bold('--> ') +
|
||||
L( chalk.yellow.bold('--> ') +
|
||||
chalk.yellow(err.field.replace('data.','resume.').toUpperCase() + ' ' +
|
||||
err.message) );
|
||||
}, this);
|
||||
|
Reference in New Issue
Block a user