-
Notifications
You must be signed in to change notification settings - Fork 215
/
update_lint_rules.ts
67 lines (52 loc) · 1.5 KB
/
update_lint_rules.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
import { extractYaml } from "@std/front-matter";
interface DenoLintRuleDesc {
code: string;
tags: string[];
}
interface DenoLintRulesOutput {
version: string;
rules: DenoLintRuleDesc[];
}
async function getCurrentLintRules(): Promise<DenoLintRulesOutput> {
const output = await new Deno.Command(Deno.execPath(), {
args: ["lint", "--rules", "--json"],
}).outputSync();
if (!output.success) {
throw new Error("Failed to run `deno lint --rules --json`");
}
const data = new TextDecoder().decode(output.stdout);
return JSON.parse(data) satisfies DenoLintRulesOutput;
}
const missingDocs: string[] = [];
async function checkIfDocsExistAndUpdate(lintRulesData: DenoLintRulesOutput) {
for (const lintRule of lintRulesData.rules) {
const name = lintRule.code;
let content = "";
try {
content = await Deno.readTextFile(`./lint/rules/${name}.md`);
} catch {
missingDocs.push(name);
continue;
}
let fmData = {
body: "",
attrs: {},
};
try {
fmData = extractYaml(content);
} catch {
fmData.body = content;
}
fmData.attrs.tags = lintRule.tags;
const newContent = `---
tags: [${fmData.attrs.tags.join(", ")}]
---
${fmData.body}`;
await Deno.writeTextFile(`./lint/rules/${name}.md`, newContent);
}
}
const lintRulesData = await getCurrentLintRules();
await checkIfDocsExistAndUpdate(lintRulesData);
if (missingDocs.length !== 0) {
throw new Error(`Some rules are missing docs:\n${missingDocs.join("\n")}`);
}