-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·115 lines (88 loc) · 2.94 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
111
112
113
114
115
#!/usr/bin/env node
const fs = require('fs');
const util = require('util');
const $path = require('path');
const readline = require('readline');
const Issuer = require('./lib/Issuer');
const { CWD } = require('./lib/constants');
const { highlight } = require('./lib/utils');
const { loadConfig } = require('./lib/config');
// utils
const readdir = util.promisify(fs.readdir);
function searchForTodo (path) {
return new Promise((resolve, reject) => {
const todos = [];
const stream = fs.createReadStream(path, { encoding: 'utf8' });
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
// listen for errors
stream.on('error', reject);
// read line by line
rl.on('line', (line) => {
const regex = /(TODO):?\s(.*)$/g;
const match = regex.exec(line);
if (match) {
todos.push({
path,
labels: [],
type: match[1],
title: match[2],
existingIssues: [],
});
}
});
// pass todos
rl.on('close', () => resolve(todos));
});
}
async function filterFiles (path = '.', config = {}) {
const files = [];
const subdirs = await readdir($path.resolve(CWD, path));
const { allowedExtensions, ignoreFiles, ignoreFolders } = config;
for (const subdir of subdirs) {
const filePath = $path.resolve(path, subdir);
const extName = $path.extname(filePath);
if (ignoreFolders.indexOf(subdir) > -1) {
continue;
}
if (allowedExtensions.indexOf(extName) > -1) {
files.push(filePath);
}
if (fs.lstatSync(filePath).isDirectory()) {
const filesInInnerFolder = await filterFiles(filePath, config);
files.push(...filesInInnerFolder);
}
}
return files;
}
async function init (config) {
const startTime = new Date();
const todos = [];
const files = await filterFiles(undefined, config);
for (const file of files) {
const fileTodos = await searchForTodo(file);
todos.push(...fileTodos);
}
const issuer = new Issuer({ config, todos });
await issuer.createIssues();
return {
totalFiles: files.length,
totalTodos: todos.length,
elpasedTime: new Date() - startTime,
};
}
(async function () {
try {
const config = loadConfig();
console.log(highlight.title('\nSearching for TODOs...\n'));
const {
totalFiles,
totalTodos,
elpasedTime,
} = await init(config);
console.log(highlight.success(`Found ${totalTodos} todos.`));
console.log(highlight.success(`Scanned ${totalFiles} files within ${elpasedTime / 1000}s.\n`));
} catch (exception) {
console.log(exception);
console.log(highlight.title(exception));
}
}());