1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-05-03 04:47:07 +01:00

CONVERT: Improve command consistency.

This commit is contained in:
hacksalot
2018-01-30 02:34:58 -05:00
parent 6b125ed907
commit c913de4bf7
19 changed files with 208 additions and 50 deletions

View File

@ -248,6 +248,11 @@ assembleError = ( ex ) ->
msg = M2C( @msgs.optionsFileNotFound.msg )
etype = 'error'
when HMSTATUS.unknownSchema
msg = M2C( @msgs.unknownSchema.msg[0] )
#msg += "\n" + M2C( @msgs.unknownSchema.msg[1], 'yellow' )
etype = 'error'
msg: msg # The error message to display
withStack: withStack # Whether to include the stack
quit: quit

View File

@ -262,7 +262,7 @@ execute = ( src, dst, opts, log ) ->
v = new HMR.verbs[ @name() ]()
# Initialize command-specific options
loadOptions.call( this, opts, this.parent.jsonArgs )
loadOptions.call this, opts, this.parent.jsonArgs
# Set up error/output handling
_opts.errHandler = v
@ -288,9 +288,15 @@ executeSuccess = (obj) ->
### Failure handler for verb invocations. Calls process.exit by default ###
executeFail = (err) ->
console.dir err
finalErrorCode = -1
if err
finalErrorCode = if err.fluenterror then err.fluenterror else err
if err.fluenterror
finalErrorCode = err.fluenterror
else if err.length
finalErrorCode = err[0].fluenterror
else
finalErrorCode = err
if _opts.debug
msgs = require('./msg').errors;
logMsg printf M2C( msgs.exiting.msg, 'cyan' ), finalErrorCode

View File

@ -115,3 +115,20 @@ errors:
- "\nMake sure the options file contains valid JSON."
optionsFileNotFound:
msg: "The specified options file is missing or inaccessible."
unknownSchema:
msg:
- "Unknown resume schema. Did you specify a valid FRESH or JRS resume?"
- |
At a minimum, a FRESH resume must include a "name" field and a "meta"
property.
"name": "John Doe",
"meta": {
"format": "FRESH@0.1.0"
}
JRS-format resumes must include a "basics" section with a "name":
"basics": {
"name": "John Doe"
}

View File

