gitAdded.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const simpleGit = require("simple-git")();
  2. function getSplitTime(date) {
  3. return {
  4. year: date.getUTCFullYear(),
  5. month: date.getUTCMonth() + 1,
  6. date: date.getUTCDate(),
  7. hours: date.getUTCHours(),
  8. minutes: date.getUTCMinutes(),
  9. timestamp: Math.round(date.getTime() / 1000)
  10. };
  11. }
  12. function getFileDetails(commits) {
  13. const firstCommit = commits[commits.length - 1] || {};
  14. const lastCommit = commits[0] || {};
  15. const addDate = new Date(firstCommit.date);
  16. const modifiedDate = new Date(lastCommit.date);
  17. return {
  18. added: { ...getSplitTime(addDate), commitId: firstCommit.hash },
  19. modified: { ...getSplitTime(modifiedDate), commitId: lastCommit.hash },
  20. authorName: firstCommit.author_name || "not committed yet",
  21. authorEmail: firstCommit.author_email || "not committed yet"
  22. };
  23. }
  24. module.exports = function gitAdded(config, item) {
  25. return new Promise((resolve, reject) => {
  26. simpleGit.log(
  27. {
  28. file: item.path,
  29. format: {
  30. hash: "%H",
  31. date: "%aI",
  32. message: "%s",
  33. refs: "%D",
  34. body: "%B",
  35. author_name: "%aN",
  36. author_email: "%ae"
  37. }
  38. },
  39. (err, res) => {
  40. if (err) {
  41. reject(e);
  42. } else {
  43. resolve({
  44. ...item,
  45. ...getFileDetails(res.all)
  46. });
  47. }
  48. }
  49. );
  50. });
  51. };