1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-22 16:30:11 +00:00

Refactor command processing.

This commit is contained in:
hacksalot 2016-01-02 00:15:46 -05:00
parent 47553b6def
commit a95b52acd0
8 changed files with 74 additions and 60 deletions

View File

@ -92,7 +92,7 @@ Definition of the ResumeFactory class.
try { try {
// TODO: Core should not log // TODO: Core should not log
opts.log( chalk.gray('Reading resume: ') + chalk.cyan.bold(fileName) ); opts.log( chalk.cyan('Reading resume: ') + chalk.cyan.bold(fileName) );
// Read the file // Read the file
rawData = FS.readFileSync( fileName, 'utf8' ); rawData = FS.readFileSync( fileName, 'utf8' );
@ -106,7 +106,7 @@ Definition of the ResumeFactory class.
catch( ex ) { catch( ex ) {
// JSON.parse failed due to invalid JSON // JSON.parse failed due to invalid JSON
if ( ex instanceof SyntaxError) { if ( !opts.muffle && ex instanceof SyntaxError) {
var info = new SyntaxErrorEx( ex, rawData ); var info = new SyntaxErrorEx( ex, rawData );
opts.log( chalk.red.bold(fileName.toUpperCase() + ' contains invalid JSON on line ' + opts.log( chalk.red.bold(fileName.toUpperCase() + ' contains invalid JSON on line ' +
info.line + ' column ' + info.col + '.' + info.line + ' column ' + info.col + '.' +
@ -118,7 +118,8 @@ Definition of the ResumeFactory class.
if( opts.throw ) throw ex; if( opts.throw ) throw ex;
else return { else return {
error: ex, error: ex,
raw: rawData raw: rawData,
file: fileName
}; };
} }

View File

@ -239,7 +239,6 @@ Definition of the TemplateGenerator class.
var t; var t;
if( this.opts.theme.startsWith('jsonresume-theme-') ) { if( this.opts.theme.startsWith('jsonresume-theme-') ) {
console.log('LOADING JSON RESUME');
t = new JRSTheme().open( tFolder ); t = new JRSTheme().open( tFolder );
} }
else { else {
@ -274,6 +273,7 @@ Definition of the TemplateGenerator class.
} }
catch(ex) { catch(ex) {
console.log(ex); console.log(ex);
throw ex;
} }
} }

View File

@ -17,7 +17,7 @@ var SPAWNW = require('./core/spawn-watch')
, PATH = require('path') , PATH = require('path')
, HACKMYSTATUS = require('./core/status-codes') , HACKMYSTATUS = require('./core/status-codes')
, opts = { } , opts = { }
, title = chalk.white('\n*** HackMyResume v' + PKG.version + ' ***') , title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***')
, _ = require('underscore'); , _ = require('underscore');
@ -61,7 +61,6 @@ function main() {
// Massage inputs and outputs // Massage inputs and outputs
var src = a._.slice(1, splitAt === -1 ? undefined : splitAt ); var src = a._.slice(1, splitAt === -1 ? undefined : splitAt );
var dst = splitAt === -1 ? [] : a._.slice( splitAt + 1 ); var dst = splitAt === -1 ? [] : a._.slice( splitAt + 1 );
( splitAt === -1 ) && (src.length > 1) && (verb !== 'validate') && dst.push( src.pop() ); // Allow omitting TO keyword
// Invoke the action // Invoke the action
(FCMD.verbs[verb] || FCMD.alias[verb]).apply(null, [src, dst, opts, logMsg]); (FCMD.verbs[verb] || FCMD.alias[verb]).apply(null, [src, dst, opts, logMsg]);

View File

@ -22,14 +22,19 @@ Implementation of the 'analyze' verb for HackMyResume.
/** /**
Run the 'analyze' command. Run the 'analyze' command.
*/ */
module.exports = function analyze( src, dst, opts, logger ) { module.exports = function analyze( sources, dst, opts, logger ) {
var _log = logger || console.log; var _log = logger || console.log;
if( !src || !src.length ) throw { fluenterror: 3 }; if( !sources || !sources.length ) throw { fluenterror: 3 };
var sourceResumes = ResumeFactory.load( src, _log, null, true );
var nlzrs = _loadInspectors(); var nlzrs = _loadInspectors();
sourceResumes.forEach( function(r) {
_analyze( r, nlzrs, opts, _log ); sources.forEach( function(src) {
var result = ResumeFactory.loadOne( src, {
log: _log, format: 'FRESH', objectify: true, throw: false
}); });
result.error || _analyze( result, nlzrs, opts, _log );
});
}; };
@ -39,7 +44,9 @@ Implementation of the 'analyze' verb for HackMyResume.
*/ */
function _analyze( resumeObject, nlzrs, opts, log ) { function _analyze( resumeObject, nlzrs, opts, log ) {
var rez = resumeObject.rez; var rez = resumeObject.rez;
var safeFormat = rez.meta.format.startsWith('FRESH') ? 'FRESH' : 'JRS'; var safeFormat =
(rez.meta && rez.meta.format && rez.meta.format.startsWith('FRESH')) ?
'FRESH' : 'JRS';
log(chalk.cyan('Analyzing ') + chalk.cyan.bold(safeFormat) + log(chalk.cyan('Analyzing ') + chalk.cyan.bold(safeFormat) +
chalk.cyan(' resume: ') + chalk.cyan.bold(resumeObject.file)); chalk.cyan(' resume: ') + chalk.cyan.bold(resumeObject.file));
var info = _.mapObject( nlzrs, function(val, key) { var info = _.mapObject( nlzrs, function(val, key) {

View File

@ -18,42 +18,42 @@ Implementation of the 'convert' verb for HackMyResume.
/** /**
Convert between FRESH and JRS formats. Convert between FRESH and JRS formats.
*/ */
module.exports = function convert( sources, dst, opts, logger ) { module.exports = function convert( srcs, dst, opts, logger ) {
// Housekeeping // Housekeeping
var _log = logger || console.log; var _log = logger || console.log;
if( !sources || !sources.length ) { throw { fluenterror: 6 }; } if( !srcs || !srcs.length ) { throw { fluenterror: 6 }; }
if( !dst || !dst.length ) { if( !dst || !dst.length ) {
if( sources.length === 1 ) { throw { fluenterror: 5 }; } if( srcs.length === 1 ) { throw { fluenterror: 5 }; }
else if( sources.length === 2 ) { else if( srcs.length === 2 ) { dst = dst || []; dst.push( srcs.pop() ); }
dst = [ sources[1] ]; sources = [ sources[0] ];
}
else { throw { fluenterror: 5 }; } else { throw { fluenterror: 5 }; }
} }
if( sources && dst && sources.length && dst.length && if( srcs && dst && srcs.length && dst.length &&
sources.length !== dst.length ) { throw { fluenterror: 7 }; } srcs.length !== dst.length ) { throw { fluenterror: 7 }; }
// Load source resumes // Load source resumes
var sourceResumes = ResumeFactory.load( sources, { srcs.forEach( function( src, idx ) {
// Load the resume
var rinfo = ResumeFactory.loadOne( src, {
log: _log, format: null, objectify: true, throw: true log: _log, format: null, objectify: true, throw: true
}); });
// Apply the conversion to each var s = rinfo.rez
sourceResumes.forEach(function( src, idx ) {
var s = src.rez
, srcFmt = ((s.basics && s.basics.imp) || s.imp).orgFormat === 'JRS' ? , srcFmt = ((s.basics && s.basics.imp) || s.imp).orgFormat === 'JRS' ?
'JRS' : 'FRESH'; 'JRS' : 'FRESH'
, targetFormat = srcFmt === 'JRS' ? 'FRESH' : 'JRS';
var targetFormat = srcFmt === 'JRS' ? 'FRESH' : 'JRS';
// TODO: Core should not log // TODO: Core should not log
_log( chalk.green('Converting ') + chalk.green.bold(src.file) + _log( chalk.green('Converting ') + chalk.green.bold(rinfo.file) +
chalk.green(' (' + sourceFormat + ') to ') + chalk.green.bold(dst[0]) + chalk.green(' (' + srcFmt + ') to ') + chalk.green.bold(dst[0]) +
chalk.green(' (' + targetFormat + ').')); chalk.green(' (' + targetFormat + ').'));
// Save it to the destination format
s.saveAs( dst[idx], targetFormat ); s.saveAs( dst[idx], targetFormat );
}); });
}; };

View File

@ -45,14 +45,20 @@ Implementation of the 'generate' verb for HackMyResume.
*/ */
function build( src, dst, opts, logger, errHandler ) { function build( src, dst, opts, logger, errHandler ) {
// Housekeeping... // Housekeeping
//_opts = extend( true, _opts, opts );
_log = logger || console.log; _log = logger || console.log;
_err = errHandler || error; _err = errHandler || error;
//_opts = extend( true, _opts, opts );
_opts.theme = (opts.theme && opts.theme.toLowerCase().trim())|| 'modern'; _opts.theme = (opts.theme && opts.theme.toLowerCase().trim())|| 'modern';
_opts.prettify = opts.prettify === true ? _opts.prettify : false; _opts.prettify = opts.prettify === true ? _opts.prettify : false;
_opts.css = opts.css; _opts.css = opts.css;
// If two or more files are passed to the GENERATE command and the TO
// keyword is omitted, the last file specifies the output file.
if( src.length > 1 && ( !dst || !dst.length ) ) {
dst.push( src.pop() );
}
// Load the theme... // Load the theme...
var tFolder = verify_theme( _opts.theme ); var tFolder = verify_theme( _opts.theme );
var theme = load_theme( tFolder ); var theme = load_theme( tFolder );
@ -60,18 +66,18 @@ Implementation of the 'generate' verb for HackMyResume.
// Load input resumes... // Load input resumes...
if( !src || !src.length ) { throw { fluenterror: 3 }; } if( !src || !src.length ) { throw { fluenterror: 3 }; }
var sheets = ResumeFactory.load(src, { var sheets = ResumeFactory.load(src, {
log: _log, format: theme.render ? 'JRS' : 'FRESH', objectify: true, throw: true log: _log, format: theme.render ? 'JRS' : 'FRESH',
}); objectify: true, throw: true
}).map(function(sh){ return sh.rez; });
// Merge input resumes... // Merge input resumes...
var msg = ''; var msg = '';
var rezRep = _.reduceRight( sheets, function( a, b, idx ) { rez = _.reduceRight( sheets, function( a, b, idx ) {
msg += ((idx == sheets.length - 2) ? msg += ((idx == sheets.length - 2) ?
chalk.gray('Merging ') + a.rez.imp.fileName : '') + chalk.gray(' onto ') + b.rez.fileName; chalk.cyan('Merging ') + chalk.cyan.bold(a.file) : '') +
return extend( true, b.rez, a.rez ); chalk.cyan(' onto ') + chalk.cyan.bold(b.file);
return extend( true, b, a );
}); });
rez = rezRep.rez;
msg && _log(msg); msg && _log(msg);
// Expand output resumes... // Expand output resumes...
@ -277,8 +283,8 @@ Implementation of the 'generate' verb for HackMyResume.
// Output a message TODO: core should not log // Output a message TODO: core should not log
var numFormats = Object.keys(theTheme.formats).length; var numFormats = Object.keys(theTheme.formats).length;
_log( chalk.gray('Applying ') + chalk.gray.bold(theTheme.name.toUpperCase()) + _log( chalk.yellow('Applying ') + chalk.yellow.bold(theTheme.name.toUpperCase()) +
chalk.gray(' theme (' + numFormats + ' formats)')); chalk.yellow(' theme (' + numFormats + ' formats)'));
return theTheme; return theTheme;
} }

View File

@ -27,26 +27,27 @@ Implementation of the 'validate' verb for HackMyResume.
jars: require('../core/resume.json') jars: require('../core/resume.json')
}; };
// Load input resumes... var resumes = ResumeFactory.load( sources, {
sources.forEach(function( src ) { log: _log,
var result = ResumeFactory.loadOne( src, {
log: function(){},
format: null, format: null,
objectify: false, objectify: false,
throw: false throw: false,
muffle: true
}); });
if( result.error ) { // Load input resumes...
resumes.forEach(function( src ) {
if( src.error ) {
// TODO: Core should not log // TODO: Core should not log
_log( chalk.white('Validating ') + chalk.gray.bold(src) + _log( chalk.white('Validating ') + chalk.gray.bold(src.file) +
chalk.white(' against ') + chalk.gray.bold('AUTO') + chalk.white(' against ') + chalk.gray.bold('AUTO') +
chalk.white(' schema:') + chalk.red.bold(' BROKEN') ); chalk.white(' schema:') + chalk.red.bold(' BROKEN') );
var ex = result.error; // alias var ex = src.error; // alias
if ( ex instanceof SyntaxError) { if ( ex instanceof SyntaxError) {
var info = new SyntaxErrorEx( ex, result.raw ); var info = new SyntaxErrorEx( ex, src.raw );
_log( chalk.red.bold('--> ' + src.toUpperCase() + ' contains invalid JSON on line ' + _log( chalk.red.bold('--> ' + src.file.toUpperCase() + ' contains invalid JSON on line ' +
info.line + ' column ' + info.col + '.' + info.line + ' column ' + info.col + '.' +
chalk.red(' Unable to validate.') ) ); chalk.red(' Unable to validate.') ) );
_log( chalk.red.bold(' INTERNAL: ' + ex) ); _log( chalk.red.bold(' INTERNAL: ' + ex) );
@ -57,7 +58,7 @@ Implementation of the 'validate' verb for HackMyResume.
return; return;
} }
var json = result.json; var json = src.json;
var isValid = false; var isValid = false;
var style = 'green'; var style = 'green';
var errors = []; var errors = [];
@ -81,7 +82,7 @@ Implementation of the 'validate' verb for HackMyResume.
return; return;
} }
_log( chalk.white('Validating ') + chalk.white.bold(result.file) + chalk.white(' against ') + _log( chalk.white('Validating ') + chalk.white.bold(src.file) + chalk.white(' against ') +
chalk.white.bold(fmt.replace('jars','JSON Resume').toUpperCase()) + chalk.white.bold(fmt.replace('jars','JSON Resume').toUpperCase()) +
chalk.white(' schema: ') + chalk[style].bold(isValid ? 'VALID!' : 'INVALID') ); chalk.white(' schema: ') + chalk[style].bold(isValid ? 'VALID!' : 'INVALID') );

View File

@ -12,7 +12,7 @@ var SPAWNWATCHER = require('../src/core/spawn-watch')
, fileContains = require('../src/utils/file-contains') , fileContains = require('../src/utils/file-contains')
, FS = require('fs'); , FS = require('fs');
chai.config.includeStack = false; chai.config.includeStack = true;
function genThemes( title, src, fmt ) { function genThemes( title, src, fmt ) {
@ -31,9 +31,9 @@ function genThemes( title, src, fmt ) {
theme: themeLoc, theme: themeLoc,
format: fmt, format: fmt,
prettify: true, prettify: true,
silent: true silent: false
}; };
FCMD.verbs.build( src, dst, opts, function() {} ); FCMD.verbs.build( src, dst, opts, function(msg) { console.log(msg); } );
} }
tryOpen.should.not.Throw(); tryOpen.should.not.Throw();
}); });