mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2025-05-15 10:07:07 +01:00
Interim changes supporting v1.3.0.
This commit is contained in:
@ -1,43 +1,107 @@
|
||||
/**
|
||||
Core resume-loading logic for HackMyResume.
|
||||
Definition of the ResumeFactory class.
|
||||
@license MIT. See LICENSE.md for details.
|
||||
@module resume-factory.js
|
||||
*/
|
||||
|
||||
|
||||
|
||||
(function(){
|
||||
|
||||
|
||||
|
||||
require('string.prototype.startswith');
|
||||
var FS = require('fs');
|
||||
var ResumeConverter = require('./convert');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
A simple factory class for FRESH and JSON Resumes.
|
||||
@class ResumeFactory
|
||||
*/
|
||||
module.exports = {
|
||||
var ResumeFactory = module.exports = {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Load one or more resumes in a specific source format.
|
||||
Load one or more resumes from disk.
|
||||
*/
|
||||
load: function ( src, log, fn, toFormat ) {
|
||||
|
||||
toFormat = toFormat && (toFormat.toLowerCase().trim()) || 'fresh';
|
||||
var ResumeClass = require('../core/' + toFormat + '-resume');
|
||||
|
||||
return src.map( function( res ) {
|
||||
var rezJson = JSON.parse( FS.readFileSync( res ) );
|
||||
var orgFormat = ( rezJson.meta && rezJson.meta.format &&
|
||||
rezJson.meta.format.startsWith('FRESH@') ) ?
|
||||
'fresh' : 'jrs';
|
||||
if(orgFormat !== toFormat) {
|
||||
rezJson = ResumeConverter[ 'to' + toFormat.toUpperCase() ]( rezJson );
|
||||
}
|
||||
// TODO: Core should not log
|
||||
log( 'Reading '.info + orgFormat.toUpperCase().infoBold + ' resume: '.info + res.cyan.bold );
|
||||
return (fn && fn(res)) || (new ResumeClass()).parseJSON( rezJson );
|
||||
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 );
|
||||
});
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
/**
|
||||
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());
|
||||
|
||||
// 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
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
function _parse( fileName, log, toFormat ) {
|
||||
var rawData;
|
||||
try {
|
||||
|
||||
// TODO: Core should not log
|
||||
log( 'Reading '.info + /*orgFormat.toUpperCase().infoBold +*/
|
||||
'resume: '.info + fileName.cyan.bold );
|
||||
|
||||
rawData = FS.readFileSync( fileName, 'utf8' );
|
||||
return {
|
||||
json: JSON.parse( rawData )
|
||||
};
|
||||
|
||||
}
|
||||
catch(ex) {
|
||||
return {
|
||||
error: ex,
|
||||
raw: rawData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}());
|
||||
|
Reference in New Issue
Block a user