home-path.js 854 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict'
  2. var os = require('os')
  3. /**
  4. * Cross-platform home directory retriever, tested on Windows XP and above, Mac OSX and Linux.
  5. *
  6. * With node versions 2.3.0 (iojs) or higher, the built-in [`os.homedir`](https://nodejs.org/api/os.html#os_os_homedir) method is used.
  7. *
  8. * @module home-path
  9. * @example
  10. * var getHomePath = require('home-path')
  11. */
  12. module.exports = os.homedir ? os.homedir : getHomePath
  13. /**
  14. * @alias module:home-path
  15. * @example
  16. * Mac OSX
  17. * ```js
  18. * > getHomePath()
  19. * '/Users/Lloyd'
  20. * ```
  21. *
  22. * Ubuntu Linux
  23. * ```js
  24. * > getHomePath()
  25. * '/home/lloyd'
  26. * ```
  27. *
  28. * Windows 8.1
  29. * ```js
  30. * > getHomePath()
  31. * 'C:\\Users\\Lloyd'
  32. * ```
  33. */
  34. function getHomePath () {
  35. if (process.platform === 'win32') {
  36. return process.env.USERPROFILE || process.env.HOMEDRIVE + process.env.HOMEPATH || process.env.HOME
  37. } else {
  38. return process.env.HOME
  39. }
  40. }