All files index.js

61.76% Statements 21/34
50% Branches 3/6
50% Functions 5/10
63.64% Lines 21/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 911x 1x 1x   1x 1x     2x 2x 2x 1x             1x             2x 2x         2x   2x 1x             1x     2x   2x       1x                                       1x               1x                        
const core = require("./core");
const coreCmds = require("./cmds");
const coreInterpreters = require("./interpreters");
 
let interpreters = Object.assign({}, coreInterpreters);
let context = {};
 
function promisify(fn) {
  Iif (fn.eadPromisified) return fn;
  const validator = fn.validator;
  const promised = function(...args) {
    Iif (validator) {
      try {
        validator(...args);
      } catch (e) {
        return Promise.reject(e);
      }
    }
    return core.call(context, interpreters, fn, ...args);
  };
  // try/catch because this is nice for reporting, but not
  // necessary for the system to function
  // Note: there is a unit test to validate this behavior
  // so errors, although swallowed here, would be picked
  // up in the unit test.
  try {
    Object.defineProperty(promised, "name", {
      value: fn.name,
      writable: false
    });
  } catch (e) {}
  promised.eadFn = fn;
 
  promised.callWithContext = function(c, ...args) {
    Iif (validator) {
      try {
        validator(...args);
      } catch (e) {
        return Promise.reject(e);
      }
    }
    return core.call(Object.assign({}, context, c), interpreters, fn, ...args);
  };
 
  promised.eadPromisified = true;
 
  return promised;
}
 
function setContext(c) {
  context = c;
}
 
function getContext() {
  return context;
}
 
function addToContext(c) {
  context = Object.assign({}, context, c);
}
 
function setInterpreters(h) {
  interpreters = h;
}
 
function getInterpreters() {
  return interpreters;
}
 
function addInterpreters(h) {
  interpreters = Object.assign({}, interpreters, h);
}
 
function reset() {
  interpreters = {};
  context = {};
}
 
module.exports = Object.assign({}, coreCmds, {
  cmds: coreCmds,
  interpreters: coreInterpreters,
  promisify,
  setContext,
  getContext,
  addToContext,
  setInterpreters,
  getInterpreters,
  addInterpreters,
  reset
});