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

134 lines
3.0 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');
var SyntaxErrorEx = require('../utils/syntax-error-ex');
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.
*/
load: function ( sources, opts ) {
2015-12-31 08:34:41 +00:00
// 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, opts );
});
2015-12-31 08:34:41 +00:00
},
/**
Load a single resume from disk.
*/
loadOne: function( src, opts ) {
var log = opts.log;
var toFormat = opts.format;
var objectify = opts.objectify;
2015-12-31 08:34:41 +00:00
// 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, opts );
2015-12-31 08:34:41 +00:00
if( info.error ) return info;
// Determine the resume format: FRESH or JRS
var json = info.json;
2015-12-31 08:34:41 +00:00
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 );
2016-01-06 03:02:11 +00:00
rez.i().file = src;
2015-12-31 08:34:41 +00:00
}
return {
file: src,
json: info.json,
rez: rez
};
}
};
2015-12-31 08:34:41 +00:00
function _parse( fileName, opts ) {
2015-12-31 08:34:41 +00:00
var rawData;
try {
// TODO: Core should not log
2016-01-02 05:15:46 +00:00
opts.log( chalk.cyan('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 ) {
// JSON.parse failed due to invalid JSON
2016-01-02 05:15:46 +00:00
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 + '.' +
chalk.red(' Unable to validate.')));
opts.log( chalk.red.bold('INTERNAL: ' + ex) );
ex.handled = true;
}
2016-01-01 20:06:16 +00:00
2016-01-07 21:13:09 +00:00
// FS.readFileSync failed
if( !rawData || opts.throw ) throw ex;
return {
2015-12-31 08:34:41 +00:00
error: ex,
2016-01-02 05:15:46 +00:00
raw: rawData,
file: fileName
2015-12-31 08:34:41 +00:00
};
2016-01-01 20:06:16 +00:00
2015-12-31 08:34:41 +00:00
}
}
}());