1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-07-05 01:20:06 +01:00

Refactor.

This commit is contained in:
devlinjd 2015-11-21 07:59:30 -05:00
parent 9fbab27d73
commit 317de75a5b
2 changed files with 46 additions and 25 deletions

View File

@ -37,10 +37,7 @@ module.exports = function () {
// Load input resumes... // Load input resumes...
if(!src || !src.length) { throw { fluenterror: 3 }; } if(!src || !src.length) { throw { fluenterror: 3 }; }
var sheets = src.map( function( res ) { var sheets = loadSourceResumes( src );
_log( 'Reading '.gray + 'SOURCE' + ' resume: '.gray + res.cyan.bold );
return (new FLUENT.FRESHResume()).open( res );
});
// Merge input resumes... // Merge input resumes...
var msg = ''; var msg = '';
@ -136,19 +133,30 @@ module.exports = function () {
jars: require('./core/resume.json') jars: require('./core/resume.json')
}; };
var sheets = src.map( function( file ) { // Load input resumes...
var sheets = loadSourceResumes(src, function( res ) {
var textData = '';
try { try {
textData = FS.readFileSync( file, 'utf8' ); return {
var rez = JSON.parse( textData ); file: res,
raw: FS.readFileSync( res, 'utf8' )
};
} }
catch( ex ) { 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) { if (ex instanceof SyntaxError) {
// Invalid JSON // 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 ); _log( (' INTERNAL: ' + ex).red );
} }
else { else {
@ -159,8 +167,8 @@ module.exports = function () {
} }
var fmt = rez.meta && rez.meta.format === 'FRESH@0.1.0' ? 'fresh':'jars'; var fmt = rez.meta && rez.meta.format === 'FRESH@0.1.0' ? 'fresh':'jars';
process.stdout.write( 'Validating ' + file.cyan.bold + ' against ' + process.stdout.write( 'Validating '.gray + rep.file + ' against '.gray +
fmt.replace('jars','JSON Resume').toUpperCase() + ' schema: ' ); fmt.replace('jars','JSON Resume').toUpperCase() + ' schema: '.gray );
var validate = validator( schemas[ fmt ], { // Note [1] var validate = validator( schemas[ fmt ], { // Note [1]
formats: { date: /^\d{4}(?:-(?:0[0-9]{1}|1[0-2]{1})(?:-[0-9]{2})?)?$/ } 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 ) { function convert( src, dst, opts, logger ) {
_log = logger || console.log; _log = logger || console.log;
if( !src || src.length !== 1 ) { throw { fluenterror: 3 }; } if( !src || src.length !== 1 ) { throw { fluenterror: 3 }; }
_log( 'Reading JSON resume: ' + src[0] ); var sheet = loadSourceResumes( src )[ 0 ];
var sheet = (new FLUENT.FRESHResume()).open( src[ 0 ] );
var sourceFormat = sheet.imp.orgFormat === 'JRS' ? 'JRS' : 'FRESH'; var sourceFormat = sheet.imp.orgFormat === 'JRS' ? 'JRS' : 'FRESH';
var targetFormat = sourceFormat === 'JRS' ? 'FRESH' : 'JRS'; var targetFormat = sourceFormat === 'JRS' ? 'FRESH' : 'JRS';
_log( 'Converting ' + src[0] + ' (' + sourceFormat + ') to ' + dst[0] + _log( 'Converting '.gray + src[0] + (' (' + sourceFormat + ') to ').gray + dst[0] +
' (' + targetFormat + ').' ); (' (' + targetFormat + ').').gray );
sheet.saveAs( dst[0], targetFormat ); 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. Supported resume formats.
*/ */

View File

@ -8,7 +8,8 @@ Command-line interface (CLI) for FluentCV:CLI.
var ARGS = require( 'minimist' ) var ARGS = require( 'minimist' )
, FCMD = require( './fluentcmd') , FCMD = require( './fluentcmd')
, PKG = require('../package.json') , PKG = require('../package.json')
, opts = { }; , opts = { }
, title = ('*** FluentCV v' + PKG.version + ' ***').white.bold;
@ -23,9 +24,7 @@ catch( ex ) {
function main() { function main() {
// Setup if( process.argv.length <= 2 ) { throw { fluenterror: 4 }; }
var title = ('*** FluentCV v' + PKG.version + ' ***').bold;
if( process.argv.length <= 2 ) { logMsg(title); throw { fluenterror: 4 }; }
var a = ARGS( process.argv.slice(2) ); var a = ARGS( process.argv.slice(2) );
opts = getOpts( a ); opts = getOpts( a );
logMsg( title ); logMsg( title );
@ -33,7 +32,8 @@ function main() {
// Get the action to be performed // Get the action to be performed
var verb = a._[0].toLowerCase().trim(); var verb = a._[0].toLowerCase().trim();
if( !FCMD.verbs[ verb ] ) { if( !FCMD.verbs[ verb ] ) {
throw 'Invalid command: "' + verb + '"'; logMsg('Invalid command: "'.yellow + verb.yellow.bold + '"'.yellow);
return;
} }
// Preload our params array // Preload our params array
@ -66,8 +66,12 @@ function handleError( ex ) {
switch( ex.fluenterror ) { // TODO: Remove magic numbers switch( ex.fluenterror ) { // TODO: Remove magic numbers
case 1: msg = "The specified theme couldn't be found: " + ex.data; break; 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 2: msg = "Couldn't copy CSS file to destination folder"; break;
case 3: msg = "Please specify a valid JSON resume file."; break; case 3: msg = "Please specify a valid SOURCE resume in FRESH or JSON Resume format.".gray; break;
case 4: msg = "Please specify a valid command (GENERATE, VALIDATE, or CONVERT)." 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; exitCode = ex.fluenterror;
} }
@ -78,7 +82,10 @@ function handleError( ex ) {
var idx = msg.indexOf('Error: '); var idx = msg.indexOf('Error: ');
var trimmed = idx === -1 ? msg : msg.substring( idx + 7 ); 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 ); process.exit( exitCode );
} }