I am looking for a way to keep functions declared in module scope, export them, and allow them to be wrapped in a completely different file, and export a wrapped copy of all modules. The issue: The wrap works, but it only wraps the called function, that function is still calling the unwrapped counter-parts. Without changing the scope of the original authored modules, is there any way to tell the log version, to use only other logged version?
colors.js
let green = () => 'green'
let red = () => 'red'
let purple = () => 'purple'
let colors = () => green() + red() + purple()
module.exports = {
  red,
  green,
  purple,
  colors
}
colors.log.js
const {logWrapFns} = require('./index')
let {
  red,
  green,
  purple,
  colors
} = require('./colors')
;[red, green, purple, colors] = logWrapFns('color')([
  red,
  green,
  purple,
  colors
])
module.exports = {
  red,
  green,
  purple,
  colors
}
debugFns.js
const {map, mapValues, get} = require('lodash')
const debug = require('debug')
const logWrapFn = (scope) => (fn, key) => (...input) => {
  const d = debug(`${scope}:${fn.name || key}`)
  d(JSON.stringify({input}))
  const possiblePromise = fn.apply(null, input)
  if (get(possiblePromise, 'then')) {
    return possiblePromise.then(output => {
      d(JSON.stringify({output}))
      return output
    })
  } else {
    d(JSON.stringify({output: possiblePromise}))
    return possiblePromise
  }
}
const logWrapFnsReturnObj = (scope) => (fns) => mapValues(fns, logWrapFn(scope))
const logWrapFnsReturnArr = (scope) => (fns) => map(fns, logWrapFn(scope))
module.exports = {
  logWrapFn,
  logWrapFns
}
usage.js
const colors = require('./colors.log')
console.log(colors)
console.log(colors.green())
Only logging debug statement for green not the rest.
I know this is possible if I author the module with a class and use static methods, however then I have to call each method with the extra class name, which is something I am trying to avoid.
 
    