Passed
Push — master ( beba88...91d9d9 )
by Björn
02:26
created

lib/adapters/gitinfo.js (6 issues)

1
2
var fs          = require('fs');
3
var glob        = require('glob');
4
var globAll     = require('glob-all');
5
var path        = require('path');
6
var extend      = require('util')._extend;
7
var format      = require('string-template');
8
var frontMatter = require('front-matter');
9
var yaml        = require('js-yaml');
10
11
var git         = require('../util/git');
12
13
module.exports = function(value, config, cb, pl) {
14
    var data = getGitInfo (value, config, pl);
15
    //config.$PL.debug('gitinfo:', data);
16
    cb(null, processTree(data, config));
17
}
18
19
module.exports.config = {
20
    verbose: false
21
}
22
23
module.exports.search = function(items, link) {
0 ignored issues
show
The parameter link is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
The parameter items is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
24
    var results = [];
25
    // this => collider
26
    /* results.push({
27
        name: name,
28
        type: 'sass ' + type,
29
        description: description,
30
        link: link + hash
31
    }); */
32
33
    return results;
34
}
35
36
37
function getGitInfo(value, config, pl) {
38
39
    if (!fs.existsSync(value)) return false;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
40
41
    var logFormatYaml = 
42
	    '- date: \'%cd\''+'%n'+
43
	    '  timestamp: %ct'+'%n'+
44
	    '  author: %an'+'%n'+
45
	    '  email: %ae'+'%n'+
46
	    '  message: \'\"%s\"\''+'%n'+
47
	    '  commit: %h'+'%n'+
48
	    '  hash: %H'+'%n'+
49
	    '  body: \'\"%b\"\''+'%n'+
50
	    '%n';
51
52
    var logFormatCompact = 
53
	    '### %cd'+'%n'+
54
	    '%n'+
55
	    '- %s (%h, by [%an](mailto:%ae), <%ae>)'+'%n'+
56
	    '%n'+
57
	    '%n';
58
59
    var logFormatFull = 
60
	    '### %cD'+'%n'+
61
	    '%n'+
62
	    '- Author: [%an](mailto:%ae) (<%ae>)'+'%n'+
63
	    '%n'+
64
	    '  Commit: %H'+'%n'+
65
	    '%n'+
66
	    '  Message: %s'+'%n'+
67
	    '%n'+
68
	    //'  Body: \'%b\''+'%n'+
69
	    //'%n'+
70
	    //'  \\`\\`\\`diff'+'%n'+
71
	    //'  \`git diff %H '+value+'\`'+'%n'+
72
	    //'  \\`\\`\\`'+'%n'+
73
	    '%n';
74
   
75
76
    var logData    = yaml.load( getGitLog(value, logFormatYaml, config) );
77
    var logCompact = getGitLog(value, logFormatCompact, config);
78
    var logFull    = getGitLog(value, logFormatFull, config);
79
    
80
    return {
81
    	log: {
82
	    	data: logData,
83
	    	list: pl.markdown.render(String(logCompact)),
84
	    	full: pl.markdown.render(String(logFull))
85
	    }
86
    };
87
}
88
89
90
function processTree(tree, config) {
0 ignored issues
show
The parameter config is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
91
92
	var logs = {};
93
	
94
	for (var i in tree.log.data) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
95
		var obj = tree.log.data[i];
96
		var logdate = obj.date;
97
		
98
		if (!logs[logdate]) { logs[logdate] = []; }
99
		logs[logdate].push(obj);
100
	}
101
	tree.log.data = logs;
102
    
103
    return tree;
104
}
105
106
function getGitLog ( filepath, logFormat, config ) {
0 ignored issues
show
The parameter config is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
107
	var gitLog = git.log( filepath, 'format:"'+logFormat+'"' );
108
    return gitLog;
109
}
110
111
112