1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-22 08:20:11 +00:00

Introduce PEEK command.

Peek at arbitrary resumes and resume objects paths with "hackmyresume
peek <resume> [objectPath]". For ex:

hackmyresume PEEK resume.json
hackmyresume PEEK resume.json info
hackmyresume PEEK resume.json employment[2].keywords
hackmyresume PEEK r1.json r2.json r3.json info.brief
This commit is contained in:
hacksalot 2016-01-15 13:08:01 -05:00
parent de5c2ecb95
commit 4c5ccc001a
7 changed files with 125 additions and 10 deletions

View File

@ -16,6 +16,7 @@ Error-handling routines for HackMyResume.
, FCMD = require('../hackmyapi')
, PATH = require('path')
, WRAP = require('word-wrap')
, M2C = require('../utils/md2chalk.js')
, chalk = require('chalk')
, SyntaxErrorEx = require('../utils/syntax-error-ex');
require('string.prototype.startswith');
@ -182,7 +183,7 @@ Error-handling routines for HackMyResume.
' column ' + se.col + '.';
}
else {
msg = formatError( ex.inner.toString() );
msg = ex;
}
warn = false;
break;

View File

@ -22,7 +22,6 @@ Definition of the `main` function.
, StringUtils = require('../utils/string.js')
, _ = require('underscore')
, OUTPUT = require('./out')
, SAFELOADJSON = require('../utils/safe-json-loader')
, PAD = require('string-padding')
, Command = require('commander').Command;
@ -93,6 +92,15 @@ Definition of the `main` function.
execute.call( this, sources, [], this.opts(), logMsg);
});
// Create the PEEK command
program
.command('peek')
.arguments('<sources...>')
.description('Peek at a resume field or section')
.action(function( sources, sectionOrField ) {
execute.call( this, sources, [ sources.pop() ], this.opts(), logMsg);
});
// Create the BUILD command
program
.command('build')
@ -189,7 +197,8 @@ Definition of the `main` function.
if( optStr[0] === '{')
oJSON = eval('(' + optStr + ')'); // jshint ignore:line
else {
oJSON = SAFELOADJSON( optStr );
oJSON = safeLoadJSON( optStr );
// TODO: Error handling
}
}
}

View File

@ -189,6 +189,20 @@ Output routines for HackMyResume.
break;
case HME.beforePeek:
if( evt.target )
L(M2C('Peeking at **%s** in **%s**...', 'cyan'), evt.target, evt.file);
else
L(M2C('Peeking at **%s**...', 'cyan'), evt.file);
break;
case HME.afterPeek:
if( evt.target )
console.dir( evt.target, { depth: null, colors: true } );
else
L(M2C('The specified key **%s** was not found in **%s**.', 'yellow'), evt.requested, evt.file);
break;
}
}

View File

@ -32,7 +32,9 @@ Event code definitions.
afterConvert: 16,
verifyOutputs: 17,
beforeParse: 18,
afterParse: 19
afterParse: 19,
beforePeek: 20,
afterPeek: 21
};

View File

@ -19,7 +19,8 @@ External API surface for HackMyResume.
analyze: require('./verbs/analyze'),
validate: require('./verbs/validate'),
convert: require('./verbs/convert'),
new: require('./verbs/create')
new: require('./verbs/create'),
peek: require('./verbs/peek')
},
alias: {
generate: require('./verbs/build'),

View File

@ -8,16 +8,41 @@ Definition of the SafeJsonLoader class.
(function() {
var FS = require('fs');
var FS = require('fs')
, HMSTATUS = require('../core/status-codes')
, SyntaxErrorEx = require('./syntax-error-ex');
module.exports = function loadSafeJson( file ) {
var ret = { };
try {
return JSON.parse( FS.readFileSync( file ) );
ret.raw = FS.readFileSync( file, 'utf8' );
ret.json = JSON.parse( ret.raw );
}
catch( ex ) {
loadSafeJson.error = ex;
// If we get here, either FS.readFileSync or JSON.parse failed.
// We'll return HMSTATUS.readError or HMSTATUS.parseError.
ret.ex = ( ret.raw && ret.raw.trim() ) ?
{ // JSON.parse failed, likely because of a SyntaxError
fluenterror: HMSTATUS.parseError,
inner: SyntaxErrorEx.is( ex ) ? new SyntaxErrorEx( ex ) : ex
} :
{ // FS.readFileSync failed, likely because of ENOENT or EACCES
fluenterror: HMSTATUS.readError,
inner: ex
};
}
return null;
return ret;
};

63
src/verbs/peek.js Normal file
View File

@ -0,0 +1,63 @@
/**
Implementation of the 'peek' verb for HackMyResume.
@module peek.js
@license MIT. See LICENSE.md for details.
*/
(function(){
var Verb = require('../verbs/verb')
, _ = require('underscore')
, __ = require('lodash')
, safeLoadJSON = require('../utils/safe-json-loader')
, HMSTATUS = require('../core/status-codes')
, HMEVENT = require('../core/event-codes');
var PeekVerb = module.exports = Verb.extend({
init: function() {
this._super('peek');
},
invoke: function() {
peek.apply( this, arguments );
}
});
/**
Peek at a resume, resume section, or resume field.
*/
function peek( src, dst, opts ) {
if(!src || !src.length) throw {fluenterror: HMSTATUS.resumeNotFound};
this.stat( HMEVENT.begin );
var objPath = (dst && dst[0]) || '';
_.each( src, function( t ) {
this.stat( HMEVENT.beforePeek, { file: t, target: objPath } );
var obj = safeLoadJSON( t );
if( obj.ex ) {
this.err( obj.ex.fluenterror, obj.ex );
}
var targ = objPath ? __.get( obj.json, objPath ) : obj;
this.stat( HMEVENT.afterPeek, { file: t, requested: objPath, target: targ } );
}, this);
this.stat( HMEVENT.end );
}
}());