Brak opisu

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. var os = require('os');
  3. var fs = require('fs');
  4. var parse = require('parse-passwd');
  5. function homedir() {
  6. // The following logic is from looking at logic used in the different platform
  7. // versions of the uv_os_homedir function found in https://github.com/libuv/libuv
  8. // This is the function used in modern versions of node.js
  9. if (process.platform === 'win32') {
  10. // check the USERPROFILE first
  11. if (process.env.USERPROFILE) {
  12. return process.env.USERPROFILE;
  13. }
  14. // check HOMEDRIVE and HOMEPATH
  15. if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
  16. return process.env.HOMEDRIVE + process.env.HOMEPATH;
  17. }
  18. // fallback to HOME
  19. if (process.env.HOME) {
  20. return process.env.HOME;
  21. }
  22. return null;
  23. }
  24. // check HOME environment variable first
  25. if (process.env.HOME) {
  26. return process.env.HOME;
  27. }
  28. // on linux platforms (including OSX) find the current user and get their homedir from the /etc/passwd file
  29. var passwd = tryReadFileSync('/etc/passwd');
  30. var home = find(parse(passwd), getuid());
  31. if (home) {
  32. return home;
  33. }
  34. // fallback to using user environment variables
  35. var user = process.env.LOGNAME || process.env.USER || process.env.LNAME || process.env.USERNAME;
  36. if (!user) {
  37. return null;
  38. }
  39. if (process.platform === 'darwin') {
  40. return '/Users/' + user;
  41. }
  42. return '/home/' + user;
  43. }
  44. function find(arr, uid) {
  45. var len = arr.length;
  46. for (var i = 0; i < len; i++) {
  47. if (+arr[i].uid === uid) {
  48. return arr[i].homedir;
  49. }
  50. }
  51. }
  52. function getuid() {
  53. if (typeof process.geteuid === 'function') {
  54. return process.geteuid();
  55. }
  56. return process.getuid();
  57. }
  58. function tryReadFileSync(fp) {
  59. try {
  60. return fs.readFileSync(fp, 'utf8');
  61. } catch (err) {
  62. return '';
  63. }
  64. }
  65. if (typeof os.homedir === 'undefined') {
  66. module.exports = homedir;
  67. } else {
  68. module.exports = os.homedir;
  69. }