-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
179 lines (142 loc) · 4.4 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var fs = require('fs');
function fetchAbsolutePath(fileList) {
var path = null;
if (fileList.length < 1) {
return path;
}
for (var i = 0, li = fileList.length; i < li; i++) {
var matchResult = fileList[i].match(/\.git/);
if (matchResult != null) {
path = fileList[i].substring(0, matchResult.index - 1);
return path;
}
}
return path;
}
function getUserHome() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}
function parsingAuthinfoOnGitConfig(callback) {
fs.readFile(getUserHome() + "/.gitconfig", {encoding: "utf-8"}, function (err, data) {
if (err) {
return callback(err);
}
var nameStrOnConfig = data.match(/name[ \t]*=[ \t]*[a-zA-Z0-9]+\n/);
if (nameStrOnConfig === null) {
return callback("Check Your name on gitconfig(path: ~/.gitconfig)");
}
var configuredName = nameStrOnConfig[0].split("=")[1].replace(" ", "").replace(/\n/, "");
return callback(null, configuredName);
});
}
function fetchAbsolutePath(fileList) {
var path = null;
if (fileList.length < 1) {
return path;
}
for (var i = 0, li = fileList.length; i < li; i++) {
var matchResult = fileList[i].match(/\.git/);
if (matchResult != null) {
path = fileList[i].substring(0, matchResult.index - 1);
return path;
}
}
return path;
}
//default desc
function sortAndMergeLogList(repositoryInfoArr, isDesc) {
if (repositoryInfoArr.length === 0) {
return "ERR : ";
}
var concatArr = [];
for (var i = 0, li = repositoryInfoArr.length; i < li; i++) {
for (var j = 0, lj = repositoryInfoArr[i].logArr.length; j < lj; j++) {
repositoryInfoArr[i].logArr[j].repositoryName = repositoryInfoArr[i].repositoryName;
}
concatArr = concatArr.concat(repositoryInfoArr[i].logArr);
}
return sortByOption(concatArr, isDesc);;
}
//sort by option
function sortByOption(concatArr, isDesc) {
if (isDesc === true) {
concatArr.sort(function (a,b) {
a = new Date(a.date);
b = new Date(b.date);
return a > b ? -1 : a < b ? 1 : 0;
});
} else {
concatArr.sort(function (a,b) {
a = new Date(a.date);
b = new Date(b.date);
return a < b ? -1 : a > b ? 1 : 0;
});
}
return concatArr;
}
function createTableHeadRowElement(log) {
var tableHeadRowElement = "<tr>";
for (var key in log) {
if (key != "diffWeek") {
tableHeadRowElement += ("<th>" + key + "</th>");
}
}
tableHeadRowElement += "</tr>";
return tableHeadRowElement;
}
function createTableBodyRowElement(log) {
var tableBodyRowElement = "<tr>";
for (var key in log) {
if (key != "diffWeek") {
tableBodyRowElement += ("<td>" + log[key] + "</td>");
}
}
tableBodyRowElement += "</tr>";
return tableBodyRowElement;
}
function createSetTableBodyRow(logs) {
var today = new Date();
var logDay = new Date(log.date);
if (today.getDay() === logDay.getDay()) {
var diffWeek = getDiffWeekWithToday(today, logDay);
if (diffWeek != 0) {
tableBodyRowElement += "<tr><td>Before " + diffWeek + " weeks</td></tr>";
}
}
var returnElement = "";
for (var i = 0, li = logs.length; i < li; i++) {
returnElement += createTableBodyRowElement(logs[i]);
}
return returnElement;
}
function getDiffWeekWithToday(today, date) {
var todayFullYear = today.getFullYear();
var dateFullYear = date.getFullYear();
if (todayFullYear === dateFullYear) {
return getWeekOfYear(today) - getWeekOfYear(date);
} else {
var yearDiff = todayFullYear - dateFullYear;
return (yearDiff * 52) + getWeekOfYear(today) - getWeekOfYear(date);
}
}
function getWeekOfYear(date) {
var onejan = new Date(date.getFullYear(),0,1);
return Math.ceil((((date - onejan) / 86400000) + onejan.getDay()+1)/7);
}
function repeatElement(el, count) {
var result = "";
for (var i = 0; i < count; i++) {
result += el;
}
return result;
}
module.exports.parsingAuthinfoOnGitConfig = parsingAuthinfoOnGitConfig;
module.exports.getUserHome = getUserHome;
module.exports.fetchAbsolutePath = fetchAbsolutePath;
module.exports.sortAndMergeLogList = sortAndMergeLogList;
module.exports.createTableHeadRowElement = createTableHeadRowElement;
module.exports.createTableBodyRowElement = createTableBodyRowElement;
module.exports.sortByOption = sortByOption;
module.exports.getWeekOfYear = getWeekOfYear;
module.exports.getDiffWeekWithToday = getDiffWeekWithToday;
module.exports.repeatElement = repeatElement;