index.js 725 B

123456789101112131415161718192021
  1. 'use strict';
  2. // Call this function in a another function to find out the file from
  3. // which that function was called from. (Inspects the v8 stack trace)
  4. //
  5. // Inspired by http://stackoverflow.com/questions/13227489
  6. module.exports = function getCallerFile(_position) {
  7. var oldPrepareStackTrace = Error.prepareStackTrace;
  8. Error.prepareStackTrace = function(err, stack) { return stack; };
  9. var stack = new Error().stack;
  10. Error.prepareStackTrace = oldPrepareStackTrace;
  11. var position = _position ? _position : 2;
  12. // stack[0] holds this file
  13. // stack[1] holds where this function was called
  14. // stack[2] holds the file we're interested in
  15. return stack[position] ? stack[position].getFileName() : undefined;
  16. };