-
Notifications
You must be signed in to change notification settings - Fork 0
/
website_cli.ts
285 lines (243 loc) · 11 KB
/
website_cli.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { FIXABLE_ISSUE_TYPES, ISSUE_DESCRIPTIONS, ISSUE_TITLES, WARNING_ISSUE_TYPES } from "./constants.ts";
import { parse, stringify } from "./deps/oson.ts";
import { parseArgs, Spinner } from "./deps/std/cli.ts";
import { blue, bold, cyan, green, red, yellow } from "./deps/std/fmt.ts";
import { join, resolve } from "./deps/std/path.ts";
import { makePrettyDetails, processIssues } from "./issues.ts";
import { FixableIssue, Issue, IssueWithStack, Stack } from "./types.ts";
import { getPossibleMatches, indentText, parseLink } from "./utilities.ts";
import { readMarkdownFiles } from "./website.ts";
const args = parseArgs(Deno.args, {
boolean: ["clean-url", "allow-ext-html", "fix", "include-ref", "ignore-warnings"],
string: ["index-file"],
default: {
"index-file": "README.md",
"allow-ext-html": false,
"include-ref": false,
"ignore-warnings": false,
},
});
if (args._.length > 1) {
console.log("Multiple directories were specified. Ignoring everything except the first one.");
}
const rootDirectory = (args._[0] ?? ".").toString();
const cacheFile = join(rootDirectory, ".link-checker");
try {
const result = await Deno.lstat(join(rootDirectory, "ref"));
if (!result.isDirectory) throw new Deno.errors.NotFound();
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
console.error(red("Couldn't find the /ref directory containing generated API documentation."));
console.error(red("Please generate it by running the corresponding script and try again."));
Deno.exit(1);
}
}
if (args.fix) {
console.warn(
"%c| %cNote%c: You have specified the --fix argument. This will try to fix all the issues this tool can fix.\n",
"font-weight: bold",
"color: orange",
"color: none",
);
}
let grouped: Record<Issue["type"], IssueWithStack[]>;
if (Deno.env.get("DEBUG") != null) {
console.log("=== DEBUGGING MODE ===");
try {
console.log("reading the cache file");
grouped = parse(await Deno.readTextFile(cacheFile));
} catch (_error) {
console.log("failed to read the cache file");
const issues = await getIssues();
grouped = await processIssues(issues);
await Deno.writeTextFile(cacheFile, stringify(grouped));
console.log("cache file created and will be used next time debugging");
}
} else {
console.log("Reading files and checking for bad links...");
const issues = await getIssues();
grouped = await processIssues(issues);
}
const getIssueTypes = () => (Object.keys(grouped) as Issue["type"][]);
// .filter((type) => !(WARNING_ISSUE_TYPES.includes(type) && args["ignore-warnings"]));
const getTotal = () =>
getIssueTypes()
.filter((type) => !WARNING_ISSUE_TYPES.includes(type))
.reduce((total, type) => total + grouped[type].length, 0);
if (args["ignore-warnings"]) {
const count = WARNING_ISSUE_TYPES.reduce((p, type) => p + grouped[type].length, 0);
console.log(yellow("--ignore-warnings:"), `ignoring ${count} warnings`);
}
if (getIssueTypes().length === 0) {
console.log(green("Found no issues with links in the documentation!"));
Deno.exit(0);
}
const initial = getTotal();
console.log("\n" + red(bold(`Found ${initial} issues across the documentation:`)));
let totalPlaces = 0, fixed = 0;
if (args.fix) {
console.log(blue("note:"), "--fix was specified. trying to fix fixable issues...");
const spinner = new Spinner({ message: "fixing issues..." });
spinner.start();
let fixesMadeThisRound: number, round = 1;
do {
fixesMadeThisRound = 0;
spinner.message = `fixing: round ${round++}`;
for (const type of getIssueTypes()) {
if (!isFixableIssueType(type)) continue;
spinner.message = `fixing ${ISSUE_TITLES[type]} issues (${grouped[type].length})...`;
const groupLength = grouped[type].length;
let issueCount = 0;
for (let i = 0; issueCount < groupLength; i++, issueCount++) {
const issue = grouped[type][i];
totalPlaces += issue.stack.length;
const fixStrings = getFixedString(issue);
if (fixStrings == null) {
spinner.message = `(${issueCount}/${groupLength}) skipped: no fix available`;
continue;
}
const stackLength = grouped[type][i].stack.length;
const fixedPlaces = new Set<string>();
if (grouped[type][i].stack.length != 0) {
spinner.message = `(${issueCount}/${groupLength}) fixing...`;
}
// Fix all occurrences
for (let j = 0, stackCount = 1; stackCount <= stackLength; stackCount++, j++) {
const stack = grouped[type][i].stack[j];
if (stack.filepath.startsWith("ref/")) continue; // do not fix /ref stuff, just report it.
fixedPlaces.add(stack.filepath);
const content = await Deno.readTextFile(stack.filepath);
await Deno.writeTextFile(stack.filepath, content.replaceAll(fixStrings[0], fixStrings[1]));
grouped[type][i].stack.splice(j, 1), j--;
spinner.message = `(${issueCount}/${groupLength}): ${stack.filepath}`;
fixesMadeThisRound++;
}
// All occurrences were fixed, no use keeping the issue in accounts now.
if (grouped[type][i].stack.length == 0) {
grouped[type].splice(i--, 1);
spinner.message = `(${issueCount}/${groupLength}) fixed`;
}
// Update all issues with same references
spinner.message = "updating references...";
for (const type of getIssueTypes()) {
for (const issue of grouped[type]) {
if (!isFixableIssueType(issue.type)) break;
// Only update the reference if all the files have been updated:
if (issue.stack.some(({ filepath }) => !fixedPlaces.has(filepath))) continue;
switch (issue.type) {
case "redirected":
issue.from = issue.from.replace(fixStrings[0], fixStrings[1]);
break;
case "empty_anchor":
case "missing_anchor":
case "disallow_extension":
case "wrong_extension":
issue.reference = issue.reference.replace(fixStrings[0], fixStrings[1]);
break;
}
}
}
}
if (groupLength - grouped[type].length > 0) {
spinner.stop();
console.log(
green("fixed"),
`${groupLength - grouped[type].length} of ${groupLength} ${ISSUE_TITLES[type]} issues`,
);
spinner.start();
}
// No issues left in this group
if (grouped[type].length == 0) delete grouped[type];
fixed += fixesMadeThisRound;
}
} while (fixesMadeThisRound != 0);
spinner.stop();
console.log(green("done"), `resolved ${initial - getTotal()} issues completely and fixed problems in ${fixed} places.`);
if (fixed > 0) {
await Deno.writeTextFile(cacheFile, stringify(grouped));
console.log("cache file was updated to reflect the changes made by --fix");
}
}
console.log();
const warningIssueTypes = getIssueTypes()
.filter((type) => WARNING_ISSUE_TYPES.includes(type));
if (warningIssueTypes.length !== 0) {
console.log(yellow(bold("--------- WARNINGS ---------")));
console.log(warningIssueTypes.map((type) => getIssueTypeSummary(type)).join("\n") + "\n");
}
const issueTypes = getIssueTypes()
.filter((type) => !WARNING_ISSUE_TYPES.includes(type));
if (issueTypes.length > 0) {
console.log(red(bold("---------- ISSUES ----------")));
console.log(issueTypes.map((type) => getIssueTypeSummary(type)).join("\n") + "\n");
}
const current = getTotal();
console.log(`Checking completed and found ${bold(current.toString())} issues.`);
if (args.fix) console.log(`Fixed issues in ${bold(fixed.toString())} places.`);
if (current == 0 || getIssueTypes().every((type) => WARNING_ISSUE_TYPES.includes(type))) {
Deno.exit(0); // print the warnings but exit successfully, dont fail the check
}
Deno.exit(1);
function getIssues() {
return readMarkdownFiles(rootDirectory, {
isCleanUrl: args["clean-url"],
indexFile: args["index-file"],
allowHtmlExtension: args["allow-ext-html"],
includeRefDirectory: args["include-ref"],
});
}
function getIssueTypeSummary(type: Issue["type"]): string {
const title = `${bold(ISSUE_TITLES[type])} (${grouped[type].length})`;
const description = ISSUE_DESCRIPTIONS[type];
const issueInfo = grouped[type].map((issue) => {
return [
indentText(makePrettyDetails(issue), 1),
indentText(generateStackTrace(issue.stack), 4),
].join("\n");
}).join("\n\n");
return `\n${title}\n${description}\n\n${issueInfo}`;
}
/** Generate stacktrace for the report */
function generateStackTrace(stacktrace: Stack[]) {
return stacktrace.map((stack) =>
stack.locations.map((location) =>
location.columns.map((column) =>
[
`at ${cyan(resolve(stack.filepath))}`,
location.line === -1 ? "?" : yellow(location.line.toString()),
column === -1 ? "?" : yellow(column.toString()),
].join(":")
)
).flat()
).flat().join("\n");
}
/**
* Returns original search string and replaceable string if the issue can be fixed,
* otherwise returns undefined.
*/
function getFixedString(issue: IssueWithStack): [string, string] | undefined {
switch (issue.type) {
case "redirected":
return [issue.from, issue.to];
case "missing_anchor": {
const { root } = parseLink(decodeURIComponent(issue.reference));
const possible = getPossibleMatches(issue.anchor, issue.allAnchors)[0];
return possible == null ? undefined : [issue.reference, root + "#" + possible];
}
case "empty_anchor":
return [issue.reference, issue.reference.slice(0, -1)];
case "wrong_extension": {
const { root } = parseLink(issue.reference);
return [root, root.slice(0, -issue.actual.length) + issue.expected];
}
case "disallow_extension": {
const { root } = parseLink(issue.reference);
return [root, root.slice(0, -(issue.extension.length + 1))];
}
default:
throw new Error("Invalid fixable type");
}
}
function isFixableIssueType(type: Issue["type"]): type is FixableIssue["type"] {
return FIXABLE_ISSUE_TYPES.includes(type);
}