From a95b52acd0aef65ea9ae4c8a3e64bf9164ba4aba Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sat, 2 Jan 2016 00:15:46 -0500 Subject: [PATCH] Refactor command processing. --- src/core/resume-factory.js | 7 ++++--- src/gen/template-generator.js | 2 +- src/index.js | 3 +-- src/verbs/analyze.js | 19 ++++++++++++------ src/verbs/convert.js | 38 +++++++++++++++++------------------ src/verbs/generate.js | 28 ++++++++++++++++---------- src/verbs/validate.js | 31 ++++++++++++++-------------- test/test-themes.js | 6 +++--- 8 files changed, 74 insertions(+), 60 deletions(-) diff --git a/src/core/resume-factory.js b/src/core/resume-factory.js index dda26cc..025ca1f 100644 --- a/src/core/resume-factory.js +++ b/src/core/resume-factory.js @@ -92,7 +92,7 @@ Definition of the ResumeFactory class. try { // 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 rawData = FS.readFileSync( fileName, 'utf8' ); @@ -106,7 +106,7 @@ Definition of the ResumeFactory class. catch( ex ) { // JSON.parse failed due to invalid JSON - if ( ex instanceof SyntaxError) { + if ( !opts.muffle && ex instanceof SyntaxError) { var info = new SyntaxErrorEx( ex, rawData ); opts.log( chalk.red.bold(fileName.toUpperCase() + ' contains invalid JSON on line ' + info.line + ' column ' + info.col + '.' + @@ -118,7 +118,8 @@ Definition of the ResumeFactory class. if( opts.throw ) throw ex; else return { error: ex, - raw: rawData + raw: rawData, + file: fileName }; } diff --git a/src/gen/template-generator.js b/src/gen/template-generator.js index 257f477..2eadd55 100644 --- a/src/gen/template-generator.js +++ b/src/gen/template-generator.js @@ -239,7 +239,6 @@ Definition of the TemplateGenerator class. var t; if( this.opts.theme.startsWith('jsonresume-theme-') ) { - console.log('LOADING JSON RESUME'); t = new JRSTheme().open( tFolder ); } else { @@ -274,6 +273,7 @@ Definition of the TemplateGenerator class. } catch(ex) { console.log(ex); + throw ex; } } diff --git a/src/index.js b/src/index.js index dad71a7..9d8ac12 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ var SPAWNW = require('./core/spawn-watch') , PATH = require('path') , HACKMYSTATUS = require('./core/status-codes') , opts = { } - , title = chalk.white('\n*** HackMyResume v' + PKG.version + ' ***') + , title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***') , _ = require('underscore'); @@ -61,7 +61,6 @@ function main() { // Massage inputs and outputs var src = a._.slice(1, splitAt === -1 ? undefined : splitAt ); 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 (FCMD.verbs[verb] || FCMD.alias[verb]).apply(null, [src, dst, opts, logMsg]); diff --git a/src/verbs/analyze.js b/src/verbs/analyze.js index d8d177b..48fbbc4 100644 --- a/src/verbs/analyze.js +++ b/src/verbs/analyze.js @@ -22,14 +22,19 @@ Implementation of the 'analyze' verb for HackMyResume. /** 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; - if( !src || !src.length ) throw { fluenterror: 3 }; - var sourceResumes = ResumeFactory.load( src, _log, null, true ); + if( !sources || !sources.length ) throw { fluenterror: 3 }; + 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 ) { 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) + chalk.cyan(' resume: ') + chalk.cyan.bold(resumeObject.file)); var info = _.mapObject( nlzrs, function(val, key) { diff --git a/src/verbs/convert.js b/src/verbs/convert.js index 2539c96..e5e483a 100644 --- a/src/verbs/convert.js +++ b/src/verbs/convert.js @@ -18,42 +18,42 @@ Implementation of the 'convert' verb for HackMyResume. /** Convert between FRESH and JRS formats. */ - module.exports = function convert( sources, dst, opts, logger ) { + module.exports = function convert( srcs, dst, opts, logger ) { // Housekeeping var _log = logger || console.log; - if( !sources || !sources.length ) { throw { fluenterror: 6 }; } + if( !srcs || !srcs.length ) { throw { fluenterror: 6 }; } if( !dst || !dst.length ) { - if( sources.length === 1 ) { throw { fluenterror: 5 }; } - else if( sources.length === 2 ) { - dst = [ sources[1] ]; sources = [ sources[0] ]; - } + if( srcs.length === 1 ) { throw { fluenterror: 5 }; } + else if( srcs.length === 2 ) { dst = dst || []; dst.push( srcs.pop() ); } else { throw { fluenterror: 5 }; } } - if( sources && dst && sources.length && dst.length && - sources.length !== dst.length ) { throw { fluenterror: 7 }; } + if( srcs && dst && srcs.length && dst.length && + srcs.length !== dst.length ) { throw { fluenterror: 7 }; } // Load source resumes - var sourceResumes = ResumeFactory.load( sources, { - log: _log, format: null, objectify: true, throw: true - }); + srcs.forEach( function( src, idx ) { - // Apply the conversion to each - sourceResumes.forEach(function( src, idx ) { + // Load the resume + var rinfo = ResumeFactory.loadOne( src, { + log: _log, format: null, objectify: true, throw: true + }); - var s = src.rez + var s = rinfo.rez , srcFmt = ((s.basics && s.basics.imp) || s.imp).orgFormat === 'JRS' ? - 'JRS' : 'FRESH'; - - var targetFormat = srcFmt === 'JRS' ? 'FRESH' : 'JRS'; + 'JRS' : 'FRESH' + , targetFormat = srcFmt === 'JRS' ? 'FRESH' : 'JRS'; // TODO: Core should not log - _log( chalk.green('Converting ') + chalk.green.bold(src.file) + - chalk.green(' (' + sourceFormat + ') to ') + chalk.green.bold(dst[0]) + + _log( chalk.green('Converting ') + chalk.green.bold(rinfo.file) + + chalk.green(' (' + srcFmt + ') to ') + chalk.green.bold(dst[0]) + chalk.green(' (' + targetFormat + ').')); + // Save it to the destination format s.saveAs( dst[idx], targetFormat ); + }); + }; diff --git a/src/verbs/generate.js b/src/verbs/generate.js index 3351962..ca912d1 100644 --- a/src/verbs/generate.js +++ b/src/verbs/generate.js @@ -45,14 +45,20 @@ Implementation of the 'generate' verb for HackMyResume. */ function build( src, dst, opts, logger, errHandler ) { - // Housekeeping... + // Housekeeping + //_opts = extend( true, _opts, opts ); _log = logger || console.log; _err = errHandler || error; - //_opts = extend( true, _opts, opts ); _opts.theme = (opts.theme && opts.theme.toLowerCase().trim())|| 'modern'; _opts.prettify = opts.prettify === true ? _opts.prettify : false; _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... var tFolder = verify_theme( _opts.theme ); var theme = load_theme( tFolder ); @@ -60,18 +66,18 @@ Implementation of the 'generate' verb for HackMyResume. // Load input resumes... if( !src || !src.length ) { throw { fluenterror: 3 }; } 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... var msg = ''; - var rezRep = _.reduceRight( sheets, function( a, b, idx ) { + rez = _.reduceRight( sheets, function( a, b, idx ) { msg += ((idx == sheets.length - 2) ? - chalk.gray('Merging ') + a.rez.imp.fileName : '') + chalk.gray(' onto ') + b.rez.fileName; - return extend( true, b.rez, a.rez ); + chalk.cyan('Merging ') + chalk.cyan.bold(a.file) : '') + + chalk.cyan(' onto ') + chalk.cyan.bold(b.file); + return extend( true, b, a ); }); - - rez = rezRep.rez; msg && _log(msg); // Expand output resumes... @@ -277,8 +283,8 @@ Implementation of the 'generate' verb for HackMyResume. // Output a message TODO: core should not log var numFormats = Object.keys(theTheme.formats).length; - _log( chalk.gray('Applying ') + chalk.gray.bold(theTheme.name.toUpperCase()) + - chalk.gray(' theme (' + numFormats + ' formats)')); + _log( chalk.yellow('Applying ') + chalk.yellow.bold(theTheme.name.toUpperCase()) + + chalk.yellow(' theme (' + numFormats + ' formats)')); return theTheme; } diff --git a/src/verbs/validate.js b/src/verbs/validate.js index 2d7f744..7d4eefb 100644 --- a/src/verbs/validate.js +++ b/src/verbs/validate.js @@ -27,26 +27,27 @@ Implementation of the 'validate' verb for HackMyResume. jars: require('../core/resume.json') }; + var resumes = ResumeFactory.load( sources, { + log: _log, + format: null, + objectify: false, + throw: false, + muffle: true + }); + // Load input resumes... - sources.forEach(function( src ) { + resumes.forEach(function( src ) { - var result = ResumeFactory.loadOne( src, { - log: function(){}, - format: null, - objectify: false, - throw: false - }); - - if( result.error ) { + if( src.error ) { // 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(' schema:') + chalk.red.bold(' BROKEN') ); - var ex = result.error; // alias + var ex = src.error; // alias if ( ex instanceof SyntaxError) { - var info = new SyntaxErrorEx( ex, result.raw ); - _log( chalk.red.bold('--> ' + src.toUpperCase() + ' contains invalid JSON on line ' + + var info = new SyntaxErrorEx( ex, src.raw ); + _log( chalk.red.bold('--> ' + src.file.toUpperCase() + ' contains invalid JSON on line ' + info.line + ' column ' + info.col + '.' + chalk.red(' Unable to validate.') ) ); _log( chalk.red.bold(' INTERNAL: ' + ex) ); @@ -57,7 +58,7 @@ Implementation of the 'validate' verb for HackMyResume. return; } - var json = result.json; + var json = src.json; var isValid = false; var style = 'green'; var errors = []; @@ -81,7 +82,7 @@ Implementation of the 'validate' verb for HackMyResume. 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(' schema: ') + chalk[style].bold(isValid ? 'VALID!' : 'INVALID') ); diff --git a/test/test-themes.js b/test/test-themes.js index fa10848..b7ff672 100644 --- a/test/test-themes.js +++ b/test/test-themes.js @@ -12,7 +12,7 @@ var SPAWNWATCHER = require('../src/core/spawn-watch') , fileContains = require('../src/utils/file-contains') , FS = require('fs'); -chai.config.includeStack = false; +chai.config.includeStack = true; function genThemes( title, src, fmt ) { @@ -31,9 +31,9 @@ function genThemes( title, src, fmt ) { theme: themeLoc, format: fmt, 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(); });