@ -131,7 +131,7 @@ module.exports = class OutputHandler
@log( chalk.cyan(output) )
when HME.beforeConvert
L( M2C( this.msgs.beforeConvert.msg, 'green' ),
L( M2C( this.msgs.beforeConvert.msg, if evt.error then 'red' else 'green' ),
evt.srcFile, evt.srcFmt, evt.dstFile, evt.dstFmt
);

View File

@ -6,14 +6,15 @@ Definition of the ResumeFactory class.
FS = require('fs')
HACKMYSTATUS = require('./status-codes')
HME = require('./event-codes')
ResumeConverter = require('fresh-jrs-converter')
chalk = require('chalk')
SyntaxErrorEx = require('../utils/syntax-error-ex')
_ = require('underscore')
require('string.prototype.startswith')
FS = require 'fs'
HMS = require './status-codes'
HME = require './event-codes'
ResumeConverter = require 'fresh-jrs-converter'
chalk = require 'chalk'
SyntaxErrorEx = require '../utils/syntax-error-ex'
_ = require 'underscore'
resumeDetect = require '../utils/resume-detector'
require 'string.prototype.startswith'
@ -62,20 +63,23 @@ ResumeFactory = module.exports =
# Determine the resume format: FRESH or JRS
json = info.json
isFRESH = json.meta && json.meta.format && json.meta.format.startsWith('FRESH@');
orgFormat = if isFRESH then 'fresh' else 'jrs'
orgFormat = resumeDetect json
if orgFormat == 'unk'
info.fluenterror = HMS.unknownSchema
return info
# Convert between formats if necessary
if toFormat and ( orgFormat != toFormat )
json = ResumeConverter[ 'to' + toFormat.toUpperCase() ]( json )
json = ResumeConverter[ 'to' + toFormat.toUpperCase() ] json
# Objectify the resume, that is, convert it from JSON to a FRESHResume
# or JRSResume object.
rez = null
if objectify
ResumeClass = require('../core/' + (toFormat || orgFormat) + '-resume');
rez = new ResumeClass().parseJSON( json, opts.inner );
rez.i().file = src;
reqLib = '../core/' + (toFormat || orgFormat) + '-resume'
ResumeClass = require reqLib
rez = new ResumeClass().parseJSON( json, opts.inner )
rez.i().file = src
file: src
json: info.json
@ -103,7 +107,7 @@ _parse = ( fileName, opts, eve ) ->
return ret
catch
# Can be ENOENT, EACCES, SyntaxError, etc.
fluenterror: if rawData then HACKMYSTATUS.parseError else HACKMYSTATUS.readError
fluenterror: if rawData then HMS.parseError else HMS.readError
inner: _error
raw: rawData
file: fileName

View File

@ -35,3 +35,4 @@ module.exports =
validateError: 26
invalidOptionsFile: 27
optionsFileNotFound: 28
unknownSchema: 29

View File

@ -0,0 +1,13 @@
###*
Definition of the ResumeDetector class.
@module utils/resume-detector
@license MIT. See LICENSE.md for details.
###
module.exports = ( rez ) ->
if rez.meta && rez.meta.format #&& rez.meta.format.substr(0, 5).toUpperCase() == 'FRESH'
'fresh'
else if rez.basics
'jrs'
else
'unk'

View File

@ -26,10 +26,14 @@ formats. ###
_convert = ( srcs, dst, opts ) ->
# Housekeeping...
# If no source resumes are specified, error out
if !srcs || !srcs.length
@err HMSTATUS.resumeNotFound, { quit: true }
return null
# If no destination resumes are specified, error out except for the special
# case of two resumes:
# hackmyresume CONVERT r1.json r2.json
if !dst || !dst.length
if srcs.length == 1
@err HMSTATUS.inputOutputParity, { quit: true }
@ -37,19 +41,30 @@ _convert = ( srcs, dst, opts ) ->
dst = dst || []; dst.push( srcs.pop() )
else
@err HMSTATUS.inputOutputParity, { quit: true }
# Different number of source and dest resumes? Error out.
if srcs && dst && srcs.length && dst.length && srcs.length != dst.length
@err HMSTATUS.inputOutputParity, { quit: true }
# Load source resumes
# If any errors have occurred this early, we're done.
if @hasError()
@reject @errorCode
return null
# Map each source resume to the converted destination resume
results = _.map srcs, ( src, idx ) ->
return { } if opts.assert and @hasError()
# Convert each resume in turn
r = _convertOne.call @, src, dst, idx
# Handle conversion errors
if r.fluenterror
r.quit = opts.assert
@err r.fluenterror, r
r
, @
if @hasError() and !opts.assert
@reject results
else if !@hasError()
@ -60,17 +75,31 @@ _convert = ( srcs, dst, opts ) ->
###* Private workhorse method. Convert a single resume. ###
_convertOne = (src, dst, idx) ->
# Load the resume
rinfo = ResumeFactory.loadOne src, format: null, objectify: true
# If a load error occurs, report it and move on to the next file (if any)
if rinfo.fluenterror
@stat HMEVENT.beforeConvert,
srcFile: src #rinfo.file
srcFmt: '???'
dstFile: dst[idx]
dstFmt: '???'
error: true
#@err rinfo.fluenterror, rinfo
return rinfo
rez = rinfo.rez
srcFmt = ''
if rez.meta && rez.meta.format #&& rez.meta.format.substr(0, 5).toUpperCase() == 'FRESH'
srcFmt = 'FRESH'
else if rez.basics
srcFmt = 'JRS'
else
rinfo.fluenterror = HMSTATUS.unknownSchema
return rinfo
s = rinfo.rez
srcFmt =
if ((s.basics && s.basics.imp) || s.imp).orgFormat == 'JRS'
then 'JRS' else 'FRESH'
targetFormat = if srcFmt == 'JRS' then 'FRESH' else 'JRS'
this.stat HMEVENT.beforeConvert,
@ -80,5 +109,5 @@ _convertOne = (src, dst, idx) ->
dstFmt: targetFormat
# Save it to the destination format
s.saveAs dst[idx], targetFormat
s
rez.saveAs dst[idx], targetFormat
rez

View File

@ -32,12 +32,20 @@ module.exports = class Verb
###* Invoke the command. ###
invoke: ->
# Sent the 'begin' notification for this verb
@stat HMEVENT.begin, cmd: @moniker
# Prepare command arguments
argsArray = Array::slice.call arguments
# Create a promise for this verb instance
that = @
@promise = new Promise (res, rej) ->
that.resolve = res; that.reject = rej
that.workhorse.apply that, argsArray; return
that.resolve = res
that.reject = rej
that.workhorse.apply that, argsArray
return