-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortAuthors.js
116 lines (78 loc) · 2.97 KB
/
sortAuthors.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
//Package Imports
const shell = require(`shelljs`);
const fs = require(`fs`);
const readline = require(`readline`);
const reader = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function getUserInput() {
//Stores user entries
let inputs = [];
//Count for question cycle
let i = 0;
let question = ``;
let cli = {};
//Welcome message
console.log(`\n\x1b[35m%s\x1b[0m`, `Welcome To Sort Git Authors CLI`);
cli.askQuestion = function () {
//Iterating questions
if (i === 0) {
question = `Please enter the repository full path as using this format \n"C:/Users/shaqu/Desktop/...": `;
} else if (i === 1) {
question = `Please enter the starting date using this format 'Dec 20 2021': `;
} else if (i === 2) {
question = `Please enter the ending date using this format 'Jul 21 2022': `;
} else if (i === 3) {
question = `Please enter the number of authors you'd like returned: `;
}
//Readline question function
reader.question(`${question}`, ans => {
if (!ans) {
return console.log("Please enter a value with the proper format");
} else {
//Incrementing count
i++;
//Logging user entries
console.log(`\x1b[32m%s\x1b[0m`, `${ans} saved! \n`);
//Pushing answers to array
inputs.push(ans);
if (i < 4) {
//Running readline question for 4 iterations
cli.askQuestion();
} else {
//Passing strings in sorting function
sortAuthors(inputs);
}
}
});
}
//Initiating function the first time
cli.askQuestion();
}
getUserInput();
//Saving top 12 authors from git repo data
function sortAuthors(inputs) {
//Repository full path
let repo = inputs[0].toString();
//Time period
let startDate = inputs[1].toString();
let endDate = inputs[2].toString();
//Authors sorted by most commits from git repo
const data = shell.exec(`git -C ${repo} shortlog head -sne --since="${startDate}" --until="${endDate}"`);
//Spliting string line by line and storing it into a new variable
const info = data.split(`\n`);
//Number of authors to return
let count = parseInt(inputs[3]);
//Looping through the 12 most popular authors of the repo
for (let i = 1; i <= count; i++) {
fs.appendFileSync(`authors.txt`, `\n ${info[i]}`, (err) => {
if (err) {
return console.log(err); //Returning error if operation fails
}
});
};
console.clear();
console.log(`\x1b[32m%s\x1b[0m`, `\n Authors saved sucessfully!`);
reader.close();
}