index.js 664 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Modules
  3. */
  4. var path = require('path')
  5. /**
  6. * Vars
  7. */
  8. var relative = path.relative
  9. var lastCwd = process.cwd()
  10. var cache = Object.create(null)
  11. /**
  12. * Expose cachedPathRelative
  13. */
  14. module.exports = cachedPathRelative
  15. /**
  16. * cachedPathRelative
  17. */
  18. function cachedPathRelative (from, to) {
  19. // If the current working directory changes, we need
  20. // to invalidate the cache
  21. var cwd = process.cwd()
  22. if (cwd !== lastCwd) {
  23. cache = {}
  24. lastCwd = cwd
  25. }
  26. if (cache[from] && cache[from][to]) return cache[from][to]
  27. var result = relative.call(path, from, to)
  28. cache[from] = cache[from] || {}
  29. cache[from][to] = result
  30. return result
  31. }