-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
93 lines (85 loc) · 3.11 KB
/
index.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
'use strict';
var exec = require('child_process').exec;
// Just to be sure we are in the right directory, before runnig any git
// command we get into the directory where this script was called from
var path = 'cd ' + process.cwd() + ';';
var async = require('async');
// See https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
// List of git commands needed to execute to get given data
var commands = {
name: '(basename $(git rev-parse --show-toplevel))',
authorDate: 'git --no-pager log --pretty=format:"%ad" -n1',
authorDateRelative: 'git --no-pager log --pretty=format:"%ar" -n1',
author: 'git --no-pager log --pretty=format:"%an <%ae>" -n1',
authors: 'git log --all --format=\'%aN <%cE>\' | sort -u',
repository: 'git config --get remote.origin.url',
branch: 'git branch | grep "*"',
branches: 'git branch',
shaShort: 'git rev-parse --short HEAD',
sha: 'git rev-parse HEAD',
subject: 'git --no-pager log --pretty=format:"%s" -n1'
};
// This function executes git command and add the data to the final object
var execGitCommand = function(responseObject, command, cb) {
var response;
exec(path + commands[command], function(error, stdout, stderr) {
if (error) {
console.log(error);
}
// Response lines are separated with new line sign, so we remove them
// (from the begining and the end) using trim() function, then, if asnwer
// has multi lines, we create an array out of what left
response = stdout.trim().split('\n');
// We don't want asterix char in current branch name
response = response.map(function(el){
return el.replace('* ', '');
});
// When answer has only one line, we don't need an Array
if (response.length === 1) {
response = response.pop();
}
responseObject[command] = response;
// async.each callback
cb();
});
};
function filterValid(responseObject, value) {
if (!commands[value]) {
if (!responseObject.errors) {
responseObject.errors = [];
}
responseObject.errors.push(value);
}
return !!commands[value];
}
/* Main function
Create a unique responseObject and return it when all async are done.
*/
var gitInfo = function(gitDataToGet, cb) {
/* if called with param that must be the callback
and we assume we want all commands.
*/
if (!cb) {
cb = gitDataToGet;
gitDataToGet = Object.keys(commands);
}
// If first parameter is not an array, make one (with just 1 element)
if (gitDataToGet && !Array.isArray(gitDataToGet)) {
gitDataToGet = [gitDataToGet];
}
// this object will be sent to callback, with all the answers
const responseObject = {};
// We don't want to execute a command that wasn't defined before
const filtered = gitDataToGet.filter(filterValid.bind(null, responseObject));
if (filtered.length === 0) {
throw new Error('No valid definitions for ' +
JSON.stringify(responseObject.errors));
}
// Execute all the commands in the same time
async.each(filtered, execGitCommand
.bind(null, responseObject), function(err) {
// And run callback with results when finished
cb(err, responseObject);
});
};
module.exports = gitInfo;