-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolint.js
211 lines (178 loc) · 6.09 KB
/
todolint.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const minimatch = require('minimatch');
const chalk = require('chalk');
let config = { warn: {} };
const configFile = path.join(process.cwd(), '.todolintrc.json');
if (fs.existsSync(configFile)) {
config = require(configFile);
}
let root = config.root || ['.'];
const tags = config.tags || [];
const warnFail = config.warn.fail || false;
const warnLimit = config.warn.limit === undefined ? (warnFail ? 0 : null) : config.warn.limit;
const warnTags = config.warn.tags || tags.map(tag => tag.name);
const defaultWarning = `⚠ ⚠ WARNING!! There are ${warnLimit ? `more than ${warnLimit} ` : ''}items that need addressing!! ⚠ ⚠`;
const warnMessage = config.warn.message || (warnLimit !== null ? defaultWarning : '');
let ignorePatterns = config.ignore || [];
ignorePatterns = ignorePatterns.concat('.todolintrc.json');
function todolint(onComplete) {
if (typeof onComplete === 'undefined') {
onComplete = function () {};
}
if (typeof root === 'string') {
root = [root];
}
if (!Array.isArray(root)) {
console.error(chalk.red(`"${root}" is not valid. Must be a string or array of strings.`));
onComplete('Invalid root');
}
let rootsToProcess = root.length;
let itemsToProcess = 0;
let totalWarnMessages = 0;
root.forEach(dir => {
readDirectoryRecursive(dir, (error, results) => {
if (error) {
console.error(chalk.red(error));
}
rootsToProcess -= 1;
itemsToProcess += results.length;
results.forEach(file => {
const messages = [];
const fileStream = fs.createReadStream(file.path);
const reader = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
let lineNumber = 0;
reader.on('line', line => {
lineNumber += 1;
tags.forEach(tag => {
let match = line.match(tag.regex);
if (match) {
// Remove beginning of string up to just after the tag match
let finalMessage = line.slice(match.index + match[0].length);
let description = '';
// Capture anything in '()' before the first ':'
match = finalMessage.match(/^.*\((.*)\).*:/);
if (match) {
description = match[1];
finalMessage = finalMessage.slice(match.index + match[0].length);
}
// Remove any preceding ':'
match = finalMessage.match(/^.*:/);
if (match) {
finalMessage = finalMessage.slice(match.index + match[0].length);
}
// Remove trailing '*/' comment terminator
match = finalMessage.match(/\s*\*\/.*$/);
if (match) {
finalMessage = finalMessage.slice(0, match.index);
}
let chalkStyle = chalk;
for (let i = 0; i < tag.style.length; i++) {
chalkStyle = chalkStyle[tag.style[i]];
if (typeof chalkStyle === 'undefined') {
console.error(chalk.red(`Style "${tag.style[i]}" is not valid. Tag "${tag.name}" skipped.`));
return;
}
}
messages.push({
line: lineNumber,
message: chalkStyle(` ${tag.label}${(description.length > 0 ? ` (${description})` : '')}: ${finalMessage.trim()} `)
});
if (warnTags.indexOf(tag.name) !== -1) {
totalWarnMessages += 1;
}
}
});
});
fileStream.on('end', () => {
if (messages.length > 0) {
messages.forEach(msg => {
const lineString = `${file.relativePath}:${msg.line} `;
console.log(chalk.white(lineString) + msg.message);
});
}
itemsToProcess -= 1;
if (itemsToProcess === 0) {
if (rootsToProcess === 0) {
if (warnLimit !== null && totalWarnMessages > warnLimit) {
console.log(`\n${chalk.red(warnMessage)}`);
if (warnFail) {
onComplete(warnMessage);
return;
}
}
onComplete();
return;
}
}
});
});
}, itemFilter);
});
}
function itemFilter(item) {
let allowed = true;
ignorePatterns.forEach(pattern => {
if (minimatch(item, pattern, { dot: true })) {
allowed = false;
}
});
return allowed;
}
function readDirectoryRecursive(dir, done, filter) {
let results = [];
fs.readdir(dir, (error, list) => {
if (error) {
return done(error, results);
}
let numItems = list.length;
// Base case. All items have been processed.
if (numItems === 0) {
return done(null, results);
}
list.forEach(item => {
item = path.resolve(dir, item);
const itemPath = path.relative(path.basename('.'), item);
fs.stat(item, (error, stat) => {
if (stat && stat.isDirectory()) {
// Item is a directory. Do recursive call if not filtered by filter.
if (filter(itemPath)) {
readDirectoryRecursive(item, (error, recursiveResults) => {
if (error) {
return done(error, results);
}
results = results.concat(recursiveResults);
numItems -= 1;
if (numItems === 0) {
return done(null, results);
}
}, filter);
} else {
numItems -= 1;
if (numItems === 0) {
return done(null, results);
}
}
} else {
// Item is a file. Add it to the list if not filtered by filter.
if (filter(itemPath)) {
results.push({
path: item,
relativePath: itemPath
});
}
numItems -= 1;
if (numItems === 0) {
return done(null, results);
}
}
});
});
});
}
module.exports = todolint;