-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
110 lines (98 loc) · 4.01 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
const colors = require('ansi-colors');
const fancyLog = require('fancy-log');
const leasot = require('leasot');
const path = require('path');
const PluginError = require('plugin-error');
const through = require('through2');
const Vinyl = require('vinyl');
const pluginName = 'gulp-todo';
function logCommentsToConsole(comments) {
comments.forEach(function(comment) {
const isTodo = /todo/i.test(comment.tag);
const commentType = isTodo ? colors.cyan(comment.tag) : colors.magenta(comment.tag);
const commentLocation = '@' + colors.gray(comment.file + ':' + comment.line);
fancyLog(commentType, comment.text, commentLocation);
});
}
/**
*
* @param {boolean} [absolute=false] Output absolute paths of files (as available via `file.path`)
* @param {string} [fileName=TODO.md] Specify the output filename
* @param {Object} [parseOptions={}] Passed directly to `leasot.parse` - See [ParseConfig](https://pgilad.github.io/leasot/interfaces/parseconfig.html)
* @param {string|function} [reporter=markdown] The reporter to use. See https://pgilad.github.io/leasot/enums/builtinreporters.html
* @param {Object} [reportOptions={}] Passed directly to `leasot.report` - See https://pgilad.github.io/leasot/index.html#report
* @param {boolean} [skipUnsupported=false] Whether to skip unsupported files or not
* @param {boolean} [verbose=false] Output comments to console as well
* @returns {*}
*/
module.exports = function({
absolute = false,
fileName = 'TODO.md',
parseOptions = {},
reporter = 'markdown',
reportOptions = {},
skipUnsupported = false,
verbose = false,
} = {}) {
let firstFile;
const 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
const ext = path.extname(file.path) || '.js';
//check if parser for filetype exists
if (!leasot.isExtensionSupported(ext)) {
if (!skipUnsupported) {
const msg = `File: ${file.path} with extension ${colors.red(ext)} is not supported`;
return cb(new PluginError(pluginName, msg));
}
if (verbose) {
const msg = `Skipping file ${file.path} with extension ${colors.red(ext)} as it is unsupported`;
fancyLog(msg);
}
return cb();
}
const filePath = absolute ? file.path : (file.path && file.relative) || file.path;
const parsedComments = leasot.parse(file.contents.toString(), {
associateParser: parseOptions.associateParser,
customParsers: parseOptions.customParsers,
customTags: parseOptions.customTags,
extension: ext,
filename: filePath,
withInlineFiles: parseOptions.withInlineFiles,
});
if (verbose) {
logCommentsToConsole(parsedComments);
}
comments.push(...parsedComments);
cb();
},
function reportTodos(cb) {
if (!firstFile) {
return cb();
}
const reporterContents = leasot.report(comments, reporter, reportOptions);
const todoFile = new Vinyl({
base: firstFile.base,
contents: Buffer.from(reporterContents),
cwd: firstFile.cwd,
path: path.join(firstFile.base, fileName),
});
// also pass along comments object for future reporters
todoFile.todos = comments;
cb(null, todoFile);
}
);
};
const reporter = require('./lib/reporter');
module.exports.reporter = reporter;