1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-06-30 07:20:05 +01:00
HackMyResume/src/verbs/convert.js

90 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-12-31 08:34:41 +00:00
/**
Implementation of the 'convert' verb for HackMyResume.
2016-01-18 02:46:58 +00:00
@module verbs/convert
2015-12-31 08:34:41 +00:00
@license MIT. See LICENSE.md for details.
*/
2016-01-01 22:30:57 +00:00
2015-12-21 07:56:02 +00:00
(function(){
2016-01-01 22:30:57 +00:00
var ResumeFactory = require('../core/resume-factory')
2016-01-03 07:22:26 +00:00
, chalk = require('chalk')
2016-01-11 00:08:29 +00:00
, Verb = require('../verbs/verb')
2016-01-18 05:34:57 +00:00
, HMSTATUS = require('../core/status-codes')
2016-01-09 20:49:08 +00:00
, _ = require('underscore')
2016-01-18 05:34:57 +00:00
, HMEVENT = require('../core/event-codes');
2016-01-01 22:30:57 +00:00
var ConvertVerb = module.exports = Verb.extend({
2016-01-09 20:49:08 +00:00
init: function() {
this._super('convert');
},
invoke: function() {
2016-01-18 05:34:57 +00:00
this.stat( HMEVENT.begin, { cmd: 'convert' });
convert.apply( this, arguments );
2016-01-18 05:34:57 +00:00
this.stat( HMEVENT.end );
}
});
2015-12-21 07:56:02 +00:00
/**
Convert between FRESH and JRS formats.
*/
function convert( srcs, dst, opts ) {
2016-01-01 22:30:57 +00:00
// Housekeeping
2016-01-02 05:15:46 +00:00
if( !srcs || !srcs.length ) { throw { fluenterror: 6 }; }
2015-12-21 07:56:02 +00:00
if( !dst || !dst.length ) {
2016-01-03 07:22:26 +00:00
if( srcs.length === 1 ) {
2016-01-18 05:34:57 +00:00
throw { fluenterror: HMSTATUS.inputOutputParity };
2016-01-03 07:22:26 +00:00
}
else if( srcs.length === 2 ) {
dst = dst || []; dst.push( srcs.pop() );
}
else {
2016-01-18 05:34:57 +00:00
throw { fluenterror: HMSTATUS.inputOutputParity };
2016-01-03 07:22:26 +00:00
}
}
if(srcs && dst && srcs.length && dst.length && srcs.length !== dst.length){
2016-01-18 05:34:57 +00:00
throw { fluenterror: HMSTATUS.inputOutputParity };
2015-12-21 07:56:02 +00:00
}
2016-01-01 22:30:57 +00:00
// Load source resumes
2016-01-09 20:49:08 +00:00
_.each(srcs, function( src, idx ) {
2016-01-01 22:30:57 +00:00
2016-01-02 05:15:46 +00:00
// Load the resume
var rinfo = ResumeFactory.loadOne( src, {
2016-01-19 00:21:25 +00:00
format: null, objectify: true, throw: false
2016-01-02 05:15:46 +00:00
});
2016-01-01 22:30:57 +00:00
2016-01-19 00:21:25 +00:00
// If a load error occurs, report it and move on to the next file (if any)
if( rinfo.fluenterror ) {
this.err( rinfo.fluenterror, rinfo );
return;
}
2016-01-02 05:15:46 +00:00
var s = rinfo.rez
2016-01-01 22:30:57 +00:00
, srcFmt = ((s.basics && s.basics.imp) || s.imp).orgFormat === 'JRS' ?
2016-01-02 05:15:46 +00:00
'JRS' : 'FRESH'
, targetFormat = srcFmt === 'JRS' ? 'FRESH' : 'JRS';
2016-01-01 22:30:57 +00:00
2016-01-18 05:34:57 +00:00
this.stat(HMEVENT.beforeConvert, { srcFile: rinfo.file, srcFmt: srcFmt, dstFile: dst[idx], dstFmt: targetFormat });
2016-01-01 22:30:57 +00:00
2016-01-02 05:15:46 +00:00
// Save it to the destination format
2016-01-01 22:30:57 +00:00
s.saveAs( dst[idx], targetFormat );
2016-01-02 05:15:46 +00:00
2016-01-09 20:49:08 +00:00
}, this);
2016-01-02 05:15:46 +00:00
}
2015-12-21 07:56:02 +00:00
2016-01-01 22:30:57 +00:00
2015-12-21 07:56:02 +00:00
}());