1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-09-28 20:19:12 +01:00

Refactor verb invocations to base.

This commit is contained in:
hacksalot 2016-01-31 08:37:12 -05:00
parent f1ba7765ee
commit 90765bf90b
14 changed files with 81 additions and 129 deletions

View File

@ -26,14 +26,7 @@ Implementation of the 'analyze' verb for HackMyResume.
AnalyzeVerb = module.exports = Verb.extend({ AnalyzeVerb = module.exports = Verb.extend({
init: function() { init: function() {
return this._super('analyze'); return this._super('analyze', analyze);
},
invoke: function() {
this.stat(HMEVENT.begin, {
cmd: 'analyze'
});
analyze.apply(this, arguments);
return this.stat(HMEVENT.end);
} }
}); });

39
dist/verbs/build.js vendored
View File

@ -74,18 +74,7 @@ Implementation of the 'build' verb for HackMyResume.
/** Create a new build verb. */ /** Create a new build verb. */
init: function() { init: function() {
return this._super('build'); return this._super('build', build);
},
/** Invoke the Build command. */
invoke: function() {
var ret;
this.stat(HMEVENT.begin, {
cmd: 'build'
});
ret = build.apply(this, arguments);
this.stat(HMEVENT.end);
return ret;
} }
}); });
@ -95,16 +84,16 @@ Implementation of the 'build' verb for HackMyResume.
theme file, generate 0..N resumes in the desired formats. theme file, generate 0..N resumes in the desired formats.
@param src Path to the source JSON resume file: "rez/resume.json". @param src Path to the source JSON resume file: "rez/resume.json".
@param dst An array of paths to the target resume file(s). @param dst An array of paths to the target resume file(s).
@param theme Friendly name of the resume theme. Defaults to "modern". @param opts Generation options.
@param logger Optional logging override.
*/ */
build = function(src, dst, opts) { build = function(src, dst, opts) {
var ex, inv, isFRESH, mixed, newEx, orgFormat, rez, sheetObjects, sheets, tFolder, targets, theme, toFormat; var ex, inv, isFRESH, mixed, newEx, orgFormat, problemSheets, rez, sheetObjects, sheets, tFolder, targets, theme, toFormat;
if (!src || !src.length) { if (!src || !src.length) {
this.err(HMSTATUS.resumeNotFound, { this.err(HMSTATUS.resumeNotFound, {
quit: true quit: true
}); });
return null;
} }
prep(src, dst, opts); prep(src, dst, opts);
sheetObjects = ResumeFactory.load(src, { sheetObjects = ResumeFactory.load(src, {
@ -115,9 +104,12 @@ Implementation of the 'build' verb for HackMyResume.
sort: _opts.sort sort: _opts.sort
} }
}, this); }, this);
if (!sheetObjects || _.some(sheetObjects, function(so) { problemSheets = _.filter(sheetObjects, function(so) {
return so.fluenterror; return so.fluenterror;
})) { });
if (problemSheets && problemSheets.length) {
problemSheets[0].quit = true;
this.err(problemSheets[0].fluenterror, problemSheets[0]);
return null; return null;
} }
sheets = sheetObjects.map(function(r) { sheets = sheetObjects.map(function(r) {
@ -130,12 +122,14 @@ Implementation of the 'build' verb for HackMyResume.
try { try {
tFolder = verifyTheme.call(this, _opts.theme); tFolder = verifyTheme.call(this, _opts.theme);
theme = _opts.themeObj = loadTheme(tFolder); theme = _opts.themeObj = loadTheme(tFolder);
addFreebieFormats(theme);
} catch (_error) { } catch (_error) {
ex = _error; ex = _error;
newEx = { newEx = {
fluenterror: HMSTATUS.themeLoad, fluenterror: HMSTATUS.themeLoad,
inner: ex, inner: ex,
attempted: _opts.theme attempted: _opts.theme,
quit: true
}; };
this.err(HMSTATUS.themeLoad, newEx); this.err(HMSTATUS.themeLoad, newEx);
return null; return null;
@ -147,8 +141,10 @@ Implementation of the 'build' verb for HackMyResume.
if (inv && inv.length) { if (inv && inv.length) {
this.err(HMSTATUS.invalidFormat, { this.err(HMSTATUS.invalidFormat, {
data: inv, data: inv,
theme: theme theme: theme,
quit: true
}); });
return null;
} }
rez = null; rez = null;
if (sheets.length > 1) { if (sheets.length > 1) {
@ -186,7 +182,6 @@ Implementation of the 'build' verb for HackMyResume.
fmt: toFormat fmt: toFormat
}); });
} }
addFreebieFormats(theme);
this.stat(HMEVENT.applyTheme, { this.stat(HMEVENT.applyTheme, {
r: rez, r: rez,
theme: theme theme: theme
@ -284,9 +279,7 @@ Implementation of the 'build' verb for HackMyResume.
}; };
/** /** Ensure that user-specified outputs/targets are valid. */
Ensure that user-specified outputs/targets are valid.
*/
verifyOutputs = function(targets, theme) { verifyOutputs = function(targets, theme) {
this.stat(HMEVENT.verifyOutputs, { this.stat(HMEVENT.verifyOutputs, {

View File

@ -22,14 +22,7 @@ Implementation of the 'convert' verb for HackMyResume.
ConvertVerb = module.exports = Verb.extend({ ConvertVerb = module.exports = Verb.extend({
init: function() { init: function() {
return this._super('convert'); return this._super('convert', convert);
},
invoke: function() {
this.stat(HMEVENT.begin, {
cmd: 'convert'
});
convert.apply(this, arguments);
return this.stat(HMEVENT.end);
} }
}); });

11
dist/verbs/create.js vendored
View File

@ -24,16 +24,7 @@ Implementation of the 'create' verb for HackMyResume.
CreateVerb = module.exports = Verb.extend({ CreateVerb = module.exports = Verb.extend({
init: function() { init: function() {
return this._super('new'); return this._super('new', create);
},
invoke: function() {
this.stat(HMEVENT.begin, {
cmd: 'create'
});
create.apply(this, arguments);
this.stat(HMEVENT.begin, {
cmd: 'convert'
});
} }
}); });

9
dist/verbs/peek.js vendored
View File

@ -22,14 +22,7 @@ Implementation of the 'peek' verb for HackMyResume.
PeekVerb = module.exports = Verb.extend({ PeekVerb = module.exports = Verb.extend({
init: function() { init: function() {
return this._super('peek'); return this._super('peek', peek);
},
invoke: function() {
this.stat(HMEVENT.begin, {
cmd: 'peek'
});
peek.apply(this, arguments);
return this.stat(HMEVENT.end);
} }
}); });

View File

@ -31,16 +31,7 @@ Implementation of the 'validate' verb for HackMyResume.
ValidateVerb = module.exports = Verb.extend({ ValidateVerb = module.exports = Verb.extend({
init: function() { init: function() {
return this._super('validate'); return this._super('validate', validate);
},
invoke: function() {
var ret;
this.stat(HMEVENT.begin, {
cmd: 'validate'
});
ret = validate.apply(this, arguments);
this.stat(HMEVENT.end);
return ret;
} }
}); });

18
dist/verbs/verb.js vendored
View File

@ -6,12 +6,14 @@ Definition of the Verb class.
*/ */
(function() { (function() {
var Class, EVENTS, Verb; var Class, EVENTS, HMEVENT, Verb;
Class = require('../utils/class'); Class = require('../utils/class');
EVENTS = require('events'); EVENTS = require('events');
HMEVENT = require('../core/event-codes');
/** /**
An instantiation of a HackMyResume command. An instantiation of a HackMyResume command.
@ -21,9 +23,21 @@ Definition of the Verb class.
Verb = module.exports = Class.extend({ Verb = module.exports = Class.extend({
/** Constructor. Automatically called at creation. */ /** Constructor. Automatically called at creation. */
init: function(moniker) { init: function(moniker, workhorse) {
this.moniker = moniker; this.moniker = moniker;
this.emitter = new EVENTS.EventEmitter(); this.emitter = new EVENTS.EventEmitter();
this.workhorse = workhorse;
},
/** Invoke the command. */
invoke: function() {
var ret;
this.stat(HMEVENT.begin, {
cmd: this.moniker
});
ret = this.workhorse.apply(this, arguments);
this.stat(HMEVENT.end);
return ret;
}, },
/** Forward subscriptions to the event emitter. */ /** Forward subscriptions to the event emitter. */

View File

@ -19,12 +19,7 @@ chalk = require('chalk')
AnalyzeVerb = module.exports = Verb.extend AnalyzeVerb = module.exports = Verb.extend
init: -> @._super 'analyze' init: -> @_super 'analyze', analyze
invoke: ->
@.stat HMEVENT.begin, { cmd: 'analyze' }
analyze.apply @, arguments
@.stat HMEVENT.end

View File

@ -42,14 +42,7 @@ loadTheme = null
BuildVerb = module.exports = Verb.extend BuildVerb = module.exports = Verb.extend
###* Create a new build verb. ### ###* Create a new build verb. ###
init: () -> @._super 'build' init: () -> @_super 'build', build
###* Invoke the Build command. ###
invoke: ->
@stat HMEVENT.begin, { cmd: 'build' }
ret = build.apply @, arguments
@stat HMEVENT.end
return ret
@ -58,13 +51,13 @@ Given a source resume in FRESH or JRS format, a destination resume path, and a
theme file, generate 0..N resumes in the desired formats. theme file, generate 0..N resumes in the desired formats.
@param src Path to the source JSON resume file: "rez/resume.json". @param src Path to the source JSON resume file: "rez/resume.json".
@param dst An array of paths to the target resume file(s). @param dst An array of paths to the target resume file(s).
@param theme Friendly name of the resume theme. Defaults to "modern". @param opts Generation options.
@param logger Optional logging override.
### ###
build = ( src, dst, opts ) -> build = ( src, dst, opts ) ->
if !src || !src.length if !src || !src.length
@err HMSTATUS.resumeNotFound, { quit: true } @err HMSTATUS.resumeNotFound, quit: true
return null
prep src, dst, opts prep src, dst, opts
@ -74,10 +67,14 @@ build = ( src, dst, opts ) ->
}, @); }, @);
# Explicit check for any resume loading errors... # Explicit check for any resume loading errors...
if !sheetObjects || _.some( sheetObjects, (so) -> return so.fluenterror ) problemSheets = _.filter( sheetObjects, (so) -> return so.fluenterror )
if( problemSheets && problemSheets.length )
problemSheets[0].quit = true
@err problemSheets[0].fluenterror, problemSheets[0]
return null return null
sheets = sheetObjects.map((r) -> return r.json ) # Get the collection of raw JSON sheets
sheets = sheetObjects.map (r) -> r.json
# Load the theme... # Load the theme...
theme = null theme = null
@ -85,12 +82,14 @@ build = ( src, dst, opts ) ->
try try
tFolder = verifyTheme.call @, _opts.theme tFolder = verifyTheme.call @, _opts.theme
theme = _opts.themeObj = loadTheme tFolder theme = _opts.themeObj = loadTheme tFolder
addFreebieFormats theme
catch ex catch ex
newEx = newEx =
fluenterror: HMSTATUS.themeLoad fluenterror: HMSTATUS.themeLoad
inner: ex inner: ex
attempted: _opts.theme attempted: _opts.theme
this.err HMSTATUS.themeLoad, newEx quit: true
@err HMSTATUS.themeLoad, newEx
return null return null
@stat HMEVENT.afterTheme, { theme: theme } @stat HMEVENT.afterTheme, { theme: theme }
@ -98,7 +97,8 @@ build = ( src, dst, opts ) ->
# Check for invalid outputs... # Check for invalid outputs...
inv = verifyOutputs.call @, dst, theme inv = verifyOutputs.call @, dst, theme
if inv && inv.length if inv && inv.length
@err HMSTATUS.invalidFormat, { data: inv, theme: theme } @err HMSTATUS.invalidFormat, data: inv, theme: theme, quit: true
return null
## Merge input resumes, yielding a single source resume. ## Merge input resumes, yielding a single source resume.
rez = null rez = null
@ -107,7 +107,7 @@ build = ( src, dst, opts ) ->
mixed = _.any sheets, (s) -> return if isFRESH then s.basics else !s.basics mixed = _.any sheets, (s) -> return if isFRESH then s.basics else !s.basics
@stat HMEVENT.beforeMerge, { f: _.clone(sheetObjects), mixed: mixed } @stat HMEVENT.beforeMerge, { f: _.clone(sheetObjects), mixed: mixed }
if mixed if mixed
this.err HMSTATUS.mixedMerge @err HMSTATUS.mixedMerge
rez = _.reduceRight sheets, ( a, b, idx ) -> rez = _.reduceRight sheets, ( a, b, idx ) ->
extend( true, b, a ) extend( true, b, a )
@ -125,7 +125,6 @@ build = ( src, dst, opts ) ->
@stat HMEVENT.afterInlineConvert, { file: sheetObjects[0].file, fmt: toFormat } @stat HMEVENT.afterInlineConvert, { file: sheetObjects[0].file, fmt: toFormat }
# Add freebie formats to the theme # Add freebie formats to the theme
addFreebieFormats theme
@stat HMEVENT.applyTheme, { r: rez, theme: theme } @stat HMEVENT.applyTheme, { r: rez, theme: theme }
# Load the resume into a FRESHResume or JRSResume object # Load the resume into a FRESHResume or JRSResume object
@ -235,11 +234,9 @@ single = ( targInfo, theme, finished ) ->
###* ###* Ensure that user-specified outputs/targets are valid. ###
Ensure that user-specified outputs/targets are valid.
###
verifyOutputs = ( targets, theme ) -> verifyOutputs = ( targets, theme ) ->
@.stat HMEVENT.verifyOutputs, { targets: targets, theme: theme } @stat HMEVENT.verifyOutputs, { targets: targets, theme: theme }
_.reject targets.map( ( t ) -> _.reject targets.map( ( t ) ->
pathInfo = parsePath t pathInfo = parsePath t
{ {
@ -247,6 +244,8 @@ verifyOutputs = ( targets, theme ) ->
}), }),
(t) -> t.format == 'all' || theme.hasFormat( t.format ) (t) -> t.format == 'all' || theme.hasFormat( t.format )
###* ###*
Reinforce the chosen theme with "freebie" formats provided by HackMyResume. Reinforce the chosen theme with "freebie" formats provided by HackMyResume.
A "freebie" format is an output format such as JSON, YML, or PNG that can be A "freebie" format is an output format such as JSON, YML, or PNG that can be

View File

@ -17,12 +17,7 @@ HMEVENT = require('../core/event-codes');
ConvertVerb = module.exports = Verb.extend ConvertVerb = module.exports = Verb.extend
init: -> @._super 'convert' init: -> @_super 'convert', convert
invoke: ->
@.stat HMEVENT.begin, { cmd: 'convert' }
convert.apply @, arguments
@.stat HMEVENT.end

View File

@ -17,13 +17,8 @@ HMEVENT = require('../core/event-codes')
CreateVerb = module.exports = Verb.extend CreateVerb = module.exports = Verb.extend
init: -> @._super('new') init: -> @_super 'new', create
invoke: ->
@.stat HMEVENT.begin, { cmd: 'create' }
create.apply @, arguments
@.stat HMEVENT.begin, { cmd: 'convert' }
return
###* ###*

View File

@ -17,12 +17,7 @@ HMEVENT = require('../core/event-codes')
PeekVerb = module.exports = Verb.extend PeekVerb = module.exports = Verb.extend
init: -> @._super('peek') init: -> @_super 'peek', peek
invoke: ->
@.stat HMEVENT.begin, { cmd: 'peek' }
peek.apply @, arguments
@.stat HMEVENT.end

View File

@ -21,13 +21,7 @@ safeLoadJSON = require '../utils/safe-json-loader'
###* An invokable resume validation command. ### ###* An invokable resume validation command. ###
ValidateVerb = module.exports = Verb.extend ValidateVerb = module.exports = Verb.extend
init: -> @_super 'validate' init: -> @_super 'validate', validate
invoke: ->
@stat HMEVENT.begin, { cmd: 'validate' }
ret = validate.apply @, arguments
@stat HMEVENT.end
return ret

View File

@ -6,10 +6,10 @@ Definition of the Verb class.
# Use J. Resig's nifty class implementation # Use J. Resig's nifty class implementation
Class = require '../utils/class' Class = require '../utils/class'
EVENTS = require 'events' EVENTS = require 'events'
HMEVENT = require '../core/event-codes'
@ -22,24 +22,34 @@ Verb = module.exports = Class.extend
###* Constructor. Automatically called at creation. ### ###* Constructor. Automatically called at creation. ###
init: ( moniker ) -> init: ( moniker, workhorse ) ->
@.moniker = moniker @moniker = moniker
@.emitter = new EVENTS.EventEmitter() @emitter = new EVENTS.EventEmitter()
@workhorse = workhorse
return return
###* Invoke the command. ###
invoke: ->
@stat HMEVENT.begin, cmd: @moniker
ret = @workhorse.apply @, arguments
@stat HMEVENT.end
ret
###* Forward subscriptions to the event emitter. ### ###* Forward subscriptions to the event emitter. ###
on: -> on: ->
this.emitter.on.apply @.emitter, arguments @emitter.on.apply @emitter, arguments
###* Fire an arbitrary event, scoped to "hmr:". ### ###* Fire an arbitrary event, scoped to "hmr:". ###
fire: (evtName, payload) -> fire: (evtName, payload) ->
payload = payload || { } payload = payload || { }
payload.cmd = this.moniker payload.cmd = @moniker
this.emitter.emit 'hmr:' + evtName, payload @emitter.emit 'hmr:' + evtName, payload
true true
@ -60,10 +70,11 @@ Verb = module.exports = Class.extend
stat: ( subEvent, payload ) -> stat: ( subEvent, payload ) ->
payload = payload || { } payload = payload || { }
payload.sub = subEvent payload.sub = subEvent
this.fire 'status', payload @fire 'status', payload
true true
###* Associate error info with the invocation. ### ###* Associate error info with the invocation. ###
setError: ( code, obj ) -> setError: ( code, obj ) ->
@errorCode = code @errorCode = code