Passed
Push — master ( cc9df7...a66138 )
by Björn
02:29
created

lib/adapters/gitinfo.js (1 issue)

1
2 1
var fs          = require('fs');
3 1
var glob        = require('glob');
4 1
var globAll     = require('glob-all');
5 1
var path        = require('path');
6 1
var extend      = require('util')._extend;
7 1
var format      = require('string-template');
8 1
var frontMatter = require('front-matter');
9 1
var yaml        = require('js-yaml');
10
11 1
var git         = require('../util/git');
12
13 1
module.exports = function(value, config, cb, pl) {
14 2
    var data = getGitInfo (value, config, pl);
15
    //config.$PL.debug('gitinfo:', data);
16 2
    cb(null, processTree(data, config));
17
}
18
19 1
module.exports.config = {
20
    verbose: false
21
}
22
23 1
module.exports.search = function(items, link) {
24 1
    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 1
    return results;
34
}
35
36
37
function getGitInfo(value, config, pl) {
38 2
    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...
39
    var logFormatYaml = 
40 1
	    '- date: \'%cd\''+'%n'+
41
	    '  timestamp: %ct'+'%n'+
42
	    '  author: %an'+'%n'+
43
	    '  email: %ae'+'%n'+
44
	    '  message: \'\"%s\"\''+'%n'+
45
	    '  commit: %h'+'%n'+
46
	    '  hash: %H'+'%n'+
47
	    '  body: \'\"%b\"\''+'%n'+
48
	    '%n';
49
50
    var logFormatCompact = 
51 1
	    '### %cd'+'%n'+
52
	    '%n'+
53
	    '- %s (%h, by [%an](mailto:%ae), <%ae>)'+'%n'+
54
	    '%n'+
55
	    '%n';
56
57
    var logFormatFull = 
58 1
	    '### %cD'+'%n'+
59
	    '%n'+
60
	    '- Author: [%an](mailto:%ae) (<%ae>)'+'%n'+
61
	    '%n'+
62
	    '  Commit: %H'+'%n'+
63
	    '%n'+
64
	    '  Message: %s'+'%n'+
65
	    '%n'+
66
	    //'  Body: \'%b\''+'%n'+
67
	    //'%n'+
68
	    //'  \\`\\`\\`diff'+'%n'+
69
	    //'  \`git diff %H '+value+'\`'+'%n'+
70
	    //'  \\`\\`\\`'+'%n'+
71
	    '%n';
72
   
73
74 1
    var logData    = yaml.load( getGitLog(value, logFormatYaml, config) );
75 1
    var logCompact = getGitLog(value, logFormatCompact, config);
76 1
    var logFull    = getGitLog(value, logFormatFull, config);
77
    
78 1
    return {
79
    	log: {
80
	    	data: logData,
81
	    	list: pl.markdown.render(String(logCompact)),
82
	    	full: pl.markdown.render(String(logFull))
83
	    }
84
    };
85
}
86
87
88
function processTree(tree, config) {
89
90 2
	var logs = {};
91 4
	if (tree && tree.log) {
92 1
		for (var i in tree.log.data) {
93 1
			var obj = tree.log.data[i];
94 1
			var logdate = obj.date;
95
			
96 2
			if (!logs[logdate]) { logs[logdate] = []; }
97 1
			logs[logdate].push(obj);
98
		}
99 1
		tree.log.data = logs;
100
	}
101
	
102 2
    return tree;
103
}
104
105
function getGitLog ( filepath, logFormat, config ) {
106 3
	var gitLog = git.log( filepath, 'format:"'+logFormat+'"' );
107 3
    return gitLog;
108
}
109
110
111