1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-10-06 07:25:13 +01:00
HackMyResume/src/core/resume-factory.js

118 lines
2.5 KiB
JavaScript
Raw Normal View History

/**
2015-12-31 08:34:41 +00:00
Definition of the ResumeFactory class.
@license MIT. See LICENSE.md for details.
@module resume-factory.js
*/
2015-12-31 08:34:41 +00:00
(function(){
2015-12-31 08:34:41 +00:00
require('string.prototype.startswith');
var FS = require('fs');
var ResumeConverter = require('./convert');
var chalk = require('chalk');
2015-12-31 08:34:41 +00:00
/**
A simple factory class for FRESH and JSON Resumes.
@class ResumeFactory
*/
2015-12-31 08:34:41 +00:00
var ResumeFactory = module.exports = {
/**
2015-12-31 08:34:41 +00:00
Load one or more resumes from disk.
*/
2015-12-31 08:34:41 +00:00
load: function ( sources, log, toFormat, objectify ) {
// Loop over all inputs, parsing each to JSON and then to a FRESHResume
// or JRSResume object.
var that = this;
return sources.map( function( src ) {
return that.loadOne( src, log, toFormat, objectify );
});
2015-12-31 08:34:41 +00:00
},
/**
Load a single resume from disk.
*/
loadOne: function( src, log, toFormat, objectify ) {
// Get the destination format. Can be 'fresh', 'jrs', or null/undefined.
toFormat && (toFormat = toFormat.toLowerCase().trim());
2015-12-31 08:34:41 +00:00
// Load and parse the resume JSON
var info = _parse( src, log, toFormat );
if( info.error ) return info;
var json = info.json;
// Determine the resume format: FRESH or JRS
var orgFormat = ( json.meta && json.meta.format &&
json.meta.format.startsWith('FRESH@') ) ?
'fresh' : 'jrs';
// Convert between formats if necessary
if( toFormat && (orgFormat !== toFormat) ) {
json = ResumeConverter[ 'to' + toFormat.toUpperCase() ]( json );
}
// Objectify the resume, that is, convert it from JSON to a FRESHResume
// or JRSResume object.
var rez;
if( objectify ) {
var ResumeClass = require('../core/' + (toFormat || orgFormat) + '-resume');
rez = new ResumeClass().parseJSON( json );
}
return {
file: src,
json: info.json,
rez: rez
};
}
};
2015-12-31 08:34:41 +00:00
function _parse( fileName, log, toFormat ) {
var rawData;
try {
// TODO: Core should not log
log( chalk.gray('Reading resume: ') + chalk.cyan.bold(fileName) );
2015-12-31 08:34:41 +00:00
2016-01-01 20:06:16 +00:00
// Read the file
2015-12-31 08:34:41 +00:00
rawData = FS.readFileSync( fileName, 'utf8' );
2016-01-01 20:06:16 +00:00
// Parse it to JSON
2015-12-31 08:34:41 +00:00
return {
json: JSON.parse( rawData )
};
}
2016-01-01 20:06:16 +00:00
catch( ex ) {
// If FS.readFileSync threw, pass the exception along.
if (!rawData)
throw ex;
// Otherwise if JSON.parse failed: probably a SyntaxError.
2015-12-31 08:34:41 +00:00
return {
error: ex,
raw: rawData
};
2016-01-01 20:06:16 +00:00
2015-12-31 08:34:41 +00:00
}
}
}());