2016-01-27 10:29:26 +00:00
|
|
|
(function() {
|
2018-02-12 05:05:29 +00:00
|
|
|
/**
|
|
|
|
Implementation of the 'peek' verb for HackMyResume.
|
|
|
|
@module verbs/peek
|
|
|
|
@license MIT. See LICENSE.md for details.
|
|
|
|
*/
|
|
|
|
/** Peek at a resume, resume section, or resume field. */
|
|
|
|
/** Peek at a single resume, resume section, or resume field. */
|
|
|
|
var HMEVENT, HMSTATUS, PeekVerb, Verb, _, __, _peek, _peekOne, safeLoadJSON;
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
Verb = require('../verbs/verb');
|
|
|
|
|
|
|
|
_ = require('underscore');
|
|
|
|
|
|
|
|
__ = require('lodash');
|
|
|
|
|
|
|
|
safeLoadJSON = require('../utils/safe-json-loader');
|
|
|
|
|
|
|
|
HMSTATUS = require('../core/status-codes');
|
|
|
|
|
|
|
|
HMEVENT = require('../core/event-codes');
|
|
|
|
|
2018-02-12 05:05:29 +00:00
|
|
|
module.exports = PeekVerb = class PeekVerb extends Verb {
|
|
|
|
constructor() {
|
|
|
|
super('peek', _peek);
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
2016-02-02 04:16:49 +00:00
|
|
|
|
2018-02-12 05:05:29 +00:00
|
|
|
};
|
2016-01-27 10:29:26 +00:00
|
|
|
|
2016-02-02 02:14:36 +00:00
|
|
|
_peek = function(src, dst, opts) {
|
|
|
|
var objPath, results;
|
2016-01-27 10:29:26 +00:00
|
|
|
if (!src || !src.length) {
|
2016-02-02 02:14:36 +00:00
|
|
|
this.err(HMSTATUS.resumeNotFound, {
|
|
|
|
quit: true
|
2016-01-27 10:29:26 +00:00
|
|
|
});
|
2016-02-02 02:14:36 +00:00
|
|
|
return null;
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
|
|
|
objPath = (dst && dst[0]) || '';
|
2016-02-02 02:14:36 +00:00
|
|
|
results = _.map(src, function(t) {
|
|
|
|
var tgt;
|
|
|
|
if (opts.assert && this.hasError()) {
|
|
|
|
return {};
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
2016-02-02 02:14:36 +00:00
|
|
|
tgt = _peekOne.call(this, t, objPath);
|
2016-02-02 22:34:10 +00:00
|
|
|
if (tgt.error) {
|
2016-02-04 01:08:17 +00:00
|
|
|
this.setError(tgt.error.fluenterror, tgt.error);
|
2016-01-27 10:29:26 +00:00
|
|
|
}
|
2018-02-12 05:05:29 +00:00
|
|
|
//tgt.error.quit = opts.assert
|
|
|
|
//@err tgt.error.fluenterror, tgt.error
|
2016-02-02 22:34:10 +00:00
|
|
|
return tgt;
|
2016-01-27 10:29:26 +00:00
|
|
|
}, this);
|
2016-02-02 02:14:36 +00:00
|
|
|
if (this.hasError() && !opts.assert) {
|
|
|
|
this.reject(this.errorCode);
|
|
|
|
} else if (!this.hasError()) {
|
|
|
|
this.resolve(results);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
|
|
|
_peekOne = function(t, objPath) {
|
2016-02-02 22:34:10 +00:00
|
|
|
var errCode, obj, pkgError, tgt;
|
2016-02-02 02:14:36 +00:00
|
|
|
this.stat(HMEVENT.beforePeek, {
|
|
|
|
file: t,
|
|
|
|
target: objPath
|
|
|
|
});
|
2018-02-12 05:05:29 +00:00
|
|
|
// Load the input file JSON 1st
|
2016-02-02 02:14:36 +00:00
|
|
|
obj = safeLoadJSON(t);
|
2018-02-12 05:05:29 +00:00
|
|
|
// Fetch the requested object path (or the entire file)
|
2016-02-02 02:14:36 +00:00
|
|
|
tgt = null;
|
|
|
|
if (!obj.ex) {
|
|
|
|
tgt = objPath ? __.get(obj.json, objPath) : obj.json;
|
|
|
|
}
|
2018-02-12 05:05:29 +00:00
|
|
|
//# safeLoadJSON can only return a READ error or a PARSE error
|
2016-02-02 22:34:10 +00:00
|
|
|
pkgError = null;
|
2016-02-02 02:14:36 +00:00
|
|
|
if (obj.ex) {
|
2018-01-29 07:04:00 +00:00
|
|
|
errCode = obj.ex.op === 'parse' ? HMSTATUS.parseError : HMSTATUS.readError;
|
2016-02-02 02:14:36 +00:00
|
|
|
if (errCode === HMSTATUS.readError) {
|
|
|
|
obj.ex.quiet = true;
|
|
|
|
}
|
2016-02-02 22:34:10 +00:00
|
|
|
pkgError = {
|
2016-02-02 02:14:36 +00:00
|
|
|
fluenterror: errCode,
|
|
|
|
inner: obj.ex
|
|
|
|
};
|
|
|
|
}
|
2018-02-12 05:05:29 +00:00
|
|
|
// Fire the 'afterPeek' event with collected info
|
2016-02-02 22:34:10 +00:00
|
|
|
this.stat(HMEVENT.afterPeek, {
|
|
|
|
file: t,
|
|
|
|
requested: objPath,
|
|
|
|
target: obj.ex ? void 0 : tgt,
|
|
|
|
error: pkgError
|
|
|
|
});
|
|
|
|
return {
|
2016-02-04 01:08:17 +00:00
|
|
|
val: obj.ex ? void 0 : tgt,
|
|
|
|
error: pkgError
|
2016-02-02 22:34:10 +00:00
|
|
|
};
|
2016-01-27 10:29:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|
2016-02-02 02:14:36 +00:00
|
|
|
|
|
|
|
//# sourceMappingURL=peek.js.map
|