-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
91 lines (82 loc) · 2.55 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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var leasot = require('leasot');
var defaults = require('lodash.defaults');
var omit = require('lodash.omit');
var countBy = require('lodash.countby');
var padRight = require('lodash.padright');
var PluginError = gutil.PluginError;
var pluginName = 'gulp-todo';
function logCommentsToConsole(comments) {
comments.forEach(function (comment) {
var isTodo = /todo/i.test(comment.kind);
var commentType = isTodo ? gutil.colors.cyan(comment.kind) : gutil.colors.magenta(comment.kind);
var commentLocation = '@' + gutil.colors.gray(comment.file + ':' + comment.line);
gutil.log(commentType, comment.text, commentLocation);
});
}
function logTotalsToConsole(header, totals) {
if(header){
gutil.log(gutil.colors.underline('Totals'));
}
Object.keys(totals).forEach(function(key) {
var isTodo = /todo/i.test(key);
var totalType = isTodo ? gutil.colors.cyan(key) : gutil.colors.magenta(key);
var totalCount = isTodo ? gutil.colors.cyan(totals[key]) : gutil.colors.magenta(totals[key]);
gutil.log(padRight(totalType, 20), String.fromCharCode(0x27A9), totalCount);
});
}
module.exports = function (options) {
options = defaults(options || {}, {
silent: false,
verbose: false,
absolute: false
});
var config = omit(options, ['verbose', 'absolute', 'silent']);
var firstFile;
var comments = [];
return through.obj(function collectTodos(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError(pluginName, 'Streaming not supported'));
return;
}
firstFile = firstFile || file;
//get extension - assume .js as default
var ext = path.extname(file.path) || '.js';
//check if parser for filetype exists
//TODO: perhaps just skip unsupported files
if (!leasot.isExtSupported(ext)) {
var msg = ['File:', file.path, '- Extension', gutil.colors.red(ext),
'is not supported'
].join(' ');
cb(new PluginError(pluginName, msg));
return;
}
var filePath;
if (options.absolute) {
filePath = file.path;
} else {
filePath = file.path && file.relative || file.path;
}
var _comments = leasot.parse(ext, file.contents.toString('utf8'), filePath);
if (options.verbose) {
logCommentsToConsole(_comments);
}
comments = comments.concat(_comments);
cb();
},
function reportTotals(cb) {
var totals = countBy(comments, function(c) { return c.kind; });
if(!options.silent){
logTotalsToConsole(options.verbose, totals);
}
this.emit('data', totals);
cb();
});
};