HackMyResume/dist/core/resume-factory.js

133 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Definition of the ResumeFactory class.
@license MIT. See LICENSE.md for details.
@module core/resume-factory
*/
/**
A simple factory class for FRESH and JSON Resumes.
@class ResumeFactory
*/
2018-01-30 07:34:58 +00:00
var FS, HME, HMS, ResumeConverter, ResumeFactory, SyntaxErrorEx, _, _parse, chalk, resumeDetect;
2016-01-27 10:29:26 +00:00
FS = require('fs');
2018-01-30 07:34:58 +00:00
HMS = require('./status-codes');
2016-01-27 10:29:26 +00:00
HME = require('./event-codes');
ResumeConverter = require('fresh-jrs-converter');
chalk = require('chalk');
SyntaxErrorEx = require('../utils/syntax-error-ex');
_ = require('underscore');
2018-01-30 07:34:58 +00:00
resumeDetect = require('../utils/resume-detector');
2016-01-27 10:29:26 +00:00
require('string.prototype.startswith');
ResumeFactory = module.exports = {
/**
Load one or more resumes from disk.
2018-02-12 05:05:29 +00:00
2016-01-27 10:29:26 +00:00
@param {Object} opts An options object with settings for the factory as well
as passthrough settings for FRESHResume or JRSResume. Structure:
2018-02-12 05:05:29 +00:00
2016-01-27 10:29:26 +00:00
{
format: 'FRESH', // Format to open as. ('FRESH', 'JRS', null)
objectify: true, // FRESH/JRSResume or raw JSON?
inner: { // Passthru options for FRESH/JRSResume
sort: false
}
}
2018-02-12 05:05:29 +00:00
*/
2016-01-27 10:29:26 +00:00
load: function(sources, opts, emitter) {
return sources.map(function(src) {
return this.loadOne(src, opts, emitter);
}, this);
},
2018-02-12 05:05:29 +00:00
/** Load a single resume from disk. */
2016-01-27 10:29:26 +00:00
loadOne: function(src, opts, emitter) {
2018-01-31 21:17:46 +00:00
var ResumeClass, info, json, orgFormat, reqLib, rez, toFormat;
2018-02-12 05:05:29 +00:00
toFormat = opts.format; // Can be null
// Get the destination format. Can be 'fresh', 'jrs', or null/undefined.
2016-01-27 10:29:26 +00:00
toFormat && (toFormat = toFormat.toLowerCase().trim());
2018-02-12 05:05:29 +00:00
// Load and parse the resume JSON
2016-01-27 10:29:26 +00:00
info = _parse(src, opts, emitter);
if (info.fluenterror) {
return info;
}
2018-02-12 05:05:29 +00:00
// Determine the resume format: FRESH or JRS
2016-01-27 10:29:26 +00:00
json = info.json;
2018-01-30 07:34:58 +00:00
orgFormat = resumeDetect(json);
if (orgFormat === 'unk') {
info.fluenterror = HMS.unknownSchema;
return info;
}
2018-02-12 05:05:29 +00:00
// Convert between formats if necessary
2016-01-27 10:29:26 +00:00
if (toFormat && (orgFormat !== toFormat)) {
json = ResumeConverter['to' + toFormat.toUpperCase()](json);
}
2018-02-12 05:05:29 +00:00
// Objectify the resume, that is, convert it from JSON to a FRESHResume
// or JRSResume object.
2016-01-27 10:29:26 +00:00
rez = null;
2018-01-31 21:17:46 +00:00
if (opts.objectify) {
2018-01-30 07:34:58 +00:00
reqLib = '../core/' + (toFormat || orgFormat) + '-resume';
ResumeClass = require(reqLib);
2016-01-27 10:29:26 +00:00
rez = new ResumeClass().parseJSON(json, opts.inner);
rez.i().file = src;
}
return {
file: src,
json: info.json,
rez: rez
};
}
};
_parse = function(fileName, opts, eve) {
2018-02-12 05:05:29 +00:00
var err, orgFormat, rawData, ret;
2016-01-27 10:29:26 +00:00
rawData = null;
try {
2018-02-12 05:05:29 +00:00
// Read the file
2016-01-27 10:29:26 +00:00
eve && eve.stat(HME.beforeRead, {
file: fileName
});
rawData = FS.readFileSync(fileName, 'utf8');
eve && eve.stat(HME.afterRead, {
file: fileName,
data: rawData
});
eve && eve.stat(HME.beforeParse, {
data: rawData
});
ret = {
json: JSON.parse(rawData)
};
orgFormat = ret.json.meta && ret.json.meta.format && ret.json.meta.format.startsWith('FRESH@') ? 'fresh' : 'jrs';
eve && eve.stat(HME.afterParse, {
file: fileName,
data: ret.json,
fmt: orgFormat
});
return ret;
2018-02-12 05:05:29 +00:00
} catch (error) {
err = error;
2016-02-02 02:14:36 +00:00
return {
2018-02-12 05:05:29 +00:00
// Can be ENOENT, EACCES, SyntaxError, etc.
2018-01-30 07:34:58 +00:00
fluenterror: rawData ? HMS.parseError : HMS.readError,
2018-02-12 05:05:29 +00:00
inner: err,
2016-01-27 10:29:26 +00:00
raw: rawData,
2016-02-02 02:14:36 +00:00
file: fileName
2016-01-27 10:29:26 +00:00
};
}
};
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=resume-factory.js.map