-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·215 lines (185 loc) · 4.53 KB
/
index.mjs
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
212
213
214
215
#!/usr/bin/env node
import chalk from "chalk";
import { Command } from "commander";
import * as fs from "fs";
import * as readline from "readline";
function onlyAlphaNumeric(str) {
return str.replace(/[^a-zA-Z0-9\s\-\[\]\/]/g, "");
}
class Response {
begin() {
console.log("Begin");
}
end() {
console.log("End");
}
debug() {
console.log(this.constructor.name);
}
}
class ConsoleResponse extends Response {
begin(file) {
console.log(`Linting ${file}`);
console.log();
}
errorBegin() {
console.log(warn + "Found todos in file!");
console.log();
}
end() {
console.log(success + "Linting complete");
}
header(section) {
console.log(header(section));
}
headerEnd(_section) {
return;
}
warn(message, _message2) {
console.log(warn + message);
}
success() {
console.log(success + "No todos found in file");
}
}
class GitlabReportResponse extends Response {
begin(_file) {
console.log(
'<testsuites name="tasks test" tests="0" failures="0" errors="0" time="0">'
);
}
end() {
console.log("</testsuites>");
}
errorBegin() {
return;
}
header(section) {
console.log(
` <testsuite name="${section}" errors="0" failures="0" skipped="0">`
);
}
headerEnd(section) {
console.log(` </testsuite><!-- ${section} -->`);
}
warn(message, message2) {
console.log(
` <testcase classname="${onlyAlphaNumeric(
message2
)}" name="${onlyAlphaNumeric(message)}" time="0">
<failure message="${onlyAlphaNumeric(message2)}">${message}</failure>
</testcase>`
);
}
success() {
return;
}
}
const program = new Command();
const error = chalk.red("[error] ");
const warn = chalk.yellowBright("[warn] ");
const header = chalk.blueBright;
const success = chalk.greenBright("[success] ");
program
.name("todomd")
.description("A simple tool for working with TODO.md files")
.usage(
"lint\t\t - lints TODO.md\n todomd lint -f README.md\t - lints README.md"
)
.version(process.env.npm_package_version);
program
.command("lint")
.description("Lint a TODO.md file")
.option(
"-f, --filename",
"Location of markdown file to lint, defaults to TODO.md"
)
.option("-g, --gitlab", "Create a gitlab report response")
.parse()
.action((_str, options) => {
const todoFile = options.args[0] || "TODO.md";
const gitlabFile = options.args[1];
lint(todoFile, gitlabFile);
});
program
.command("init")
.description("Create a new TODO.md file")
.action(() => {
console.log("Creating a new TODO.md file");
init();
});
const lint = async (file, gitlab) => {
let response;
if (gitlab) {
response = new GitlabReportResponse();
} else {
response = new ConsoleResponse();
}
response.begin(file);
let currentSection = "";
let previousSection = "";
let printedHeader = false;
let found = false;
let shouldPrintHeaderEnd = false;
const fileStream = fs.createReadStream(file);
const lines = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (const line of lines) {
// Headers are # so they need to be isolated and only printed once
if (line.startsWith("#")) {
const header = line.match(/^#{1,3} (.*)/);
previousSection = currentSection;
currentSection = header[1];
printedHeader = false;
}
// match the line to see if it is a todo item
const match = line.match(/\s*- \[ \] (.*)/);
if (match) {
if (!found) {
response.errorBegin();
found = true;
}
if (!printedHeader) {
if (shouldPrintHeaderEnd) {
response.headerEnd(previousSection);
}
response.header(currentSection);
// make sure to print only once
printedHeader = true;
// make sure to close the header if in xml
shouldPrintHeaderEnd = true;
}
// print the actual warning
response.warn(match[0], match[1]);
}
}
if (!found) {
response.success();
}
// don't forget to close if it was the last one
if (shouldPrintHeaderEnd) {
response.headerEnd(currentSection);
}
response.end();
};
const init = () => {
fs.writeFileSync(
"TODO.md",
`
# TODO
sample README.md from [todo-md](https://github.com/todo-md/todo-md)
This text is not a task.
## Section
And this text neither.
- [ ] This task is open @owner
- [ ] And it has a subtask!
# BACKLOG
- [ ] This task is postponed
# DONE
- [x] This task is done #prio1
- [-] This task has been declined`
);
};
program.parse(process.argv);