From 317de75a5b2be5ae3a61a8fe6429668c0bd546ee Mon Sep 17 00:00:00 2001 From: devlinjd Date: Sat, 21 Nov 2015 07:59:30 -0500 Subject: [PATCH] Refactor. --- src/fluentcmd.js | 48 +++++++++++++++++++++++++++++++----------------- src/index.js | 23 +++++++++++++++-------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/fluentcmd.js b/src/fluentcmd.js index 09f7395..040b2d9 100644 --- a/src/fluentcmd.js +++ b/src/fluentcmd.js @@ -37,10 +37,7 @@ module.exports = function () { // Load input resumes... if(!src || !src.length) { throw { fluenterror: 3 }; } - var sheets = src.map( function( res ) { - _log( 'Reading '.gray + 'SOURCE' + ' resume: '.gray + res.cyan.bold ); - return (new FLUENT.FRESHResume()).open( res ); - }); + var sheets = loadSourceResumes( src ); // Merge input resumes... var msg = ''; @@ -136,19 +133,30 @@ module.exports = function () { jars: require('./core/resume.json') }; - var sheets = src.map( function( file ) { - - var textData = ''; + // Load input resumes... + var sheets = loadSourceResumes(src, function( res ) { try { - textData = FS.readFileSync( file, 'utf8' ); - var rez = JSON.parse( textData ); + return { + file: res, + raw: FS.readFileSync( res, 'utf8' ) + }; } catch( ex ) { - _log('Validating ' + file.cyan.bold + ' against FRESH/JRS schema: ' + 'ERROR!'.red.bold); + throw ex; + } + }); + + sheets.forEach( function( rep ) { + + try { + var rez = JSON.parse( rep.raw ); + } + catch( ex ) { + _log('Validating '.gray + rep.file.cyan.bold + ' against FRESH/JRS schema: '.gray + 'ERROR!'.red.bold); if (ex instanceof SyntaxError) { // Invalid JSON - _log( '--> '.bold.red + file.toUpperCase().red + ' contains invalid JSON. Unable to validate.'.red ); + _log( '--> '.bold.red + rep.file.toUpperCase().red + ' contains invalid JSON. Unable to validate.'.red ); _log( (' INTERNAL: ' + ex).red ); } else { @@ -159,8 +167,8 @@ module.exports = function () { } var fmt = rez.meta && rez.meta.format === 'FRESH@0.1.0' ? 'fresh':'jars'; - process.stdout.write( 'Validating ' + file.cyan.bold + ' against ' + - fmt.replace('jars','JSON Resume').toUpperCase() + ' schema: ' ); + process.stdout.write( 'Validating '.gray + rep.file + ' against '.gray + + fmt.replace('jars','JSON Resume').toUpperCase() + ' schema: '.gray ); var validate = validator( schemas[ fmt ], { // Note [1] formats: { date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ } @@ -189,15 +197,21 @@ module.exports = function () { function convert( src, dst, opts, logger ) { _log = logger || console.log; if( !src || src.length !== 1 ) { throw { fluenterror: 3 }; } - _log( 'Reading JSON resume: ' + src[0] ); - var sheet = (new FLUENT.FRESHResume()).open( src[ 0 ] ); + var sheet = loadSourceResumes( src )[ 0 ]; var sourceFormat = sheet.imp.orgFormat === 'JRS' ? 'JRS' : 'FRESH'; var targetFormat = sourceFormat === 'JRS' ? 'FRESH' : 'JRS'; - _log( 'Converting ' + src[0] + ' (' + sourceFormat + ') to ' + dst[0] + - ' (' + targetFormat + ').' ); + _log( 'Converting '.gray + src[0] + (' (' + sourceFormat + ') to ').gray + dst[0] + + (' (' + targetFormat + ').').gray ); sheet.saveAs( dst[0], targetFormat ); } + function loadSourceResumes( src, fn ) { + return src.map( function( res ) { + _log( 'Reading '.gray + 'SOURCE' + ' resume: '.gray + res.cyan.bold ); + return (fn && fn(res)) || (new FLUENT.FRESHResume()).open( res ); + }); + } + /** Supported resume formats. */ diff --git a/src/index.js b/src/index.js index 879eef5..c151bfe 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,8 @@ Command-line interface (CLI) for FluentCV:CLI. var ARGS = require( 'minimist' ) , FCMD = require( './fluentcmd') , PKG = require('../package.json') - , opts = { }; + , opts = { } + , title = ('*** FluentCV v' + PKG.version + ' ***').white.bold; @@ -23,9 +24,7 @@ catch( ex ) { function main() { - // Setup - var title = ('*** FluentCV v' + PKG.version + ' ***').bold; - if( process.argv.length <= 2 ) { logMsg(title); throw { fluenterror: 4 }; } + if( process.argv.length <= 2 ) { throw { fluenterror: 4 }; } var a = ARGS( process.argv.slice(2) ); opts = getOpts( a ); logMsg( title ); @@ -33,7 +32,8 @@ function main() { // Get the action to be performed var verb = a._[0].toLowerCase().trim(); if( !FCMD.verbs[ verb ] ) { - throw 'Invalid command: "' + verb + '"'; + logMsg('Invalid command: "'.yellow + verb.yellow.bold + '"'.yellow); + return; } // Preload our params array @@ -66,8 +66,12 @@ function handleError( ex ) { switch( ex.fluenterror ) { // TODO: Remove magic numbers case 1: msg = "The specified theme couldn't be found: " + ex.data; break; case 2: msg = "Couldn't copy CSS file to destination folder"; break; - case 3: msg = "Please specify a valid JSON resume file."; break; - case 4: msg = "Please specify a valid command (GENERATE, VALIDATE, or CONVERT)." + case 3: msg = "Please specify a valid SOURCE resume in FRESH or JSON Resume format.".gray; break; + case 4: msg = title + "\nPlease specify a valid command (".gray + + Object.keys( FCMD.verbs ).map( function(v, idx, ar) { + return (idx === ar.length - 1 ? 'or '.gray : '') + + v.toUpperCase().white.bold; + }).join(', ') + ")"; }; exitCode = ex.fluenterror; } @@ -78,7 +82,10 @@ function handleError( ex ) { var idx = msg.indexOf('Error: '); var trimmed = idx === -1 ? msg : msg.substring( idx + 7 ); - console.log( ('ERROR: ' + trimmed.toString()).red.bold ); + if( !ex.fluenterror || ex.fluenterror !== 4 && ex.fluenterror !== 3 ) + console.log( ('ERROR: ' + trimmed.toString()).red.bold ); + else + console.log( trimmed.toString() ); process.exit( exitCode ); }