From 88c71f6e9cbbc01c48a2efbf7383ea6452e1f75c Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sat, 9 Jan 2016 06:44:22 -0500 Subject: [PATCH] Move commands to Verb hierarchy Move flat command functions (BUILD, ANALYZE, etc.) to a shallow Verb hierarchy. Allow command verbs to inherit common functionality and prep for better debugging/logging as well as test mocks. --- src/core/verb.js | 28 ++++++++++++++++++++++++++++ src/main.js | 4 +++- src/verbs/analyze.js | 16 +++++++++++++--- src/verbs/build.js | 12 ++++++++---- src/verbs/convert.js | 14 ++++++++++++-- src/verbs/create.js | 15 +++++++++++++-- src/verbs/validate.js | 15 +++++++++++++-- 7 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 src/core/verb.js diff --git a/src/core/verb.js b/src/core/verb.js new file mode 100644 index 0000000..0739103 --- /dev/null +++ b/src/core/verb.js @@ -0,0 +1,28 @@ +/** +Definition of the Verb class. +@module verb.js +@license MIT. See LICENSE.md for details. +*/ + + + +(function(){ + + + + // Use J. Resig's nifty class implementation + var Class = require( '../utils/class' ); + + + + /** + An instantiation of a HackMyResume command. + @class Verb + */ + var Verb = module.exports = Class.extend({ + + }); + + + +}()); diff --git a/src/main.js b/src/main.js index f1a4c2a..2c3819e 100644 --- a/src/main.js +++ b/src/main.js @@ -165,7 +165,9 @@ function execVerb( src, dst, opts, log ) { loadOptions.call( this, opts ); require('./core/error-handler').init( _opts.debug ); - HMR.verbs[ this.name() ].call( null, src, dst, _opts, log ); + + var v = new HMR.verbs[ this.name() ](); + v.invoke.call( null, src, dst, _opts, log ); } diff --git a/src/verbs/analyze.js b/src/verbs/analyze.js index 110b868..eec058e 100644 --- a/src/verbs/analyze.js +++ b/src/verbs/analyze.js @@ -14,14 +14,25 @@ Implementation of the 'analyze' verb for HackMyResume. , PATH = require('path') , _ = require('underscore') , ResumeFactory = require('../core/resume-factory') + , Verb = require('../core/verb') , chalk = require('chalk'); + var AnalyzeVerb = module.exports = Verb.extend({ + + invoke: function() { + analyze.apply( this, arguments ); + } + + }); + + + /** Run the 'analyze' command. */ - module.exports = function analyze( sources, dst, opts, logger ) { + function analyze( sources, dst, opts, logger ) { var _log = logger || console.log; if( !sources || !sources.length ) throw { fluenterror: 3 }; @@ -33,8 +44,7 @@ Implementation of the 'analyze' verb for HackMyResume. }); result.error || _analyze( result, nlzrs, opts, _log ); }); - - }; + } diff --git a/src/verbs/build.js b/src/verbs/build.js index aa3f8e4..826507f 100644 --- a/src/verbs/build.js +++ b/src/verbs/build.js @@ -26,9 +26,17 @@ Implementation of the 'generate' verb for HackMyResume. , extend = require('../utils/extend') , chalk = require('chalk') , pad = require('string-padding') + , Verb = require('../core/verb') , _err, _log, rez; + var BuildVerb = module.exports = Verb.extend({ + + invoke: function() { + build.apply( this, arguments ); + } + + }); /** Given a source resume in FRESH or JRS format, a destination resume path, and a @@ -349,8 +357,4 @@ Implementation of the 'generate' verb for HackMyResume. - module.exports = build; - - - }()); diff --git a/src/verbs/convert.js b/src/verbs/convert.js index d050ec0..b9100b6 100644 --- a/src/verbs/convert.js +++ b/src/verbs/convert.js @@ -12,14 +12,24 @@ Implementation of the 'convert' verb for HackMyResume. var ResumeFactory = require('../core/resume-factory') , chalk = require('chalk') + , Verb = require('../core/verb') , HACKMYSTATUS = require('../core/status-codes'); + var ConvertVerb = module.exports = Verb.extend({ + + invoke: function() { + convert.apply( this, arguments ); + } + + }); + + /** Convert between FRESH and JRS formats. */ - module.exports = function convert( srcs, dst, opts, logger ) { + function convert( srcs, dst, opts, logger ) { // Housekeeping var _log = logger || console.log; @@ -62,7 +72,7 @@ Implementation of the 'convert' verb for HackMyResume. }); - }; + } diff --git a/src/verbs/create.js b/src/verbs/create.js index dd78407..a34accc 100644 --- a/src/verbs/create.js +++ b/src/verbs/create.js @@ -9,12 +9,23 @@ Implementation of the 'create' verb for HackMyResume. var MKDIRP = require('mkdirp') , PATH = require('path') , chalk = require('chalk') + , Verb = require('../core/verb') , HACKMYSTATUS = require('../core/status-codes'); + + var CreateVerb = module.exports = Verb.extend({ + + invoke: function() { + create.apply( this, arguments ); + } + + }); + + /** Create a new empty resume in either FRESH or JRS format. */ - module.exports = function create( src, dst, opts, logger ) { + function create( src, dst, opts, logger ) { var _log = logger || console.log; if( !src || !src.length ) throw { fluenterror: HACKMYSTATUS.createNameMissing }; src.forEach( function( t ) { @@ -26,6 +37,6 @@ Implementation of the 'create' verb for HackMyResume. RezClass.default().save(t); //FLUENT[ safeFormat + 'Resume' ].default().save( t ); }); - }; + } }()); diff --git a/src/verbs/validate.js b/src/verbs/validate.js index 3473a3f..906bb77 100644 --- a/src/verbs/validate.js +++ b/src/verbs/validate.js @@ -10,9 +10,20 @@ Implementation of the 'validate' verb for HackMyResume. var ResumeFactory = require('../core/resume-factory'); var SyntaxErrorEx = require('../utils/syntax-error-ex'); var chalk = require('chalk'); + var Verb = require('../core/verb'); var HACKMYSTATUS = require('../core/status-codes'); - module.exports = + + + var ValidateVerb = module.exports = Verb.extend({ + + invoke: function() { + validate.apply( this, arguments ); + } + + }); + + /** Validate 1 to N resumes in either FRESH or JSON Resume format. @@ -99,6 +110,6 @@ Implementation of the 'validate' verb for HackMyResume. } }); - }; + } }());