From 181419ae2832f70421c1bac43253c5ed09ec2aa3 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Mon, 18 Jan 2016 19:20:17 -0500 Subject: [PATCH] Improve PEEK command behavior. --- src/cli/out.js | 18 ++++++++++++------ src/verbs/peek.js | 28 ++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/cli/out.js b/src/cli/out.js index 20913e3..4adccb4 100644 --- a/src/cli/out.js +++ b/src/cli/out.js @@ -197,16 +197,22 @@ Output routines for HackMyResume. break; case HME.beforePeek: - if( evt.target ) - L(M2C(this.msgs.beforePeek.msg[0], evt.isError ? 'red' : 'green'), evt.target, evt.file); - else - L(M2C(this.msgs.beforePeek.msg[1], evt.isError ? 'red' : 'green'), evt.file); + // if( evt.target ) + // L(M2C(this.msgs.beforePeek.msg[0], evt.isError ? 'red' : 'green'), evt.target, evt.file); + // else + // L(M2C(this.msgs.beforePeek.msg[1], evt.isError ? 'red' : 'green'), evt.file); break; case HME.afterPeek: - if( evt.target ) - console.dir( evt.target, { depth: null, colors: true } ); + var sty = evt.error ? 'red' : ( evt.target !== undefined ? 'green' : 'yellow' ); + if( evt.requested ) + L(M2C(this.msgs.beforePeek.msg[0], sty), evt.requested, evt.file); else + L(M2C(this.msgs.beforePeek.msg[1], sty), evt.file); + + if( evt.target !== undefined ) + console.dir( evt.target, { depth: null, colors: true } ); + else if( !evt.error ) L(M2C( this.msgs.afterPeek.msg, 'yellow'), evt.requested, evt.file); break; diff --git a/src/verbs/peek.js b/src/verbs/peek.js index 27b6e04..32ead87 100644 --- a/src/verbs/peek.js +++ b/src/verbs/peek.js @@ -46,9 +46,26 @@ Implementation of the 'peek' verb for HackMyResume. _.each( src, function( t ) { - var obj = safeLoadJSON( t ); - this.stat( HMEVENT.beforePeek, { file: t, target: objPath, isError: obj.ex } ); + // Fire the 'beforePeek' event 2nd, so we have error/warning/success + this.stat( HMEVENT.beforePeek, { file: t, target: objPath } ); + // Load the input file JSON 1st + var obj = safeLoadJSON( t ); + + // Fetch the requested object path (or the entire file) + var tgt; + if( !obj.ex ) + tgt = objPath ? __.get( obj.json, objPath ) : obj.json; + + // Fire the 'afterPeek' event with collected info + this.stat( HMEVENT.afterPeek, { + file: t, + requested: objPath, + target: tgt, + error: obj.ex + }); + + // safeLoadJSON can only return a READ error or a PARSE error if( obj.ex ) { if( obj.ex.operation === 'parse' ) this.err( HMSTATUS.parseError, obj.ex ); @@ -56,12 +73,7 @@ Implementation of the 'peek' verb for HackMyResume. obj.ex.quiet = true; this.err( HMSTATUS.readError, obj.ex ); } - return; - } - - var targ = objPath ? __.get( obj.json, objPath ) : obj.json; - this.stat( HMEVENT.afterPeek, { file: t, requested: objPath, target: targ } ); - + } }, this);