user.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const shell = require('shelljs');
  3. const githubUsername = require('github-username');
  4. const nameCache = new Map();
  5. const emailCache = new Map();
  6. /**
  7. * @mixin
  8. * @alias actions/user
  9. */
  10. const user = module.exports;
  11. user.git = {};
  12. user.github = {};
  13. /**
  14. * Retrieves user's name from Git in the global scope or the project scope
  15. * (it'll take what Git will use in the current context)
  16. * @return {String} configured git name or undefined
  17. */
  18. user.git.name = () => {
  19. let name = nameCache.get(process.cwd());
  20. if (name) {
  21. return name;
  22. }
  23. if (shell.which('git')) {
  24. name = shell.exec('git config --get user.name', {silent: true}).stdout.trim();
  25. nameCache.set(process.cwd(), name);
  26. }
  27. return name;
  28. };
  29. /**
  30. * Retrieves user's email from Git in the global scope or the project scope
  31. * (it'll take what Git will use in the current context)
  32. * @return {String} configured git email or undefined
  33. */
  34. user.git.email = () => {
  35. let email = emailCache.get(process.cwd());
  36. if (email) {
  37. return email;
  38. }
  39. if (shell.which('git')) {
  40. email = shell.exec('git config --get user.email', {silent: true}).stdout.trim();
  41. emailCache.set(process.cwd(), email);
  42. }
  43. return email;
  44. };
  45. /**
  46. * Retrieves GitHub's username from the GitHub API
  47. * @return {Promise} Resolved with the GitHub username or rejected if unable to
  48. * get the information
  49. */
  50. user.github.username = () => githubUsername(user.git.email());