HackMyResume/dist/verbs/convert.js

167 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Implementation of the 'convert' verb for HackMyResume.
@module verbs/convert
@license MIT. See LICENSE.md for details.
*/
/** Private workhorse method. Convert 0..N resumes between FRESH and JRS
formats. */
/** Private workhorse method. Convert a single resume. */
var ConvertVerb, HMEVENT, HMSTATUS, ResumeFactory, Verb, _, _convert, _convertOne, chalk;
2016-01-27 10:29:26 +00:00
ResumeFactory = require('../core/resume-factory');
chalk = require('chalk');
Verb = require('../verbs/verb');
HMSTATUS = require('../core/status-codes');
_ = require('underscore');
HMEVENT = require('../core/event-codes');
2018-02-12 05:05:29 +00:00
module.exports = ConvertVerb = class ConvertVerb extends Verb {
constructor() {
super('convert', _convert);
2016-01-27 10:29:26 +00:00
}
2018-02-12 05:05:29 +00:00
};
2016-01-27 10:29:26 +00:00
2016-02-02 02:14:36 +00:00
_convert = function(srcs, dst, opts) {
2018-02-10 06:10:20 +00:00
var fmtUp, results, targetVer;
2016-01-27 10:29:26 +00:00
if (!srcs || !srcs.length) {
2016-02-02 02:14:36 +00:00
this.err(HMSTATUS.resumeNotFound, {
2016-01-27 10:29:26 +00:00
quit: true
2016-02-02 02:14:36 +00:00
});
return null;
2016-01-27 10:29:26 +00:00
}
if (!dst || !dst.length) {
if (srcs.length === 1) {
2016-02-02 02:14:36 +00:00
this.err(HMSTATUS.inputOutputParity, {
2016-01-27 10:29:26 +00:00
quit: true
2016-02-02 02:14:36 +00:00
});
2016-01-27 10:29:26 +00:00
} else if (srcs.length === 2) {
dst = dst || [];
dst.push(srcs.pop());
} else {
2016-02-02 02:14:36 +00:00
this.err(HMSTATUS.inputOutputParity, {
2016-01-27 10:29:26 +00:00
quit: true
2016-02-02 02:14:36 +00:00
});
2016-01-27 10:29:26 +00:00
}
}
2018-02-12 05:05:29 +00:00
// Different number of source and dest resumes? Error out.
2016-01-27 10:29:26 +00:00
if (srcs && dst && srcs.length && dst.length && srcs.length !== dst.length) {
2016-02-02 02:14:36 +00:00
this.err(HMSTATUS.inputOutputParity, {
quit: true
2016-01-27 10:29:26 +00:00
});
2016-02-02 02:14:36 +00:00
}
2018-02-12 05:05:29 +00:00
// Validate the destination format (if specified)
targetVer = null;
if (opts.format) {
fmtUp = opts.format.trim().toUpperCase();
2018-02-10 06:10:20 +00:00
if (!_.contains(['FRESH', 'FRESCA', 'JRS', 'JRS@1', 'JRS@edge'], fmtUp)) {
this.err(HMSTATUS.invalidSchemaVersion, {
data: opts.format.trim(),
quit: true
});
}
}
2018-02-12 05:05:29 +00:00
// freshVerRegex = require '../utils/fresh-version-regex'
// matches = fmtUp.match freshVerRegex()
// # null
// # [ 'JRS@1.0', 'JRS', '1.0', index: 0, input: 'FRESH' ]
// # [ 'FRESH', 'FRESH', undefined, index: 0, input: 'FRESH' ]
// if not matches
// @err HMSTATUS.invalidSchemaVersion, data: opts.format.trim(), quit: true
// targetSchema = matches[1]
// targetVer = matches[2] || '1'
// If any errors have occurred this early, we're done.
2018-01-30 07:34:58 +00:00
if (this.hasError()) {
this.reject(this.errorCode);
return null;
}
2018-02-12 05:05:29 +00:00
// Map each source resume to the converted destination resume
2016-02-02 02:14:36 +00:00
results = _.map(srcs, function(src, idx) {
var r;
2018-02-12 05:05:29 +00:00
// Convert each resume in turn
2018-02-10 06:10:20 +00:00
r = _convertOne.call(this, src, dst, idx, fmtUp);
2018-02-12 05:05:29 +00:00
// Handle conversion errors
2016-02-02 02:14:36 +00:00
if (r.fluenterror) {
r.quit = opts.assert;
this.err(r.fluenterror, r);
}
return r;
2016-01-27 10:29:26 +00:00
}, this);
2016-02-02 02:14:36 +00:00
if (this.hasError() && !opts.assert) {
this.reject(results);
} else if (!this.hasError()) {
this.resolve(results);
}
return results;
};
2018-02-10 06:10:20 +00:00
_convertOne = function(src, dst, idx, targetSchema) {
var err, rez, rinfo, srcFmt, targetFormat;
2018-02-12 05:05:29 +00:00
// Load the resume
2016-02-02 02:14:36 +00:00
rinfo = ResumeFactory.loadOne(src, {
format: null,
objectify: true,
inner: {
privatize: false
}
2016-02-02 02:14:36 +00:00
});
2018-02-12 05:05:29 +00:00
// If a load error occurs, report it and move on to the next file (if any)
2016-02-02 02:14:36 +00:00
if (rinfo.fluenterror) {
2018-01-30 07:34:58 +00:00
this.stat(HMEVENT.beforeConvert, {
2018-02-12 05:05:29 +00:00
srcFile: src, //rinfo.file
2018-01-30 07:34:58 +00:00
srcFmt: '???',
dstFile: dst[idx],
dstFmt: '???',
error: true
});
2018-02-12 05:05:29 +00:00
//@err rinfo.fluenterror, rinfo
2018-01-30 07:34:58 +00:00
return rinfo;
}
2018-02-12 05:05:29 +00:00
// Determine the resume's SOURCE format
// TODO: replace with detector component
2018-01-30 07:34:58 +00:00
rez = rinfo.rez;
srcFmt = '';
2018-02-12 05:05:29 +00:00
if (rez.meta && rez.meta.format) { //&& rez.meta.format.substr(0, 5).toUpperCase() == 'FRESH'
2018-01-30 07:34:58 +00:00
srcFmt = 'FRESH';
} else if (rez.basics) {
srcFmt = 'JRS';
} else {
rinfo.fluenterror = HMSTATUS.unknownSchema;
2016-02-02 02:14:36 +00:00
return rinfo;
}
2018-02-12 05:05:29 +00:00
// Determine the TARGET format for the conversion
targetFormat = targetSchema || (srcFmt === 'JRS' ? 'FRESH' : 'JRS');
2018-02-12 05:05:29 +00:00
// Fire the beforeConvert event
2016-02-02 02:14:36 +00:00
this.stat(HMEVENT.beforeConvert, {
srcFile: rinfo.file,
srcFmt: srcFmt,
dstFile: dst[idx],
dstFmt: targetFormat
});
try {
2018-02-12 05:05:29 +00:00
// Save it to the destination format
2018-02-10 06:10:20 +00:00
rez.saveAs(dst[idx], targetFormat);
2018-02-12 05:05:29 +00:00
} catch (error) {
err = error;
if (err.badVer) {
return {
fluenterror: HMSTATUS.invalidSchemaVersion,
quit: true,
data: err.badVer
};
}
}
2018-01-30 07:34:58 +00:00
return rez;
2016-01-27 10:29:26 +00:00
};
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=convert.js.map